21 package org.matsim.core.utils.gis;
23 import org.apache.commons.io.FilenameUtils;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.geotools.api.data.*;
27 import org.geotools.api.feature.simple.SimpleFeature;
28 import org.geotools.api.feature.simple.SimpleFeatureType;
29 import org.geotools.api.feature.type.Name;
30 import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
31 import org.geotools.data.simple.SimpleFeatureCollection;
32 import org.geotools.data.simple.SimpleFeatureIterator;
33 import org.geotools.geometry.jts.ReferencedEnvelope;
34 import org.geotools.geopkg.GeoPkgDataStoreFactory;
40 import java.io.IOException;
41 import java.io.UncheckedIOException;
42 import java.net.URISyntaxException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.StandardCopyOption;
60 private ReferencedEnvelope
bounds = null;
66 private SimpleFeatureType
schema = null;
70 private CoordinateReferenceSystem
crs;
72 public static Collection<SimpleFeature>
getAllFeatures(
final String filename) {
77 public static Collection<SimpleFeature>
getAllFeatures(
final String filename, Name layerName) {
79 if(filename.endsWith(
".shp")) {
80 File dataFile =
new File(filename);
81 log.info(
"will try to read from " + dataFile.getAbsolutePath());
83 FileDataStore dataStore = FileDataStoreFinder.getDataStore(dataFile);
85 }
else if(filename.endsWith(
".gpkg")){
87 Map<String, Object> params =
new HashMap<>();
88 params.put(GeoPkgDataStoreFactory.DBTYPE.key,
"geopkg");
89 params.put(GeoPkgDataStoreFactory.DATABASE.key, filename);
90 params.put(GeoPkgDataStoreFactory.READ_ONLY.key,
true);
91 DataStore dataStore = DataStoreFinder.getDataStore(params);
96 }
catch (IOException e) {
97 throw new UncheckedIOException(e);
105 log.info(
"will try to read from " + url.getPath() ) ;
106 if (url.getFile().endsWith(
".gpkg")) {
110 }
catch (IOException e) {
111 throw new UncheckedIOException(e);
112 }
catch (URISyntaxException e) {
113 throw new IllegalArgumentException(e);
120 private static Collection<SimpleFeature>
getAllFeaturesGPKG(
final URL url)
throws URISyntaxException, IOException {
124 if (url.getProtocol().startsWith(
"http") || url.getProtocol().startsWith(
"jar")) {
126 String name = FilenameUtils.getBaseName(url.getFile());
128 Path tmp = Files.createTempFile(name,
".gpkg");
129 Files.copy(url.openStream(), tmp, StandardCopyOption.REPLACE_EXISTING);
134 file =
new File(url.toURI());
136 Map<String, Object> params =
new HashMap<>();
137 params.put(GeoPkgDataStoreFactory.DBTYPE.key,
"geopkg");
138 params.put(GeoPkgDataStoreFactory.DATABASE.key, file.toString());
139 params.put(GeoPkgDataStoreFactory.READ_ONLY.key,
true);
140 DataStore dataStore = DataStoreFinder.getDataStore(params);
142 String[] typeNames = dataStore.getTypeNames();
152 public static List<SimpleFeature>
getSimpleFeatures(FileDataStore dataStore)
throws IOException {
153 SimpleFeatureSource featureSource = dataStore.getFeatureSource();
163 public static List<SimpleFeature>
getSimpleFeatures(DataStore dataStore, Name layerName)
throws IOException {
164 SimpleFeatureSource featureSource = dataStore.getFeatureSource(layerName);
176 public static List<SimpleFeature>
getSimpleFeatures(DataStore dataStore, String layerName)
throws IOException {
177 SimpleFeatureSource featureSource = dataStore.getFeatureSource(layerName);
184 private static List<SimpleFeature>
getSimpleFeatures(SimpleFeatureSource featureSource)
throws IOException {
185 SimpleFeatureIterator it = featureSource.getFeatures().features();
186 List<SimpleFeature> featureSet =
new ArrayList<>();
187 while (it.hasNext()) {
188 SimpleFeature ft = it.next();
202 public Collection<SimpleFeature>
readFileAndInitialize(
final String filename, Name layerName)
throws UncheckedIOException {
206 SimpleFeature ft = null;
207 SimpleFeatureIterator it = this.featureSource.getFeatures().features();
208 this.featureSet =
new ArrayList<SimpleFeature>();
209 log.info(
"features to read #" + this.featureSource.getFeatures().size());
211 while (it.hasNext()) {
213 this.featureSet.add(ft);
219 }
catch (IOException e) {
220 throw new UncheckedIOException(e);
224 public static SimpleFeatureSource
readDataFile(
final String filename) {
265 public static SimpleFeatureSource
readDataFile(
final String filename, Name layerName)
throws UncheckedIOException {
267 log.warn(
"Unsafe method! store.dispose() is not called from within this method");
269 if(filename.endsWith(
".shp")) {
270 File dataFile =
new File(filename);
271 FileDataStore store = FileDataStoreFinder.getDataStore(dataFile);
272 featureSource = store.getFeatureSource();
273 }
else if(filename.endsWith(
".gpkg")) {
275 Map<String, Object> params =
new HashMap<>();
276 params.put(GeoPkgDataStoreFactory.DBTYPE.key,
"geopkg");
277 params.put(GeoPkgDataStoreFactory.DATABASE.key, filename);
278 params.put(GeoPkgDataStoreFactory.READ_ONLY.key,
true);
280 DataStore datastore = DataStoreFinder.getDataStore(params);
281 featureSource = datastore.getFeatureSource(layerName);
287 }
catch (IOException e) {
288 throw new UncheckedIOException(e);
294 this.bounds = this.featureSource.getBounds();
295 this.dataStore = (DataStore) this.featureSource.getDataStore();
296 this.featureCollection = this.featureSource.getFeatures();
297 this.schema = this.featureSource.getSchema();
298 this.crs = this.featureSource.getSchema().getCoordinateReferenceSystem();
299 }
catch (IOException e) {
300 throw new UncheckedIOException(e);
CoordinateReferenceSystem crs
Collection< SimpleFeature > readFileAndInitialize(final String filename, Name layerName)
static List< SimpleFeature > getSimpleFeatures(SimpleFeatureSource featureSource)
static List< SimpleFeature > getSimpleFeatures(DataStore dataStore, Name layerName)
ReferencedEnvelope bounds
static void assertIf(boolean flag)
ReferencedEnvelope getBounds()
SimpleFeatureCollection getFeatureCollection()
Collection< SimpleFeature > getFeatureSet()
static List< SimpleFeature > getSimpleFeatures(FileDataStore dataStore)
static Collection< SimpleFeature > getAllFeaturesGPKG(final URL url)
static Collection< SimpleFeature > getAllFeatures(final String filename, Name layerName)
SimpleFeatureSource featureSource
SimpleFeatureSource getFeatureSource()
Collection< SimpleFeature > featureSet
static SimpleFeatureSource readDataFile(final String filename)
static List< SimpleFeature > getSimpleFeatures(DataStore dataStore, String layerName)
SimpleFeatureCollection featureCollection
static Collection< SimpleFeature > getAllFeatures(final String filename)
static void assertNotNull(Object obj)
SimpleFeatureType getSchema()
Collection< SimpleFeature > readFileAndInitialize(final String filename)
static Collection< SimpleFeature > getAllFeatures(final URL url)
static SimpleFeatureSource readDataFile(final String filename, Name layerName)
CoordinateReferenceSystem getCoordinateSystem()