use of org.locationtech.jts.geom.Polygon in project series-rest-api 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 graphhopper by graphhopper.
the class JTSTriangulator method triangulate.
public Result triangulate(Snap snap, QueryGraph queryGraph, ShortestPathTree shortestPathTree, ToDoubleFunction<ShortestPathTree.IsoLabel> fz, double tolerance) {
final NodeAccess na = queryGraph.getNodeAccess();
Collection<Coordinate> sites = new ArrayList<>();
shortestPathTree.search(snap.getClosestNode(), label -> {
double exploreValue = fz.applyAsDouble(label);
double lat = na.getLat(label.node);
double lon = na.getLon(label.node);
Coordinate site = new Coordinate(lon, lat);
site.z = exploreValue;
sites.add(site);
// add a pillar node to increase precision a bit for longer roads
if (label.parent != null) {
EdgeIteratorState edge = queryGraph.getEdgeIteratorState(label.edge, label.node);
PointList innerPoints = edge.fetchWayGeometry(FetchMode.PILLAR_ONLY);
if (innerPoints.size() > 0) {
int midIndex = innerPoints.size() / 2;
double lat2 = innerPoints.getLat(midIndex);
double lon2 = innerPoints.getLon(midIndex);
Coordinate site2 = new Coordinate(lon2, lat2);
site2.z = exploreValue;
sites.add(site2);
}
}
});
if (sites.size() > routerConfig.getMaxVisitedNodes() / 3)
throw new IllegalArgumentException("Too many nodes would be included in post processing (" + sites.size() + "). Let us know if you need this increased.");
// Sites may contain repeated coordinates. Especially for edge-based traversal, that's expected -- we visit
// each node multiple times.
// But that's okay, the triangulator de-dupes by itself, and it keeps the first z-value it sees, which is
// what we want.
Collection<ConstraintVertex> constraintVertices = sites.stream().map(ConstraintVertex::new).collect(Collectors.toList());
ConformingDelaunayTriangulator conformingDelaunayTriangulator = new ConformingDelaunayTriangulator(constraintVertices, tolerance);
conformingDelaunayTriangulator.setConstraints(new ArrayList<>(), new ArrayList<>());
conformingDelaunayTriangulator.formInitialDelaunay();
conformingDelaunayTriangulator.enforceConstraints();
Geometry convexHull = conformingDelaunayTriangulator.getConvexHull();
if (!(convexHull instanceof Polygon)) {
throw new IllegalArgumentException("Too few points found. " + "Please try a different 'point' or a larger 'time_limit'.");
}
QuadEdgeSubdivision tin = conformingDelaunayTriangulator.getSubdivision();
for (Vertex vertex : (Collection<Vertex>) tin.getVertices(true)) {
if (tin.isFrameVertex(vertex)) {
vertex.setZ(Double.MAX_VALUE);
}
}
ReadableTriangulation triangulation = ReadableTriangulation.wrap(tin);
return new Result(triangulation, triangulation.getEdges());
}
use of org.locationtech.jts.geom.Polygon in project graphhopper by graphhopper.
the class IsochroneResourceTest method requestTenBucketsIssue2094.
@Test
public void requestTenBucketsIssue2094() {
Response response = clientTarget(app, "/isochrone?profile=fast_car&point=42.510008,1.530018&time_limit=400&type=geojson&buckets=10").request().buildGet().invoke();
JsonFeatureCollection collection = response.readEntity(JsonFeatureCollection.class);
Polygon lastPolygon = (Polygon) collection.getFeatures().get(collection.getFeatures().size() - 1).getGeometry();
assertTrue(lastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.580229, 42.533161))));
assertFalse(lastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.584606, 42.535121))));
Polygon beforeLastPolygon = (Polygon) collection.getFeatures().get(collection.getFeatures().size() - 2).getGeometry();
assertTrue(beforeLastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.564136, 42.524938))));
assertFalse(beforeLastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.571474, 42.529176))));
}
use of org.locationtech.jts.geom.Polygon in project graphhopper by graphhopper.
the class AreaIndexTest method basic.
@Test
void basic() {
GeometryFactory geometryFactory = new GeometryFactory();
Polygon border1 = geometryFactory.createPolygon(new Coordinate[] { new Coordinate(1, 1), new Coordinate(2, 1), new Coordinate(2, 2), new Coordinate(1, 2), new Coordinate(1, 1) });
Polygon border2 = geometryFactory.createPolygon(new Coordinate[] { new Coordinate(5, 5), new Coordinate(6, 5), new Coordinate(6, 6), new Coordinate(5, 6), new Coordinate(5, 5) });
Polygon border3 = geometryFactory.createPolygon(new Coordinate[] { new Coordinate(9, 9), new Coordinate(10, 9), new Coordinate(10, 10), new Coordinate(9, 10), new Coordinate(9, 9) });
AreaIndex<CustomArea> index = new AreaIndex<>(Arrays.asList(createCustomArea("1", border1), createCustomArea("2", border2), createCustomArea("3", border3), createCustomArea("4", border2, border3)));
testQuery(index, 0, 0);
testQuery(index, 1.5, 1.5, "1");
testQuery(index, 1.00001, 1.00001, "1");
testQuery(index, 1.5, 1.00001, "1");
testQuery(index, 1.00001, 1.5, "1");
testQuery(index, 5.5, 5.5, "2", "4");
testQuery(index, 9.5, 9.5, "3", "4");
}
use of org.locationtech.jts.geom.Polygon in project ddf by codice.
the class GeospatialEvaluator method buildGeometry.
public static Geometry buildGeometry(String gmlText) throws IOException, SAXException, ParserConfigurationException {
String methodName = "buildGeometry";
LOGGER.trace(ENTERING_STR, methodName);
Geometry geometry = null;
gmlText = supportSRSName(gmlText);
try {
LOGGER.debug("Creating geoTools Configuration ...");
Configuration config = new org.geotools.gml3.GMLConfiguration();
LOGGER.debug("Parsing geoTools configuration");
Parser parser = new Parser(config);
LOGGER.debug("Parsing gmlText");
geometry = (Geometry) (parser.parse(new StringReader(gmlText)));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("geometry (before conversion): {}", geometry.toText());
}
// The metadata schema states that <gml:pos> elements specify points in
// LAT,LON order. But WKT specifies points in LON,LAT order. When the geoTools
// libraries return the geometry data, it's WKT is in LAT,LON order (which is
// incorrect).
// As a workaround here, for Polygons and Points (which are currently the only spatial
// criteria supported) we must swap the x,y of each coordinate so that they are
// specified in LON,LAT order and then use the swapped coordinates to create a new
// Polygon or Point to be returned to the caller.
GeometryFactory geometryFactory = new GeometryFactory();
if (geometry instanceof Polygon) {
// Build new array of coordinates using the swapped coordinates
ArrayList<Coordinate> newCoords = new ArrayList<Coordinate>();
// Swap each coordinate's x,y so that they specify LON,LAT order
for (Coordinate coord : geometry.getCoordinates()) {
newCoords.add(new Coordinate(coord.y, coord.x));
}
// Create a new polygon using the swapped coordinates
Polygon polygon = new Polygon(geometryFactory.createLinearRing(newCoords.toArray(new Coordinate[newCoords.size()])), null, geometryFactory);
if (LOGGER.isDebugEnabled()) {
// this logs the transformed WKT
LOGGER.debug("Translates to {}", polygon.toText());
// with LON,LAT ordered points
LOGGER.trace(EXITING_STR, methodName);
}
return polygon;
}
if (geometry instanceof Point) {
// Create a new point using the swapped coordinates that specify LON,LAT order
Point point = geometryFactory.createPoint(new Coordinate(geometry.getCoordinate().y, geometry.getCoordinate().x));
if (LOGGER.isDebugEnabled()) {
// this logs the transformed WKT
LOGGER.debug("Translates to {}", point.toText());
// with a LON,LAT ordered point
LOGGER.trace(EXITING_STR, methodName);
}
return point;
}
} catch (Exception e) {
LOGGER.debug("Exception using geotools", e);
}
LOGGER.debug("No translation done for geometry - probably not good ...");
LOGGER.trace(EXITING_STR, methodName);
return geometry;
}
Aggregations