use of org.locationtech.jts.geom.Polygon in project sensorweb-server-helgoland by 52North.
the class GeoJSONEncoder method encode.
protected ObjectNode encode(MultiPolygon geometry, int parentSrid) {
ObjectNode json = jsonFactory.objectNode();
ArrayNode list = json.put(JSONConstants.TYPE, JSONConstants.MULTI_POLYGON).putArray(JSONConstants.COORDINATES);
for (int i = 0; i < geometry.getNumGeometries(); ++i) {
list.add(encodeCoordinates((Polygon) geometry.getGeometryN(i)));
}
encodeCRS(json, geometry, parentSrid);
return json;
}
use of org.locationtech.jts.geom.Polygon in project cdmlib by cybertaxonomy.
the class GeometryBuilder method simpleCircle.
/**
* Creates perfect circles which are looking good but might be projected
* incorrectly for the resulting map
*
* @param distance
* @param latitude
* @param longitude
* @return
*/
public Geometry simpleCircle(Quantity<Length> distance, Double latitude, Double longitude) {
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
CoordinateSequence coordinateSequence = geometryFactory.getCoordinateSequenceFactory().create(new Coordinate[] { new Coordinate(longitude, latitude) });
Point point = new Point(coordinateSequence, geometryFactory);
GeodeticCalculator calc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
calc.setStartingGeographicPoint(longitude, latitude);
UnitConverter converter = distance.getUnit().getConverterTo(SI.METRE);
double d = converter.convert(distance.getValue()).doubleValue();
calc.setDirection(0.0, d);
Point2D p2 = calc.getDestinationGeographicPoint();
calc.setDirection(90.0, d);
Point2D p3 = calc.getDestinationGeographicPoint();
double dy = p2.getY() - latitude;
double dx = p3.getX() - longitude;
double dist = (dy + dx) / 2.0;
Polygon p1 = (Polygon) point.buffer(dist);
return p1;
}
use of org.locationtech.jts.geom.Polygon in project geowave by locationtech.
the class GeometryUtils method fixRangeOfCoordinates.
/**
* Adjust geometry so that coordinates fit into long/lat bounds.
*
* <p> Split date-line crossing polygons.
*
* <p> For now, clip hemisphere crossing portions of the polygon.
*
* @param geometry
* @return list valid polygons
*/
public static List<Polygon> fixRangeOfCoordinates(final CoordinateReferenceSystem crs, final Geometry geometry) {
final List<Polygon> replacements = new ArrayList<>();
if (geometry instanceof MultiPolygon) {
final MultiPolygon multi = (MultiPolygon) geometry;
for (int i = 0; i < multi.getNumGeometries(); i++) {
final Geometry geo = multi.getGeometryN(i);
replacements.addAll(fixRangeOfCoordinates(crs, geo));
}
return replacements;
} else // collection is more general than multi-polygon
if (geometry instanceof GeometryCollection) {
final GeometryCollection multi = (GeometryCollection) geometry;
for (int i = 0; i < multi.getNumGeometries(); i++) {
final Geometry geo = multi.getGeometryN(i);
replacements.addAll(fixRangeOfCoordinates(crs, geo));
}
return replacements;
}
final Coordinate[] geoCoords = geometry.getCoordinates();
final Coordinate modifier = findModifier(crs, geoCoords);
replacements.addAll(constructGeometriesOverMapRegions(modifier, geometry));
return replacements;
}
use of org.locationtech.jts.geom.Polygon in project geowave by locationtech.
the class GeometryUtils method visitGeometry.
public static void visitGeometry(final Geometry geom, final GeometryHandler geometryHandler) {
if (geom == null) {
return;
}
if (geom instanceof GeometryCollection) {
final int numGeom = ((GeometryCollection) geom).getNumGeometries();
for (int i = 0; i < numGeom; i++) {
visitGeometry(((GeometryCollection) geom).getGeometryN(i), geometryHandler);
}
} else if (geom instanceof LineString) {
geometryHandler.handleLineString((LineString) geom);
} else if (geom instanceof Polygon) {
geometryHandler.handlePolygon((Polygon) geom);
} else {
final Point centroid = geom.getCentroid();
geometryHandler.handlePoint(centroid);
}
}
use of org.locationtech.jts.geom.Polygon in project geowave by locationtech.
the class GeometryUtils method constructGeometriesOverMapRegions.
/**
* Produce a set of polygons for each region of the map corrected for date line and hemisphere
* crossings. Due to the complexity of going around the hemisphere, clip the range.
*
* <p> Consider a polygon that cross both the hemisphere in the north and the date line in the
* west (-182 92, -182 88, -178 88, -178 92, -182 92). The result is two polygons: (-180 90, -180
* 88, -178 88, -178 90, -180 90) (180 90, 180 88, 178 88, 178 90, 180 90)
*
* @param modifier
* @param geometry - a geometry that may cross date line and/or hemispheres.
* @return the set of polygons
*/
public static List<Polygon> constructGeometriesOverMapRegions(final Coordinate modifier, final Geometry geometry) {
final Coordinate[] geoCoords = geometry.getCoordinates();
final List<Polygon> polygons = new LinkedList<>();
final Geometry world = world(geometry.getFactory(), GeometryUtils.getDefaultCRS());
// First do the polygon unchanged world
final Geometry worldIntersections = world.intersection(geometry);
for (int i = 0; i < worldIntersections.getNumGeometries(); i++) {
final Polygon polyToAdd = (Polygon) worldIntersections.getGeometryN(i);
if (!polygons.contains(polyToAdd)) {
polygons.add(polyToAdd);
}
}
// optimization...do not modify if 0
if (Math.abs(modifier.x) > 0.0000000001) {
final Coordinate[] newCoords = new Coordinate[geoCoords.length];
int c = 0;
for (final Coordinate geoCoord : geoCoords) {
newCoords[c++] = new Coordinate(geoCoord.x + modifier.x, geoCoord.y, geoCoord.z);
}
final Polygon transposedPoly = geometry.getFactory().createPolygon(newCoords);
final Geometry adjustedPolyWorldIntersections = world.intersection(transposedPoly);
for (int i = 0; i < adjustedPolyWorldIntersections.getNumGeometries(); i++) {
final Polygon polyToAdd = (Polygon) adjustedPolyWorldIntersections.getGeometryN(i);
if (!polygons.contains(polyToAdd)) {
polygons.add(polyToAdd);
}
}
}
return polygons;
}
Aggregations