MATSIM
GeoFileReader.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ShapeFileReader.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.gis;
22 
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;
36 import org.matsim.core.gbl.Gbl;
38 
39 import java.io.File;
40 import java.io.IOException;
41 import java.io.UncheckedIOException;
42 import java.net.URISyntaxException;
43 import java.net.URL;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.StandardCopyOption;
47 import java.util.*;
48 
55 public class GeoFileReader implements MatsimSomeReader {
56  private static final Logger log = LogManager.getLogger(GeoFileReader.class);
57 
58  private SimpleFeatureSource featureSource = null;
59 
60  private ReferencedEnvelope bounds = null;
61 
62  private DataStore dataStore = null;
63 
64  private SimpleFeatureCollection featureCollection = null;
65 
66  private SimpleFeatureType schema = null;
67 
68  private Collection<SimpleFeature> featureSet = null;
69 
70  private CoordinateReferenceSystem crs;
71 
72  public static Collection<SimpleFeature> getAllFeatures(final String filename) {
73  return getAllFeatures(filename, null);
74  }
75 
76 
77  public static Collection<SimpleFeature> getAllFeatures(final String filename, Name layerName) {
78  try {
79  if(filename.endsWith(".shp")) {
80  File dataFile = new File(filename);
81  log.info("will try to read from " + dataFile.getAbsolutePath());
82  Gbl.assertIf(dataFile.exists());
83  FileDataStore dataStore = FileDataStoreFinder.getDataStore(dataFile);
84  return getSimpleFeatures(dataStore);
85  } else if(filename.endsWith(".gpkg")){
86  Gbl.assertNotNull(layerName);
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);
92  return getSimpleFeatures(dataStore, layerName);
93  } else {
94  throw new RuntimeException("Unsupported file type.");
95  }
96  } catch (IOException e) {
97  throw new UncheckedIOException(e);
98  }
99  }
100 
101 
102 
103  public static Collection<SimpleFeature> getAllFeatures(final URL url) {
104  try {
105  log.info( "will try to read from " + url.getPath() ) ;
106  if (url.getFile().endsWith(".gpkg")) {
107  return getAllFeaturesGPKG(url);
108  }
109  return getSimpleFeatures(FileDataStoreFinder.getDataStore(url));
110  } catch (IOException e) {
111  throw new UncheckedIOException(e);
112  } catch (URISyntaxException e) {
113  throw new IllegalArgumentException(e);
114  }
115  }
116 
120  private static Collection<SimpleFeature> getAllFeaturesGPKG(final URL url) throws URISyntaxException, IOException {
121 
122  File file;
123  // Remote files have to be downloaded
124  if (url.getProtocol().startsWith("http") || url.getProtocol().startsWith("jar")) {
125 
126  String name = FilenameUtils.getBaseName(url.getFile());
127 
128  Path tmp = Files.createTempFile(name, ".gpkg");
129  Files.copy(url.openStream(), tmp, StandardCopyOption.REPLACE_EXISTING);
130 
131  file = tmp.toFile();
132  file.deleteOnExit();
133  } else
134  file = new File(url.toURI());
135 
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);
141 
142  String[] typeNames = dataStore.getTypeNames();
143 
144  // Use first layer
145  return getSimpleFeatures(dataStore, typeNames[0]);
146  }
147 
152  public static List<SimpleFeature> getSimpleFeatures(FileDataStore dataStore) throws IOException {
153  SimpleFeatureSource featureSource = dataStore.getFeatureSource();
154  List<SimpleFeature> featureSet = getSimpleFeatures(featureSource);
155  dataStore.dispose();
156  return featureSet;
157  }
158 
163  public static List<SimpleFeature> getSimpleFeatures(DataStore dataStore, Name layerName) throws IOException {
164  SimpleFeatureSource featureSource = dataStore.getFeatureSource(layerName);
165  Gbl.assertNotNull(featureSource);
166  List<SimpleFeature> featureSet = getSimpleFeatures(featureSource);
167  dataStore.dispose();
168  return featureSet;
169  }
170 
176  public static List<SimpleFeature> getSimpleFeatures(DataStore dataStore, String layerName) throws IOException {
177  SimpleFeatureSource featureSource = dataStore.getFeatureSource(layerName);
178  Gbl.assertNotNull(featureSource);
179  List<SimpleFeature> featureSet = getSimpleFeatures(featureSource);
180  dataStore.dispose();
181  return featureSet;
182  }
183 
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();
189  featureSet.add(ft);
190  }
191  it.close();
192  return featureSet;
193  }
194 
195  public Collection<SimpleFeature> readFileAndInitialize(final String filename) {
196  return readFileAndInitialize(filename, null);
197  }
198 
202  public Collection<SimpleFeature> readFileAndInitialize(final String filename, Name layerName) throws UncheckedIOException {
203  try {
204  this.featureSource = GeoFileReader.readDataFile(filename, layerName);
205  this.init();
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());
210  Counter cnt = new Counter("features read #");
211  while (it.hasNext()) {
212  ft = it.next();
213  this.featureSet.add(ft);
214  cnt.incCounter();
215  }
216  cnt.printCounter();
217  it.close();
218  return this.featureSet;
219  } catch (IOException e) {
220  throw new UncheckedIOException(e);
221  }
222  }
223 
224  public static SimpleFeatureSource readDataFile(final String filename) {
225  return readDataFile(filename, null);
226  }
227 
265  public static SimpleFeatureSource readDataFile(final String filename, Name layerName) throws UncheckedIOException {
266  try {
267  log.warn("Unsafe method! store.dispose() is not called from within this method");
268  SimpleFeatureSource featureSource;
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")) {
274  Gbl.assertNotNull(layerName);
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);
279 
280  DataStore datastore = DataStoreFinder.getDataStore(params);
281  featureSource = datastore.getFeatureSource(layerName);
282  Gbl.assertNotNull(featureSource);
283  } else {
284  throw new RuntimeException("Unsupported file type.");
285  }
286  return featureSource;
287  } catch (IOException e) {
288  throw new UncheckedIOException(e);
289  }
290  }
291 
292  private void init() {
293  try {
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);
301  }
302  }
303 
304  public SimpleFeatureSource getFeatureSource() {
305  return featureSource;
306  }
307 
308  public ReferencedEnvelope getBounds() {
309  return bounds;
310  }
311 
312  public DataStore getDataStore() {
313  return dataStore;
314  }
315 
316  public SimpleFeatureCollection getFeatureCollection() {
317  return featureCollection;
318  }
319 
320  public SimpleFeatureType getSchema() {
321  return schema;
322  }
323 
324  public Collection<SimpleFeature> getFeatureSet() {
325  return featureSet;
326  }
327 
328  public CoordinateReferenceSystem getCoordinateSystem(){
329  return this.crs;
330  }
331 
332 
333 }
Collection< SimpleFeature > readFileAndInitialize(final String filename, Name layerName)
static List< SimpleFeature > getSimpleFeatures(SimpleFeatureSource featureSource)
static List< SimpleFeature > getSimpleFeatures(DataStore dataStore, Name layerName)
static void assertIf(boolean flag)
Definition: Gbl.java:207
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)
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)
Definition: Gbl.java:212
Collection< SimpleFeature > readFileAndInitialize(final String filename)
static Collection< SimpleFeature > getAllFeatures(final URL url)
static SimpleFeatureSource readDataFile(final String filename, Name layerName)
CoordinateReferenceSystem getCoordinateSystem()