MATSIM
GeoFileWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ShapeFileWriter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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.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;
38 
39 import java.io.File;
40 import java.io.IOException;
41 import java.io.UncheckedIOException;
42 import java.net.URL;
43 import java.util.Collection;
44 import java.util.HashMap;
45 import java.util.Map;
46 
54 public class GeoFileWriter implements MatsimSomeWriter {
55 
56  private static final Logger log = LogManager.getLogger(GeoFileWriter.class);
57 
58  public static void writeGeometries(final Collection<SimpleFeature> features, final String filename) {
59  writeGeometries(features, filename, null);
60  }
61 
62 
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"));
66  }
67 
68  try {
69  SimpleFeatureStore featureSource;
70  SimpleFeatureType featureType = features.iterator().next().getFeatureType();
71 
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());
87  }
88  featureSource = (SimpleFeatureStore) datastore.getFeatureSource(layerName);
89  } else {
90  throw new RuntimeException("Unsupported file type.");
91  }
92 
93  DefaultFeatureCollection coll = new DefaultFeatureCollection();
94  coll.addAll(features);
95 
96  featureSource.addFeatures(coll);
97  } catch (IOException e) {
98  throw new RuntimeException(e);
99  }
100  }
101 }
static void writeGeometries(final Collection< SimpleFeature > features, final String filename)
static void writeGeometries(final Collection< SimpleFeature > features, final String filename, Name layerName)