Search in sources :

Example 6 with ShapefileDataStoreFactory

use of org.geotools.data.shapefile.ShapefileDataStoreFactory in project polymap4-core by Polymap4.

the class RFeatureStoreTests method testCopyFluesse.

public void testCopyFluesse() throws Exception {
    File f = new File("/home/falko/Data/WGN_SAX_INFO/Datenuebergabe_Behoerden_Stand_1001/Shapedateien/Chem_Zustand_Fliessgew_WK_Liste_CHEM_0912.shp");
    log.debug("opening shapefile: " + f);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put("url", f.toURI().toURL());
    params.put("create spatial index", Boolean.TRUE);
    ShapefileDataStore shapeDs = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    FeatureSource<SimpleFeatureType, SimpleFeature> shapeFs = shapeDs.getFeatureSource();
    // creating schema
    ds.createSchema(shapeFs.getSchema());
    // adding features
    RFeatureStore fs = (RFeatureStore) ds.getFeatureSource(shapeFs.getSchema().getName());
    fs.addFeatures(shapeFs.getFeatures());
    // check size
    assertEquals(669, fs.getFeatures().size());
    // iterating accept
    fs.getFeatures().accepts(new FeatureVisitor() {

        public void visit(Feature feature) {
            assertTrue(feature.getDefaultGeometryProperty().getValue() instanceof Geometry);
        }
    }, null);
    // check feature
    FeatureIterator<SimpleFeature> fsIt = fs.getFeatures().features();
    FeatureIterator<SimpleFeature> shapeIt = shapeFs.getFeatures().features();
    int count = 0;
    for (; fsIt.hasNext() && shapeIt.hasNext(); count++) {
        SimpleFeature f1 = fsIt.next();
        SimpleFeature f2 = shapeIt.next();
        for (Property prop1 : f1.getProperties()) {
            Property prop2 = f2.getProperty(prop1.getName());
            if (prop1.getValue() instanceof Geometry) {
            // skip
            } else {
                assertTrue("Property don't match: " + prop1.getName() + ": " + prop1.getValue() + "!=" + prop2.getValue(), Utilities.equals(prop1.getValue(), prop2.getValue()));
            }
        }
    // assertTrue( "Features are not equal: \n" + f1 + "\n" + f2, Utilities. );
    }
    assertEquals(669, count);
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) FeatureVisitor(org.opengis.feature.FeatureVisitor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Point(com.vividsolutions.jts.geom.Point) Geometry(com.vividsolutions.jts.geom.Geometry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File) Property(org.opengis.feature.Property)

Example 7 with ShapefileDataStoreFactory

use of org.geotools.data.shapefile.ShapefileDataStoreFactory in project hale by halestudio.

the class ShapefileInstanceWriter method createSchema.

/**
 * Step 2. method to create schema. This method will create filename as:<br>
 * - filename_schemaName_geometryType.shp if multiple schema and geom.<br>
 * - filename_schemaName.shp if multiple schemas.<br>
 * - filename_geometryType.shp if multiple geometries.<br>
 * - filename.shp single schema and geom.
 *
 * @param location location to store the shape files.
 *
 * @param schemaSftMap type is used as a template to describe the file
 *            contents.
 * @return shape file data store.
 * @throws IOException exception if any.
 */
private Map<String, Map<String, ShapefileDataStore>> createSchema(URI location, Map<String, Map<String, SimpleFeatureType>> schemaSftMap) throws IOException {
    if (schemaSftMap.isEmpty()) {
        throw new IOException("Cannot export to the shape file as no Geometry found!!");
    }
    Map<String, Map<String, ShapefileDataStore>> schemaDataStoreMap = new HashMap<String, Map<String, ShapefileDataStore>>();
    // logic to create file name based on the multiple schemas and/or
    // multiple geometries.
    int numberOfSchemas = schemaSftMap.keySet().size();
    for (Entry<String, Map<String, SimpleFeatureType>> schemaEntry : schemaSftMap.entrySet()) {
        int numberOfGeometries = schemaEntry.getValue().keySet().size();
        for (Entry<String, SimpleFeatureType> geometryEntry : schemaEntry.getValue().entrySet()) {
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            File file = createFileWithFormattedName(location, numberOfSchemas, schemaEntry, numberOfGeometries, geometryEntry);
            params.put(ShapefileConstants.URL_STRING, file.toURI().toURL());
            // create schema.
            ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
            ShapefileDataStore newDataStore;
            newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
            newDataStore.createSchema(geometryEntry.getValue());
            schemaDataStoreMap.computeIfAbsent(schemaEntry.getKey(), k -> new HashMap<String, ShapefileDataStore>()).put(geometryEntry.getKey(), newDataStore);
        }
    }
    return schemaDataStoreMap;
}
Also used : SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) Arrays(java.util.Arrays) AbstractGeoInstanceWriter(eu.esdihumboldt.hale.common.instance.io.impl.AbstractGeoInstanceWriter) Transaction(org.geotools.data.Transaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Map(java.util.Map) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) URI(java.net.URI) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ProgressIndicator(eu.esdihumboldt.hale.common.core.io.ProgressIndicator) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) AugmentedValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag) Collection(java.util.Collection) Set(java.util.Set) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ShapefileConstants(eu.esdihumboldt.hale.io.shp.ShapefileConstants) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) List(java.util.List) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) Entry(java.util.Map.Entry) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Geometry(org.locationtech.jts.geom.Geometry) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) HashMap(java.util.HashMap) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DefinitionUtil(eu.esdihumboldt.hale.common.schema.model.DefinitionUtil) ArrayList(java.util.ArrayList) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) IOException(java.io.IOException) File(java.io.File) Paths(java.nio.file.Paths) InstanceAccessor(eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor) ResourceIterator(eu.esdihumboldt.hale.common.instance.model.ResourceIterator) DefaultTransaction(org.geotools.data.DefaultTransaction) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) FileSystems(java.nio.file.FileSystems) Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) IOException(java.io.IOException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File)

Example 8 with ShapefileDataStoreFactory

use of org.geotools.data.shapefile.ShapefileDataStoreFactory in project tutorials by eugenp.

the class ShapeFile method main.

public static void main(String[] args) throws Exception {
    DefaultFeatureCollection collection = new DefaultFeatureCollection();
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
    SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point:srid=4326," + "name:String");
    SimpleFeatureType CITY = createFeatureType();
    addLocations(CITY, collection);
    File shapeFile = getNewShapeFile();
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    ShapefileDataStore dataStore = setDataStoreParams(dataStoreFactory, params, shapeFile, CITY);
    writeToFile(dataStore, collection);
}
Also used : Serializable(java.io.Serializable) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) HashMap(java.util.HashMap) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection)

Aggregations

Serializable (java.io.Serializable)8 HashMap (java.util.HashMap)8 ShapefileDataStoreFactory (org.geotools.data.shapefile.ShapefileDataStoreFactory)8 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)8 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)7 File (java.io.File)6 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)6 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)5 Feature (org.opengis.feature.Feature)4 SimpleFeature (org.opengis.feature.simple.SimpleFeature)4 Optional (com.google.common.base.Optional)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)2 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)2 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)2 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)2 Function (com.google.common.base.Function)1