21 package org.matsim.core.utils.gis;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.geotools.api.data.DataStore;
26 import org.geotools.api.data.DataStoreFinder;
27 import org.geotools.api.data.FileDataStore;
28 import org.geotools.api.data.SimpleFeatureStore;
29 import org.geotools.api.feature.simple.SimpleFeature;
30 import org.geotools.api.feature.simple.SimpleFeatureType;
31 import org.geotools.api.feature.type.Name;
32 import org.geotools.data.shapefile.ShapefileDataStore;
33 import org.geotools.feature.DefaultFeatureCollection;
34 import org.geotools.feature.NameImpl;
35 import org.geotools.geopkg.GeoPkgDataStoreFactory;
36 import org.geotools.jdbc.JDBCDataStoreFactory;
40 import java.io.IOException;
41 import java.io.UncheckedIOException;
43 import java.util.Collection;
44 import java.util.HashMap;
58 public static void writeGeometries(
final Collection<SimpleFeature> features,
final String filename) {
63 public static void writeGeometries(
final Collection<SimpleFeature> features,
final String filename, Name layerName) {
64 if (features.isEmpty()) {
65 throw new UncheckedIOException(
new IOException(
"Cannot write empty collection"));
69 SimpleFeatureStore featureSource;
70 SimpleFeatureType featureType = features.iterator().next().getFeatureType();
72 if(filename.endsWith(
".shp")) {
73 log.info(
"Writing shapefile to " + filename);
74 URL fileURL = (
new File(filename)).toURI().toURL();
75 FileDataStore datastore =
new ShapefileDataStore(fileURL);
76 datastore.createSchema(featureType);
77 featureSource = (SimpleFeatureStore) datastore.getFeatureSource();
78 }
else if(filename.endsWith(
".gpkg")){
79 Map<String, Object> map =
new HashMap<>();
80 map.put(GeoPkgDataStoreFactory.DBTYPE.key, GeoPkgDataStoreFactory.DBTYPE.sample);
81 map.put(GeoPkgDataStoreFactory.DATABASE.key, filename);
82 map.put(JDBCDataStoreFactory.BATCH_INSERT_SIZE.key, 50);
83 DataStore datastore = DataStoreFinder.getDataStore(map);
84 datastore.createSchema(featureType);
85 if(layerName == null) {
86 layerName =
new NameImpl(featureType.getTypeName());
88 featureSource = (SimpleFeatureStore) datastore.getFeatureSource(layerName);
93 DefaultFeatureCollection coll =
new DefaultFeatureCollection();
94 coll.addAll(features);
96 featureSource.addFeatures(coll);
97 }
catch (IOException e) {
static void writeGeometries(final Collection< SimpleFeature > features, final String filename)
static void writeGeometries(final Collection< SimpleFeature > features, final String filename, Name layerName)