use of com.vividsolutions.jts.io.WKTWriter in project spatial-portal by AtlasOfLivingAustralia.
the class Util method createCircle.
public static String createCircle(double x, double y, final double radius, int sides) {
try {
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
String wkt4326 = "GEOGCS[" + "\"WGS 84\"," + " DATUM[" + " \"WGS_1984\"," + " SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," + " TOWGS84[0,0,0,0,0,0,0]," + " AUTHORITY[\"EPSG\",\"6326\"]]," + " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]]," + " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]]," + " AXIS[\"Lat\",NORTH]," + " AXIS[\"Long\",EAST]," + " AUTHORITY[\"EPSG\",\"4326\"]]";
String wkt900913 = "PROJCS[\"WGS84 / Google Mercator\", " + " GEOGCS[\"WGS 84\", " + " DATUM[\"World Geodetic System 1984\", " + " SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], " + " AUTHORITY[\"EPSG\",\"6326\"]], " + " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], " + " UNIT[\"degree\", 0.017453292519943295], " + " AXIS[\"Longitude\", EAST], " + " AXIS[\"Latitude\", NORTH], " + " AUTHORITY[\"EPSG\",\"4326\"]], " + " PROJECTION[\"Mercator_1SP\"], " + " PARAMETER[\"semi_minor\", 6378137.0], " + " PARAMETER[\"latitude_of_origin\", 0.0]," + " PARAMETER[\"central_meridian\", 0.0], " + " PARAMETER[\"scale_factor\", 1.0], " + " PARAMETER[\"false_easting\", 0.0], " + " PARAMETER[\"false_northing\", 0.0], " + " UNIT[\"m\", 1.0], " + " AXIS[\"x\", EAST], " + " AXIS[\"y\", NORTH], " + " AUTHORITY[\"EPSG\",\"3857\"]] ";
CoordinateReferenceSystem wgsCRS = CRS.parseWKT(wkt4326);
CoordinateReferenceSystem googleCRS = CRS.parseWKT(wkt900913);
MathTransform transform = CRS.findMathTransform(wgsCRS, googleCRS);
Point point = geometryFactory.createPoint(new Coordinate(y, x));
Geometry geom = JTS.transform(point, transform);
Point gPoint = geometryFactory.createPoint(new Coordinate(geom.getCoordinate()));
LOGGER.debug("Google point:" + gPoint.getCoordinate().x + "," + gPoint.getCoordinate().y);
MathTransform reverseTransform = CRS.findMathTransform(googleCRS, wgsCRS);
Coordinate[] coords = new Coordinate[sides + 1];
for (int i = 0; i < sides; i++) {
double angle = ((double) i / (double) sides) * Math.PI * 2.0;
double dx = Math.cos(angle) * radius;
double dy = Math.sin(angle) * radius;
geom = JTS.transform(geometryFactory.createPoint(new Coordinate(gPoint.getCoordinate().x + dx, gPoint.getCoordinate().y + dy)), reverseTransform);
coords[i] = new Coordinate(geom.getCoordinate().y, geom.getCoordinate().x);
}
coords[sides] = coords[0];
LinearRing ring = geometryFactory.createLinearRing(coords);
Polygon polygon = geometryFactory.createPolygon(ring, null);
WKTWriter writer = new WKTWriter();
String wkt = writer.write(polygon);
return wkt.replaceAll(StringConstants.POLYGON + " ", StringConstants.POLYGON).replaceAll(", ", ",");
} catch (Exception e) {
LOGGER.debug("Circle fail!");
return StringConstants.NONE;
}
}
use of com.vividsolutions.jts.io.WKTWriter 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 com.vividsolutions.jts.io.WKTWriter in project ddf by codice.
the class WfsFilterDelegate method bufferGeometry.
private String bufferGeometry(String wkt, double distance) {
LOGGER.debug("Buffering WKT {} by distance {} meter(s).", wkt, distance);
String bufferedWkt = null;
try {
Geometry geometry = getGeometryFromWkt(wkt);
double bufferInDegrees = metersToDegrees(distance);
LOGGER.debug("Buffering {} by {} degree(s).", geometry.getClass().getSimpleName(), bufferInDegrees);
Geometry bufferedGeometry = geometry.buffer(bufferInDegrees);
bufferedWkt = new WKTWriter().write(bufferedGeometry);
LOGGER.debug("Buffered WKT: {}.", bufferedWkt);
} catch (ParseException e) {
throw new IllegalArgumentException("Unable to parse WKT String", e);
}
return bufferedWkt;
}
use of com.vividsolutions.jts.io.WKTWriter in project incubator-rya by apache.
the class GeoFilterIT method showProcessorWorks.
@Test
public void showProcessorWorks() throws Exception {
// Enumerate some topics that will be re-used
final String ryaInstance = UUID.randomUUID().toString();
final UUID queryId = UUID.randomUUID();
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
// Get the RDF model objects that will be used to build the query.
final String sparql = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n" + "PREFIX geof: <" + GEO + ">\n" + "SELECT * \n" + "WHERE { \n" + " <urn:event1> geo:asWKT ?point .\n" + " FILTER(geof:sfWithin(?point, \"POLYGON((-3 -2, -3 2, 1 2, 1 -2, -3 -2))\"^^geo:wktLiteral)) " + "}";
// Setup a topology.
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
// Create the statements that will be input into the query.
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = getStatements();
// Make the expected results.
final Set<VisibilityBindingSet> expected = new HashSet<>();
final MapBindingSet bs = new MapBindingSet();
final WKTWriter w = new WKTWriter();
bs.addBinding("point", vf.createLiteral(w.write(ZERO), GeoConstants.XMLSCHEMA_OGC_WKT));
expected.add(new VisibilityBindingSet(bs, "a"));
// Run the test.
RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
}
use of com.vividsolutions.jts.io.WKTWriter in project incubator-rya by apache.
the class MongoIndexerDeleteIT method statement.
private static RyaStatement statement(final Geometry geo) {
final ValueFactory vf = new ValueFactoryImpl();
final Resource subject = vf.createURI("urn:geo");
final URI predicate = GeoConstants.GEO_AS_WKT;
final WKTWriter w = new WKTWriter();
final Value object = vf.createLiteral(w.write(geo), GeoConstants.XMLSCHEMA_OGC_WKT);
return RdfToRyaConversions.convertStatement(new StatementImpl(subject, predicate, object));
}
Aggregations