use of org.geotools.geometry.jts.GeometryBuilder 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.geotools.geometry.jts.GeometryBuilder in project GeoGig by boundlessgeo.
the class GeoGigDataStoreTest method testFeatureWriterAppend.
@Test
public void testFeatureWriterAppend() throws Exception {
dataStore.createSchema(linesType);
Transaction tx = new DefaultTransaction();
FeatureWriter<SimpleFeatureType, SimpleFeature> fw = dataStore.getFeatureWriterAppend(linesTypeName.getLocalPart(), tx);
LineString line = new GeometryBuilder().lineString(0, 0, 1, 1);
SimpleFeature f = (SimpleFeature) fw.next();
f.setAttribute("sp", "foo");
f.setAttribute("ip", 10);
f.setAttribute("pp", line);
fw.write();
fw.close();
tx.commit();
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(linesTypeName);
assertEquals(1, source.getCount(null));
FeatureReader<SimpleFeatureType, SimpleFeature> r = dataStore.getFeatureReader(new Query(linesTypeName.getLocalPart()), Transaction.AUTO_COMMIT);
assertTrue(r.hasNext());
f = r.next();
assertEquals("foo", f.getAttribute("sp"));
assertEquals(10, f.getAttribute("ip"));
assertTrue(line.equals((Geometry) f.getAttribute("pp")));
}
Aggregations