Search in sources :

Example 1 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature 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 SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project spatial-portal by AtlasOfLivingAustralia.

the class ShapefileUtils method loadShapefile.

public static Map loadShapefile(File shpfile) {
    try {
        FileDataStore store = FileDataStoreFinder.getDataStore(shpfile);
        LOGGER.debug("Loading shapefile. Reading content:");
        LOGGER.debug(store.getTypeNames()[0]);
        FeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
        FeatureCollection featureCollection = featureSource.getFeatures();
        FeatureIterator it = featureCollection.features();
        Map shape = new HashMap();
        StringBuilder sb = new StringBuilder();
        StringBuilder sbGeometryCollection = new StringBuilder();
        boolean isGeometryCollection = false;
        while (it.hasNext()) {
            SimpleFeature feature = (SimpleFeature) it.next();
            Geometry geom = (Geometry) feature.getDefaultGeometry();
            WKTWriter wkt = new WKTWriter();
            String wktString = wkt.write(geom);
            wktString = wktString.replaceAll(", ", ",");
            boolean valid = true;
            boolean multipolygon = false;
            boolean polygon = false;
            boolean geometrycollection = false;
            if (wktString.startsWith(StringConstants.MULTIPOLYGON + " ")) {
                wktString = wktString.substring((StringConstants.MULTIPOLYGON + " (").length(), wktString.length() - 1);
                multipolygon = true;
            } else if (wktString.startsWith(StringConstants.POLYGON + " ")) {
                wktString = wktString.substring((StringConstants.POLYGON + " ").length());
                polygon = true;
            } else if (wktString.startsWith(StringConstants.GEOMETRYCOLLECTION + " (")) {
                wktString = wktString.substring((StringConstants.GEOMETRYCOLLECTION + " (").length(), wktString.length() - 1);
                geometrycollection = true;
                isGeometryCollection = true;
            } else {
                valid = false;
            }
            if (valid) {
                if (sb.length() > 0) {
                    sb.append(",");
                    sbGeometryCollection.append(",");
                }
                sb.append(wktString);
                if (multipolygon) {
                    sbGeometryCollection.append(StringConstants.MULTIPOLYGON).append("(").append(wktString.replace("(((", "(("));
                    if (!wktString.endsWith(")))")) {
                        sbGeometryCollection.append(")");
                    }
                } else if (polygon) {
                    sbGeometryCollection.append(StringConstants.POLYGON).append(wktString);
                } else if (geometrycollection) {
                    sbGeometryCollection.append(wktString);
                }
            }
        }
        if (!isGeometryCollection) {
            if (!sb.toString().contains(")))")) {
                sb.append(")");
            }
            shape.put(StringConstants.WKT, StringConstants.MULTIPOLYGON + "(" + sb.toString().replace("(((", "(("));
        } else {
            sbGeometryCollection.append(")");
            shape.put(StringConstants.WKT, StringConstants.GEOMETRYCOLLECTION + "(" + sbGeometryCollection);
        }
        try {
            it.close();
        } catch (Exception e) {
        }
        return shape;
    } catch (Exception e) {
        LOGGER.error("Unable to load shapefile: ", e);
    }
    return null;
}
Also used : SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) WKTWriter(com.vividsolutions.jts.io.WKTWriter) HashMap(java.util.HashMap) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FeatureIterator(org.geotools.feature.FeatureIterator) Geometry(com.vividsolutions.jts.geom.Geometry) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) FeatureCollection(org.geotools.feature.FeatureCollection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project graphhopper by graphhopper.

the class ShapeFileReader method getFeatureIterator.

protected FeatureIterator<SimpleFeature> getFeatureIterator(DataStore dataStore) {
    if (dataStore == null)
        throw new IllegalArgumentException("DataStore cannot be null for getFeatureIterator");
    try {
        String typeName = dataStore.getTypeNames()[0];
        FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
        Filter filter = Filter.INCLUDE;
        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
        FeatureIterator<SimpleFeature> features = collection.features();
        return features;
    } catch (Exception e) {
        throw Utils.asUnchecked(e);
    }
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Filter(org.opengis.filter.Filter) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 4 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project ddf by codice.

the class TestPubSubOgcFilter method testContextualFeatureEvaluate.

@Test
@Ignore
public void testContextualFeatureEvaluate() throws TransformerException {
    SimpleFeature feature = generateSampleFeature();
    FilterFactory filterFactory = new FilterFactoryImpl();
    PropertyIsEqualTo filter = filterFactory.equal(filterFactory.property("name"), filterFactory.literal("FirstFeature"), true);
    printFilter(filter);
    assertTrue(filter.evaluate(feature));
}
Also used : PropertyIsEqualTo(org.opengis.filter.PropertyIsEqualTo) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FilterFactory(org.opengis.filter.FilterFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project ddf by codice.

the class TestPubSubOgcFilter method testGeospatialFeatureEvaluate.

@Test
@Ignore
public void testGeospatialFeatureEvaluate() throws TransformerException {
    SimpleFeature feature = generateSampleFeature();
    FilterFactoryImpl filterFactory = new FilterFactoryImpl();
    BBOX bboxFilter = filterFactory.bbox("geo", -114, 10, -110, 30, DefaultGeographicCRS.WGS84.toString());
    assertTrue(bboxFilter.evaluate(feature));
    BBOX bboxFilter1 = filterFactory.bbox("geo", -110, 10, 0, 30, DefaultGeographicCRS.WGS84.toString());
    assertFalse(bboxFilter1.evaluate(feature));
}
Also used : BBOX(org.opengis.filter.spatial.BBOX) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SimpleFeature (org.opengis.feature.simple.SimpleFeature)147 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)73 Test (org.junit.Test)49 IOException (java.io.IOException)35 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)35 ArrayList (java.util.ArrayList)26 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)25 Geometry (com.vividsolutions.jts.geom.Geometry)24 Coordinate (com.vividsolutions.jts.geom.Coordinate)22 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)20 File (java.io.File)19 HashMap (java.util.HashMap)18 Query (org.geotools.data.Query)18 RevFeature (org.locationtech.geogig.api.RevFeature)17 DefaultTransaction (org.geotools.data.DefaultTransaction)16 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)16 Optional (com.google.common.base.Optional)15 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)15 Filter (org.opengis.filter.Filter)15 Transaction (org.geotools.data.Transaction)14