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);
}
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;
}
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);
}
Aggregations