use of com.vividsolutions.jts.io.WKTReader 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 com.vividsolutions.jts.io.WKTReader in project ddf by codice.
the class TestGeoJsonInputTransformer method testGeometryCollectionStringGeo.
@Test
public void testGeometryCollectionStringGeo() throws IOException, CatalogTransformerException, ParseException {
InputStream inputStream = new ByteArrayInputStream(sampleGeometryCollectionJsonText().getBytes());
Metacard metacard = transformer.transform(inputStream);
verifyBasics(metacard);
WKTReader reader = new WKTReader();
Geometry geometry = reader.read(metacard.getLocation());
assertThat(geometry.getNumGeometries(), is(2));
}
use of com.vividsolutions.jts.io.WKTReader in project ddf by codice.
the class TestGeoJsonExtensible method verifyBasics.
private void verifyBasics(Metacard metacard) throws ParseException {
assertEquals(DEFAULT_TITLE, metacard.getTitle());
assertEquals(DEFAULT_URI, metacard.getResourceURI().toString());
assertEquals(DEFAULT_TYPE, metacard.getContentTypeName());
assertEquals(DEFAULT_VERSION, metacard.getContentTypeVersion());
assertEquals("<xml></xml>", metacard.getMetadata());
SimpleDateFormat dateFormat = new SimpleDateFormat(GeoJsonInputTransformer.ISO_8601_DATE_FORMAT);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals(DEFAULT_CREATED_DATE, dateFormat.format(metacard.getCreatedDate()));
assertEquals(DEFAULT_MODIFIED_DATE, dateFormat.format(metacard.getModifiedDate()));
assertEquals(DEFAULT_EXPIRATION_DATE, dateFormat.format(metacard.getExpirationDate()));
assertEquals(DEFAULT_EFFECTIVE_DATE, dateFormat.format(metacard.getEffectiveDate()));
assertArrayEquals(DEFAULT_BYTES, metacard.getThumbnail());
assertEquals(DEFAULT_TEMPERATURE, metacard.getAttribute(TEMPERATURE_KEY).getValue());
assertEquals(BASIC_METACARD.getName(), metacard.getMetacardType().getName());
WKTReader reader = new WKTReader();
Geometry geometry = reader.read(metacard.getLocation());
Coordinate[] coords = geometry.getCoordinates();
assertThat(coords[0].x, is(30.0));
assertThat(coords[0].y, is(10.0));
assertThat(coords[1].x, is(10.0));
assertThat(coords[1].y, is(30.0));
assertThat(coords[2].x, is(40.0));
assertThat(coords[2].y, is(40.0));
}
use of com.vividsolutions.jts.io.WKTReader in project ddf by codice.
the class TestGeoJsonInputTransformer method testPointGeo.
@Test()
public void testPointGeo() throws IOException, CatalogTransformerException, ParseException {
Metacard metacard = transformer.transform(new ByteArrayInputStream(samplePointJsonText().getBytes()));
verifyBasics(metacard);
WKTReader reader = new WKTReader();
Geometry geometry = reader.read(metacard.getLocation());
assertThat(geometry.getCoordinate().x, is(30.0));
assertThat(geometry.getCoordinate().y, is(10.0));
}
use of com.vividsolutions.jts.io.WKTReader in project GeoGig by boundlessgeo.
the class GeometryDiffTest method testNoNewGeometry.
@Test
public void testNoNewGeometry() throws Exception {
Geometry oldGeom = new WKTReader().read("MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))");
LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(Optional.of(oldGeom), Optional.fromNullable((Geometry) null));
LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText());
assertEquals(diff, deserializedDiff);
assertEquals("9 point(s) deleted, 0 new point(s) added, 0 point(s) moved", diff.toString());
Optional<Geometry> resultingGeom = diff.applyOn(Optional.of(oldGeom));
assertFalse(resultingGeom.isPresent());
}
Aggregations