use of org.geotools.data.store.ContentFeatureSource in project traffic-engine by opentraffic.
the class OSMUtils method toShapefile.
public static void toShapefile(List<SpatialDataItem> segs, String filename) throws SchemaException, IOException {
final SimpleFeatureType TYPE = DataUtilities.createType("Location", "the_geom:LineString:srid=4326," + "name:String");
System.out.println("TYPE:" + TYPE);
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
/*
* GeometryFactory will be used to create the geometry attribute of each feature,
* using a Point object for the location.
*/
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
for (SpatialDataItem seg : segs) {
featureBuilder.add(seg.getGeometry());
featureBuilder.add(seg.id);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
File newFile = new File(filename);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", newFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
/*
* TYPE is used as a template to describe the file contents
*/
newDataStore.createSchema(TYPE);
ContentFeatureSource cfs = newDataStore.getFeatureSource();
if (cfs instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) cfs;
SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
try {
featureStore.addFeatures(collection);
} catch (Exception problem) {
problem.printStackTrace();
} finally {
}
}
}
Aggregations