Search in sources :

Example 26 with Polygon

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 27 with Polygon

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());
}
Also used : PointList(com.graphhopper.util.PointList) ConstraintVertex(org.locationtech.jts.triangulate.ConstraintVertex) Vertex(org.locationtech.jts.triangulate.quadedge.Vertex) NodeAccess(com.graphhopper.storage.NodeAccess) ArrayList(java.util.ArrayList) ConformingDelaunayTriangulator(org.locationtech.jts.triangulate.ConformingDelaunayTriangulator) QuadEdgeSubdivision(org.locationtech.jts.triangulate.quadedge.QuadEdgeSubdivision) ConstraintVertex(org.locationtech.jts.triangulate.ConstraintVertex) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) Collection(java.util.Collection) Polygon(org.locationtech.jts.geom.Polygon)

Example 28 with Polygon

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))));
}
Also used : Response(javax.ws.rs.core.Response) Coordinate(org.locationtech.jts.geom.Coordinate) JsonFeatureCollection(com.graphhopper.util.JsonFeatureCollection) Polygon(org.locationtech.jts.geom.Polygon) Test(org.junit.jupiter.api.Test)

Example 29 with Polygon

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");
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) Polygon(org.locationtech.jts.geom.Polygon) CustomArea(com.graphhopper.routing.util.CustomArea) AreaIndex(com.graphhopper.routing.util.AreaIndex) Test(org.junit.jupiter.api.Test)

Example 30 with Polygon

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;
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Configuration(org.geotools.xml.Configuration) ArrayList(java.util.ArrayList) Point(org.locationtech.jts.geom.Point) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Parser(org.geotools.xml.Parser) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) StringReader(java.io.StringReader) Polygon(org.locationtech.jts.geom.Polygon)

Aggregations

Polygon (org.locationtech.jts.geom.Polygon)56 LineString (org.locationtech.jts.geom.LineString)23 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)22 Point (org.locationtech.jts.geom.Point)22 Geometry (org.locationtech.jts.geom.Geometry)20 Coordinate (org.locationtech.jts.geom.Coordinate)16 MultiPoint (org.locationtech.jts.geom.MultiPoint)15 Test (org.junit.Test)13 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)11 LinearRing (org.locationtech.jts.geom.LinearRing)10 CustomCoordinateSequence (org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence)8 MultiLineString (org.locationtech.jts.geom.MultiLineString)8 ArrayList (java.util.ArrayList)7 Test (org.junit.jupiter.api.Test)6 DimensionInfo (org.apache.jena.geosparql.implementation.DimensionInfo)5 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)5 WKTReader (org.locationtech.jts.io.WKTReader)5 AreaIndex (com.graphhopper.routing.util.AreaIndex)4 CustomArea (com.graphhopper.routing.util.CustomArea)3 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)3