Search in sources :

Example 11 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project StreetComplete by westnordost.

the class GeoJsonReader method createMultiPolygon.

private MultiPolygon createMultiPolygon(JSONArray coords) throws JSONException {
    ArrayList<Polygon> polygons = new ArrayList<>(coords.length());
    for (int i = 0; i < coords.length(); i++) {
        polygons.add(createPolygon(coords.getJSONArray(i)));
    }
    /* JTS MultiPolygon imposes a restriction on the contained Polygons that a GeoJson
		 * MultiPolygon does not impose. From the JTS documentation:
		 * "As per the OGC SFS specification, the Polygons in a MultiPolygon may not overlap, and
		 *  may only touch at single points. This allows the topological point-set semantics to be
		 *  well-defined."*/
    mergePolygons(polygons);
    return factory.createMultiPolygon(polygons.toArray(new Polygon[] {}));
}
Also used : ArrayList(java.util.ArrayList) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 12 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project StreetComplete by westnordost.

the class GeoJsonReader method createPolygon.

private Polygon createPolygon(JSONArray coords) throws JSONException {
    LinearRing[] linearRings = new LinearRing[coords.length()];
    for (int i = 0; i < coords.length(); i++) {
        linearRings[i] = createLinearRing(coords.getJSONArray(i));
    }
    LinearRing shell = linearRings[0];
    ArrayList<LinearRing> inner = new ArrayList<>();
    if (linearRings.length > 1) {
        LinearRing[] innerArray = new LinearRing[linearRings.length - 1];
        System.arraycopy(linearRings, 1, innerArray, 0, linearRings.length - 1);
        inner.addAll(Arrays.asList(innerArray));
        /* JTS imposes a restriction on a polygon that GeoJSON does not: that the linear rings
			   that define the holes may not touch each other in more than one point. So, we need to
			   merge inner linear rings that touch each other in a line together */
        mergeHoles(inner);
    }
    Polygon polygon = factory.createPolygon(shell, inner.toArray(new LinearRing[] {}));
    /* in JTS, outer shells are clockwise but in GeoJSON it is specified to be the other way
		   round. This reader is forgiving: It does not care about the direction, it will just
		   reorder if necessary (part of normalize) */
    polygon.normalize();
    return polygon;
}
Also used : ArrayList(java.util.ArrayList) LinearRing(com.vividsolutions.jts.geom.LinearRing) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 13 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project StreetComplete by westnordost.

the class GeoJsonReader method mergeHoles.

private void mergeHoles(ArrayList<LinearRing> rings) {
    if (rings.size() == 1)
        return;
    // need to be converted to polygons and back because linearring is a lineal data structure,
    // we want to merge by area
    ArrayList<Polygon> polygons = new ArrayList<>(rings.size());
    for (LinearRing ring : rings) {
        polygons.add(factory.createPolygon(ring));
    }
    mergePolygons(polygons);
    // something was merged. Convert polygons back to rings
    if (polygons.size() != rings.size()) {
        rings.clear();
        for (Polygon polygon : polygons) {
            rings.add((LinearRing) polygon.getExteriorRing());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Example 14 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project StreetComplete by westnordost.

the class GeoJsonReaderTest method testPolygon.

public void testPolygon() {
    Geometry g = read("{\n" + "  \"type\": \"Polygon\",\n" + "  \"coordinates\": [[[0,0],[4,0],[0,4],[0,0]],[[1,1],[1,2],[2,1],[1,1]]]\n" + "}");
    assertTrue(g instanceof Polygon);
    Polygon p = (Polygon) g;
    assertEquals(8, p.getNumPoints());
    assertEquals(1, p.getNumInteriorRing());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 15 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project OsmAnd-tools by osmandapp.

the class Multipolygon method toMultiPolygon.

public MultiPolygon toMultiPolygon() {
    GeometryFactory geometryFactory = new GeometryFactory();
    MultiPolygon emptyMultiPolygon = geometryFactory.createMultiPolygon(new Polygon[0]);
    List<Polygon> polygons = new ArrayList<>();
    for (Ring outerRing : outerRings) {
        if (!outerRing.isClosed()) {
            return emptyMultiPolygon;
        }
        List<LinearRing> innerLinearRings = new ArrayList<>();
        Set<Ring> innerRings = containedInnerInOuter.get(outerRing);
        if (!Algorithms.isEmpty(innerRings)) {
            for (Ring innerRing : innerRings) {
                if (!innerRing.isClosed()) {
                    return emptyMultiPolygon;
                }
                innerLinearRings.add(innerRing.toLinearRing());
            }
        }
        polygons.add(geometryFactory.createPolygon(outerRing.toLinearRing(), innerLinearRings.toArray(new LinearRing[innerLinearRings.size()])));
    }
    return geometryFactory.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()]));
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) LinearRing(com.vividsolutions.jts.geom.LinearRing) ArrayList(java.util.ArrayList) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Aggregations

Polygon (com.vividsolutions.jts.geom.Polygon)112 LinearRing (com.vividsolutions.jts.geom.LinearRing)51 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)34 Test (org.junit.Test)32 Coordinate (com.vividsolutions.jts.geom.Coordinate)30 Point (com.vividsolutions.jts.geom.Point)27 PackedCoordinateSequence (com.vividsolutions.jts.geom.impl.PackedCoordinateSequence)27 ArrayList (java.util.ArrayList)26 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)24 Resource (org.openrdf.model.Resource)24 Statement (org.openrdf.model.Statement)24 URI (org.openrdf.model.URI)24 Value (org.openrdf.model.Value)24 ValueFactory (org.openrdf.model.ValueFactory)24 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)24 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)24 Geometry (com.vividsolutions.jts.geom.Geometry)22 LineString (com.vividsolutions.jts.geom.LineString)22 List (java.util.List)20 PersistenceManager (javax.jdo.PersistenceManager)15