Search in sources :

Example 1 with DefaultFeatureCollection

use of org.geotools.feature.DefaultFeatureCollection 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 DefaultFeatureCollection

use of org.geotools.feature.DefaultFeatureCollection in project incubator-rya by apache.

the class GeoMesaGeoIndexer method deleteStatements.

private void deleteStatements(final Collection<RyaStatement> ryaStatements) throws IOException {
    // create a feature collection
    final DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
    for (final RyaStatement ryaStatement : ryaStatements) {
        final Statement statement = RyaToRdfConversions.convertStatement(ryaStatement);
        // if the predicate list is empty, accept all predicates.
        // Otherwise, make sure the predicate is on the "valid" list
        final boolean isValidPredicate = validPredicates.isEmpty() || validPredicates.contains(statement.getPredicate());
        if (isValidPredicate && (statement.getObject() instanceof Literal)) {
            try {
                final SimpleFeature feature = createFeature(featureType, statement);
                featureCollection.add(feature);
            } catch (final ParseException e) {
                logger.warn("Error getting geo from statement: " + statement.toString(), e);
            }
        }
    }
    // remove this feature collection from the store
    if (!featureCollection.isEmpty()) {
        final Set<Identifier> featureIds = new HashSet<Identifier>();
        final FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);
        final Set<String> stringIds = DataUtilities.fidSet(featureCollection);
        for (final String id : stringIds) {
            featureIds.add(filterFactory.featureId(id));
        }
        final Filter filter = filterFactory.id(featureIds);
        featureStore.removeFeatures(filter);
    }
}
Also used : RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.openrdf.model.Statement) RyaStatement(org.apache.rya.api.domain.RyaStatement) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FilterFactory(org.opengis.filter.FilterFactory) Identifier(org.opengis.filter.identity.Identifier) Filter(org.opengis.filter.Filter) Literal(org.openrdf.model.Literal) ParseException(com.vividsolutions.jts.io.ParseException) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) HashSet(java.util.HashSet)

Example 3 with DefaultFeatureCollection

use of org.geotools.feature.DefaultFeatureCollection in project tutorials by eugenp.

the class GeoToolsUnitTest method givenFeatureType_whenAddLocations_returnFeatureCollection.

@Test
public void givenFeatureType_whenAddLocations_returnFeatureCollection() {
    DefaultFeatureCollection collection = new DefaultFeatureCollection();
    SimpleFeatureType CITY = ShapeFile.createFeatureType();
    ShapeFile.addLocations(CITY, collection);
    assertNotNull(collection);
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) Test(org.junit.Test)

Example 4 with DefaultFeatureCollection

use of org.geotools.feature.DefaultFeatureCollection in project polymap4-core by Polymap4.

the class UnitOfWork method prepare.

/**
 * Starts a new {@link Transaction} for the underlying {@link FeatureStore},
 * checks concurrent modifications and updates {@link FeatureStore}.
 *
 * @param monitor
 * @throws IOException
 * @throws {@link ConcurrentModificationException}
 */
public void prepare(IProgressMonitor monitor) throws ConcurrentModificationException, IOException {
    assert tx == null : "Pending transaction found.";
    if (modified.isEmpty()) {
        return;
    }
    monitor.beginTask("Prepare commit: " + getLabel(), modified.size());
    // set transaction
    tx = new DefaultTransaction(getClass().getSimpleName() + "-" + hashCode());
    fs.setTransaction(tx);
    DefaultFeatureCollection added = new DefaultFeatureCollection();
    Set<Identifier> removed = new HashSet();
    int count = 0;
    for (FeatureBufferState buffered : modified.values()) {
        if (monitor.isCanceled()) {
            return;
        }
        if ((++count % 100) == 0) {
            monitor.subTask("(" + count + ")");
        }
        if (buffered.isAdded()) {
            // no check if fid was created already since it is propably the 'primary key'
            added.add((SimpleFeature) buffered.feature());
        } else if (buffered.isModified()) {
            checkSubmitModified(buffered);
        } else if (buffered.isRemoved()) {
            removed.add(buffered.feature().getIdentifier());
        } else {
            log.warn("Buffered feature is not added/removed/modified!");
        }
        monitor.worked(1);
    }
    if (!added.isEmpty()) {
        fs.addFeatures(added);
        monitor.worked(added.size());
    }
    if (!removed.isEmpty()) {
        fs.removeFeatures(ff.id(removed));
        monitor.worked(added.size());
    }
    monitor.done();
}
Also used : Identifier(org.opengis.filter.identity.Identifier) DefaultTransaction(org.geotools.data.DefaultTransaction) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) HashSet(java.util.HashSet)

Example 5 with DefaultFeatureCollection

use of org.geotools.feature.DefaultFeatureCollection in project OpenTripPlanner by opentripplanner.

the class LIsochrone method makeContourFeatures.

/**
 * Create a geotools feature collection from a list of isochrones in the OTPA internal format.
 * Once in a FeatureCollection, they can for example be exported as GeoJSON.
 */
public static SimpleFeatureCollection makeContourFeatures(List<IsochroneData> isochrones) {
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(null, contourSchema);
    SimpleFeatureBuilder fbuilder = new SimpleFeatureBuilder(contourSchema);
    for (IsochroneData isochrone : isochrones) {
        fbuilder.add(isochrone.geometry);
        fbuilder.add(isochrone.cutoffSec);
        featureCollection.add(fbuilder.buildFeature(null));
    }
    return featureCollection;
}
Also used : IsochroneData(org.opentripplanner.analyst.core.IsochroneData) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)15 SimpleFeature (org.opengis.feature.simple.SimpleFeature)8 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)6 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)6 ParseException (com.vividsolutions.jts.io.ParseException)4 RyaStatement (org.apache.rya.api.domain.RyaStatement)4 Literal (org.openrdf.model.Literal)4 Statement (org.openrdf.model.Statement)4 Geometry (com.vividsolutions.jts.geom.Geometry)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Identifier (org.opengis.filter.identity.Identifier)3 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)2 ShapefileDataStoreFactory (org.geotools.data.shapefile.ShapefileDataStoreFactory)2 GeometryBuilder (org.geotools.geometry.jts.GeometryBuilder)2 Filter (org.opengis.filter.Filter)2 FilterFactory (org.opengis.filter.FilterFactory)2