use of org.opengis.feature.simple.SimpleFeature in project spatial-portal by AtlasOfLivingAustralia.
the class ShapefileUtils method saveShapefile.
public static void saveShapefile(File shpfile, String wktString, String name) {
try {
final SimpleFeatureType type = createFeatureType();
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
WKTReader wkt = new WKTReader();
Geometry geom = wkt.read(wktString);
if (geom instanceof GeometryCollection) {
GeometryCollection gc = (GeometryCollection) geom;
for (int i = 0; i < gc.getNumGeometries(); i++) {
Geometry g = gc.getGeometryN(i);
if (g instanceof Polygon) {
g = new GeometryBuilder().multiPolygon((Polygon) g);
}
featureBuilder.add(g);
SimpleFeature feature = featureBuilder.buildFeature(null);
feature.setAttribute("name", name);
features.add(feature);
}
} else {
Geometry g = geom;
if (g instanceof Polygon) {
g = new GeometryBuilder().multiPolygon((Polygon) g);
}
featureBuilder.add(g);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", shpfile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(type);
newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
DefaultFeatureCollection collection = new DefaultFeatureCollection();
collection.addAll(features);
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
LOGGER.error("error pricessing shape file: " + shpfile.getAbsolutePath(), problem);
transaction.rollback();
} finally {
transaction.close();
}
}
LOGGER.debug("Active Area shapefile written to: " + shpfile.getAbsolutePath());
} catch (Exception e) {
LOGGER.error("Unable to save shapefile: " + shpfile.getAbsolutePath(), e);
}
}
use of org.opengis.feature.simple.SimpleFeature in project spatial-portal by AtlasOfLivingAustralia.
the class ShapefileUtils method loadShapefile.
public static Map loadShapefile(File shpfile) {
try {
FileDataStore store = FileDataStoreFinder.getDataStore(shpfile);
LOGGER.debug("Loading shapefile. Reading content:");
LOGGER.debug(store.getTypeNames()[0]);
FeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
FeatureCollection featureCollection = featureSource.getFeatures();
FeatureIterator it = featureCollection.features();
Map shape = new HashMap();
StringBuilder sb = new StringBuilder();
StringBuilder sbGeometryCollection = new StringBuilder();
boolean isGeometryCollection = false;
while (it.hasNext()) {
SimpleFeature feature = (SimpleFeature) it.next();
Geometry geom = (Geometry) feature.getDefaultGeometry();
WKTWriter wkt = new WKTWriter();
String wktString = wkt.write(geom);
wktString = wktString.replaceAll(", ", ",");
boolean valid = true;
boolean multipolygon = false;
boolean polygon = false;
boolean geometrycollection = false;
if (wktString.startsWith(StringConstants.MULTIPOLYGON + " ")) {
wktString = wktString.substring((StringConstants.MULTIPOLYGON + " (").length(), wktString.length() - 1);
multipolygon = true;
} else if (wktString.startsWith(StringConstants.POLYGON + " ")) {
wktString = wktString.substring((StringConstants.POLYGON + " ").length());
polygon = true;
} else if (wktString.startsWith(StringConstants.GEOMETRYCOLLECTION + " (")) {
wktString = wktString.substring((StringConstants.GEOMETRYCOLLECTION + " (").length(), wktString.length() - 1);
geometrycollection = true;
isGeometryCollection = true;
} else {
valid = false;
}
if (valid) {
if (sb.length() > 0) {
sb.append(",");
sbGeometryCollection.append(",");
}
sb.append(wktString);
if (multipolygon) {
sbGeometryCollection.append(StringConstants.MULTIPOLYGON).append("(").append(wktString.replace("(((", "(("));
if (!wktString.endsWith(")))")) {
sbGeometryCollection.append(")");
}
} else if (polygon) {
sbGeometryCollection.append(StringConstants.POLYGON).append(wktString);
} else if (geometrycollection) {
sbGeometryCollection.append(wktString);
}
}
}
if (!isGeometryCollection) {
if (!sb.toString().contains(")))")) {
sb.append(")");
}
shape.put(StringConstants.WKT, StringConstants.MULTIPOLYGON + "(" + sb.toString().replace("(((", "(("));
} else {
sbGeometryCollection.append(")");
shape.put(StringConstants.WKT, StringConstants.GEOMETRYCOLLECTION + "(" + sbGeometryCollection);
}
try {
it.close();
} catch (Exception e) {
}
return shape;
} catch (Exception e) {
LOGGER.error("Unable to load shapefile: ", e);
}
return null;
}
use of org.opengis.feature.simple.SimpleFeature in project graphhopper by graphhopper.
the class ShapeFileReader method getFeatureIterator.
protected FeatureIterator<SimpleFeature> getFeatureIterator(DataStore dataStore) {
if (dataStore == null)
throw new IllegalArgumentException("DataStore cannot be null for getFeatureIterator");
try {
String typeName = dataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
Filter filter = Filter.INCLUDE;
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
FeatureIterator<SimpleFeature> features = collection.features();
return features;
} catch (Exception e) {
throw Utils.asUnchecked(e);
}
}
use of org.opengis.feature.simple.SimpleFeature in project ddf by codice.
the class TestPubSubOgcFilter method testContextualFeatureEvaluate.
@Test
@Ignore
public void testContextualFeatureEvaluate() throws TransformerException {
SimpleFeature feature = generateSampleFeature();
FilterFactory filterFactory = new FilterFactoryImpl();
PropertyIsEqualTo filter = filterFactory.equal(filterFactory.property("name"), filterFactory.literal("FirstFeature"), true);
printFilter(filter);
assertTrue(filter.evaluate(feature));
}
use of org.opengis.feature.simple.SimpleFeature in project ddf by codice.
the class TestPubSubOgcFilter method testGeospatialFeatureEvaluate.
@Test
@Ignore
public void testGeospatialFeatureEvaluate() throws TransformerException {
SimpleFeature feature = generateSampleFeature();
FilterFactoryImpl filterFactory = new FilterFactoryImpl();
BBOX bboxFilter = filterFactory.bbox("geo", -114, 10, -110, 30, DefaultGeographicCRS.WGS84.toString());
assertTrue(bboxFilter.evaluate(feature));
BBOX bboxFilter1 = filterFactory.bbox("geo", -110, 10, 0, 30, DefaultGeographicCRS.WGS84.toString());
assertFalse(bboxFilter1.evaluate(feature));
}
Aggregations