Search in sources :

Example 1 with GeometryBuilder

use of org.geotools.geometry.jts.GeometryBuilder in project spatial-portal by AtlasOfLivingAustralia.

the class ShapefileUtils method saveShapefile.

public static void saveShapefile(File shpfile, String wktString, String name) {
    try {
        final SimpleFeatureType type = createFeatureType();
        List<SimpleFeature> features = new ArrayList<SimpleFeature>();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
        WKTReader wkt = new WKTReader();
        Geometry geom = wkt.read(wktString);
        if (geom instanceof GeometryCollection) {
            GeometryCollection gc = (GeometryCollection) geom;
            for (int i = 0; i < gc.getNumGeometries(); i++) {
                Geometry g = gc.getGeometryN(i);
                if (g instanceof Polygon) {
                    g = new GeometryBuilder().multiPolygon((Polygon) g);
                }
                featureBuilder.add(g);
                SimpleFeature feature = featureBuilder.buildFeature(null);
                feature.setAttribute("name", name);
                features.add(feature);
            }
        } else {
            Geometry g = geom;
            if (g instanceof Polygon) {
                g = new GeometryBuilder().multiPolygon((Polygon) g);
            }
            featureBuilder.add(g);
            SimpleFeature feature = featureBuilder.buildFeature(null);
            features.add(feature);
        }
        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("url", shpfile.toURI().toURL());
        params.put("create spatial index", Boolean.TRUE);
        ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
        newDataStore.createSchema(type);
        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
        Transaction transaction = new DefaultTransaction("create");
        String typeName = newDataStore.getTypeNames()[0];
        SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
        if (featureSource instanceof SimpleFeatureStore) {
            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
            DefaultFeatureCollection collection = new DefaultFeatureCollection();
            collection.addAll(features);
            featureStore.setTransaction(transaction);
            try {
                featureStore.addFeatures(collection);
                transaction.commit();
            } catch (Exception problem) {
                LOGGER.error("error pricessing shape file: " + shpfile.getAbsolutePath(), problem);
                transaction.rollback();
            } finally {
                transaction.close();
            }
        }
        LOGGER.debug("Active Area shapefile written to: " + shpfile.getAbsolutePath());
    } catch (Exception e) {
        LOGGER.error("Unable to save shapefile: " + shpfile.getAbsolutePath(), e);
    }
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) WKTReader(com.vividsolutions.jts.io.WKTReader) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) GeometryBuilder(org.geotools.geometry.jts.GeometryBuilder) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 2 with GeometryBuilder

use of org.geotools.geometry.jts.GeometryBuilder in project GeoGig by boundlessgeo.

the class GeoGigDataStoreTest method testFeatureWriterAppend.

@Test
public void testFeatureWriterAppend() throws Exception {
    dataStore.createSchema(linesType);
    Transaction tx = new DefaultTransaction();
    FeatureWriter<SimpleFeatureType, SimpleFeature> fw = dataStore.getFeatureWriterAppend(linesTypeName.getLocalPart(), tx);
    LineString line = new GeometryBuilder().lineString(0, 0, 1, 1);
    SimpleFeature f = (SimpleFeature) fw.next();
    f.setAttribute("sp", "foo");
    f.setAttribute("ip", 10);
    f.setAttribute("pp", line);
    fw.write();
    fw.close();
    tx.commit();
    FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(linesTypeName);
    assertEquals(1, source.getCount(null));
    FeatureReader<SimpleFeatureType, SimpleFeature> r = dataStore.getFeatureReader(new Query(linesTypeName.getLocalPart()), Transaction.AUTO_COMMIT);
    assertTrue(r.hasNext());
    f = r.next();
    assertEquals("foo", f.getAttribute("sp"));
    assertEquals(10, f.getAttribute("ip"));
    assertTrue(line.equals((Geometry) f.getAttribute("pp")));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Query(org.geotools.data.Query) LineString(com.vividsolutions.jts.geom.LineString) GeometryBuilder(org.geotools.geometry.jts.GeometryBuilder) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Aggregations

Geometry (com.vividsolutions.jts.geom.Geometry)2 GeometryBuilder (org.geotools.geometry.jts.GeometryBuilder)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)2 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)1 LineString (com.vividsolutions.jts.geom.LineString)1 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)1 Polygon (com.vividsolutions.jts.geom.Polygon)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DefaultTransaction (org.geotools.data.DefaultTransaction)1 Query (org.geotools.data.Query)1 Transaction (org.geotools.data.Transaction)1 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)1 ShapefileDataStoreFactory (org.geotools.data.shapefile.ShapefileDataStoreFactory)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)1 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)1