use of org.geotools.data.simple.SimpleFeatureCollection in project OpenTripPlanner by opentripplanner.
the class SimpleIsochrone method zippedShapefileGet.
/**
* @return Evenly spaced travel time contours (isochrones) as a zipped shapefile.
*/
@GET
@Produces("application/x-zip-compressed")
public Response zippedShapefileGet(@QueryParam("stream") @DefaultValue("true") boolean stream) throws Exception {
SimpleFeatureCollection contourFeatures = makeContourFeatures();
/* Output the staged features to Shapefile */
final File shapeDir = Files.createTempDir();
File shapeFile = new File(shapeDir, shpName + ".shp");
LOG.debug("writing out shapefile {}", shapeFile);
ShapefileDataStore outStore = new ShapefileDataStore(shapeFile.toURI().toURL());
outStore.createSchema(contourSchema);
/* "FeatureSource is used to read features, the subclass FeatureStore is used for
* read/write access. The way to tell if a File can be written to in GeoTools is to use an
* instanceof check. */
SimpleFeatureStore featureStore = (SimpleFeatureStore) outStore.getFeatureSource();
featureStore.addFeatures(contourFeatures);
// close?
// Note: the order is important
shapeDir.deleteOnExit();
for (File f : shapeDir.listFiles()) f.deleteOnExit();
/* Zip up the shapefile components */
StreamingOutput output = new DirectoryZipper(shapeDir);
if (stream) {
return Response.ok().entity(output).build();
} else {
File zipFile = new File(shapeDir, shpName + ".zip");
OutputStream fos = new FileOutputStream(zipFile);
output.write(fos);
zipFile.deleteOnExit();
return Response.ok().entity(zipFile).build();
}
}
use of org.geotools.data.simple.SimpleFeatureCollection in project OpenTripPlanner by opentripplanner.
the class SimpleIsochrone method geoJsonGet.
/**
* @return Evenly spaced travel time contours (isochrones) as GeoJSON.
*/
@GET
@Produces("application/json")
public Response geoJsonGet() throws Exception {
/* QGIS seems to want multi-features rather than multi-geometries. */
SimpleFeatureCollection contourFeatures = makeContourFeatures();
/* Output the staged features to JSON */
StringWriter writer = new StringWriter();
FeatureJSON fj = new FeatureJSON();
fj.writeFeatureCollection(contourFeatures, writer);
return Response.ok().entity(writer.toString()).build();
}
use of org.geotools.data.simple.SimpleFeatureCollection in project OpenTripPlanner by opentripplanner.
the class ShapefilePopulation method createIndividuals.
@Override
public void createIndividuals() {
String filename = this.sourceFilename;
LOG.debug("Loading population from shapefile {}", filename);
LOG.debug("Feature attributes: input data in {}, labeled with {}", inputAttribute, labelAttribute);
try {
File file = new File(filename);
if (!file.exists())
throw new RuntimeException("Shapefile does not exist.");
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS();
CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true);
Query query = new Query();
query.setCoordinateSystem(sourceCRS);
query.setCoordinateSystemReproject(WGS84);
SimpleFeatureCollection featureCollection = featureSource.getFeatures(query);
SimpleFeatureIterator it = featureCollection.features();
int i = 0;
while (it.hasNext()) {
SimpleFeature feature = it.next();
Geometry geom = (Geometry) feature.getDefaultGeometry();
Point point = null;
if (geom instanceof Point) {
point = (Point) geom;
} else if (geom instanceof Polygon) {
point = ((Polygon) geom).getCentroid();
} else if (geom instanceof MultiPolygon) {
point = ((MultiPolygon) geom).getCentroid();
} else {
throw new RuntimeException("Shapefile must contain either points or polygons.");
}
String label;
if (labelAttribute == null) {
label = Integer.toString(i);
} else {
label = feature.getAttribute(labelAttribute).toString();
}
double input = 0.0;
if (inputAttribute != null) {
Number n = (Number) feature.getAttribute(inputAttribute);
input = n.doubleValue();
}
Individual individual = new Individual(label, point.getX(), point.getY(), input);
this.addIndividual(individual);
i += 1;
}
LOG.debug("loaded {} features", i);
it.close();
} catch (Exception ex) {
LOG.error("Error loading population from shapefile: {}", ex.getMessage());
throw new RuntimeException(ex);
}
LOG.debug("Done loading shapefile.");
}
use of org.geotools.data.simple.SimpleFeatureCollection in project hale by halestudio.
the class ShapefileInstanceWriter method writeToFile.
/**
* Final step to write to the shape file using transaction.
*
* @param schemaDataStoreMap data store for the shape file.
* @param schemaFtMap used as a template to describe the file contents.
* @param schemaFeaturesMap for each schema, each geom list of features to
* be written to the shape file.
* @throws IOException if any.
*/
private void writeToFile(Map<String, Map<String, ShapefileDataStore>> schemaDataStoreMap, Map<String, Map<String, SimpleFeatureType>> schemaFtMap, Map<String, Map<String, List<SimpleFeature>>> schemaFeaturesMap) throws IOException {
// extract each schema
for (Entry<String, Map<String, ShapefileDataStore>> schemaEntry : schemaDataStoreMap.entrySet()) {
String localPart = schemaEntry.getKey();
// extract each geometry.
for (Entry<String, ShapefileDataStore> geomEntry : schemaEntry.getValue().entrySet()) {
Transaction transaction = new DefaultTransaction(ShapefileConstants.CREATE_CONSTANT);
String typeName = geomEntry.getValue().getTypeNames()[0];
SimpleFeatureSource geomSpecificFeatureSource = geomEntry.getValue().getFeatureSource(typeName);
if (geomSpecificFeatureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore geomSpecificFeatureStore = (SimpleFeatureStore) geomSpecificFeatureSource;
// create collection to write to the shape file.
SimpleFeatureCollection collection = new ListFeatureCollection(schemaFtMap.get(localPart).get(geomEntry.getKey()), schemaFeaturesMap.get(localPart).get(geomEntry.getKey()));
geomSpecificFeatureStore.setTransaction(transaction);
try {
geomSpecificFeatureStore.addFeatures(collection);
transaction.commit();
} catch (IOException e) {
transaction.rollback();
throw e;
} finally {
transaction.close();
}
} else {
// throw exception
transaction.close();
throw new IOException(typeName + " does not support read/write access");
}
}
}
}
use of org.geotools.data.simple.SimpleFeatureCollection in project coastal-hazards by USGS-CIDA.
the class WFSExportClient method getFeatureCollection.
@Override
public SimpleFeatureCollection getFeatureCollection(String typeName, Filter filter) throws IOException {
if (wfs == null) {
throw new IllegalStateException("Must set up datastore prior to accessing wfs");
}
SimpleFeatureSource featureSource = wfs.getFeatureSource(typeName);
SimpleFeatureCollection sfc = null;
if (filter != null) {
sfc = featureSource.getFeatures(filter);
} else {
sfc = featureSource.getFeatures();
}
return sfc;
}
Aggregations