Search in sources :

Example 81 with Polygon

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

the class GeoJsonReaderTest method testPolygonWithMergableInnerHoles.

public void testPolygonWithMergableInnerHoles() {
    Geometry g = read("{\n" + "  \"type\": \"Polygon\",\n" + "  \"coordinates\": [[[0,0],[4,0],[0,4],[4,4],[0,0]],[[1,1],[1,3],[3,3],[1,1]],[[1,1],[3,1],[3,3],[1,1]]]\n" + "}");
    assertTrue(g instanceof Polygon);
    Polygon p = (Polygon) g;
    assertEquals(10, 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 82 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project Osmand by osmandapp.

the class MvtReader method readPolys.

/**
 * Create {@link Polygon} or {@link MultiPolygon} from MVT geometry drawing commands.
 *
 * @param geomFactory    creates JTS geometry
 * @param geomCmds       contains MVT geometry commands
 * @param cursor         contains current MVT extent position
 * @param ringClassifier
 * @return JTS geometry or null on failure
 */
private static Geometry readPolys(GeometryFactory geomFactory, List<Integer> geomCmds, Vec2d cursor, RingClassifier ringClassifier) {
    // Guard: must have header
    if (geomCmds.isEmpty()) {
        return null;
    }
    /**
     * Geometry command index
     */
    int i = 0;
    int cmdHdr;
    int cmdLength;
    GeomCmd cmd;
    List<LinearRing> rings = new ArrayList<>(1);
    CoordinateSequence nextCoordSeq;
    Coordinate nextCoord;
    while (i <= geomCmds.size() - MIN_POLYGON_LEN) {
        // --------------------------------------------
        // Expected: MoveTo command of length 1
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        // Guard: command type and length
        if (cmd != GeomCmd.MoveTo || cmdLength != 1) {
            break;
        }
        // Update cursor position with relative move
        cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
        // --------------------------------------------
        // Expected: LineTo command of length > 1
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        // Guard: command type and length
        if (cmd != GeomCmd.LineTo || cmdLength < 2) {
            break;
        }
        // (require at least (2 values * 2 params) + (current index 'i') + (1 for ClosePath))
        if ((cmdLength * GeomCmd.LineTo.getParamCount()) + i + 1 > geomCmds.size()) {
            break;
        }
        nextCoordSeq = geomFactory.getCoordinateSequenceFactory().create(2 + cmdLength, 2);
        // Set first point from MoveTo command
        nextCoord = nextCoordSeq.getCoordinate(0);
        nextCoord.setOrdinate(0, cursor.x);
        nextCoord.setOrdinate(1, cursor.y);
        // Set remaining points from LineTo command
        for (int lineToIndex = 0; lineToIndex < cmdLength; ++lineToIndex) {
            // Update cursor position with relative line delta
            cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
            nextCoord = nextCoordSeq.getCoordinate(lineToIndex + 1);
            nextCoord.setOrdinate(0, cursor.x);
            nextCoord.setOrdinate(1, cursor.y);
        }
        // --------------------------------------------
        // Expected: ClosePath command of length 1
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        if (cmd != GeomCmd.ClosePath || cmdLength != 1) {
            break;
        }
        // Set last point from ClosePath command
        nextCoord = nextCoordSeq.getCoordinate(nextCoordSeq.size() - 1);
        nextCoord.setOrdinate(0, nextCoordSeq.getOrdinate(0, 0));
        nextCoord.setOrdinate(1, nextCoordSeq.getOrdinate(0, 1));
        rings.add(geomFactory.createLinearRing(nextCoordSeq));
    }
    // Classify rings
    final List<Polygon> polygons = ringClassifier.classifyRings(rings, geomFactory);
    if (polygons.size() < 1) {
        return null;
    } else if (polygons.size() == 1) {
        return polygons.get(0);
    } else {
        return geomFactory.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()]));
    }
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) GeomCmd(com.wdtinc.mapbox_vector_tile.encoding.GeomCmd) 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 83 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project incubator-rya by apache.

the class GeoIndexerTest method testDcSearch.

@Test
public void testDcSearch() throws Exception {
    // test a ring around dc
    try (final GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) {
        f.setConf(conf);
        final ValueFactory vf = new ValueFactoryImpl();
        final Resource subject = vf.createURI("foo:subj");
        final URI predicate = GeoConstants.GEO_AS_WKT;
        final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Resource context = vf.createURI("foo:context");
        final Statement statement = new ContextStatementImpl(subject, predicate, object, context);
        f.storeStatement(convertStatement(statement));
        f.flush();
        final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 };
        final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2));
        final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {});
        Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS)));
        // test a ring outside the point
        final double[] OUT = { -77, 39, -76, 39, -76, 38, -77, 38, -77, 39 };
        final LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(OUT, 2));
        final Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {});
        Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS)));
    }
}
Also used : ContextStatementImpl(org.openrdf.model.impl.ContextStatementImpl) Statement(org.openrdf.model.Statement) RdfToRyaConversions.convertStatement(org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Resource(org.openrdf.model.Resource) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) Value(org.openrdf.model.Value) LinearRing(com.vividsolutions.jts.geom.LinearRing) Polygon(com.vividsolutions.jts.geom.Polygon) PackedCoordinateSequence(com.vividsolutions.jts.geom.impl.PackedCoordinateSequence) Test(org.junit.Test)

Example 84 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project incubator-rya by apache.

the class GeoMongoDBStorageStrategy method getQuery.

public DBObject getQuery(final GeoQuery queryObj) throws MalformedQueryException {
    final Geometry geo = queryObj.getGeo();
    final GeoQueryType queryType = queryObj.getQueryType();
    if (queryType == GeoQueryType.WITHIN && !(geo instanceof Polygon)) {
        // They can also be applied to MultiPolygons, but those are not supported either.
        throw new MalformedQueryException("Mongo Within operations can only be performed on Polygons.");
    } else if (queryType == GeoQueryType.NEAR && !(geo instanceof Point)) {
        // They can also be applied to Point, but those are not supported either.
        throw new MalformedQueryException("Mongo near operations can only be performed on Points.");
    }
    BasicDBObject query;
    if (queryType.equals(GeoQueryType.EQUALS)) {
        if (geo.getNumPoints() == 1) {
            final List circle = new ArrayList();
            circle.add(getPoint(geo));
            circle.add(maxDistance);
            final BasicDBObject polygon = new BasicDBObject("$centerSphere", circle);
            query = new BasicDBObject(GEO, new BasicDBObject(GeoQueryType.WITHIN.getKeyword(), polygon));
        } else {
            query = new BasicDBObject(GEO, getCorrespondingPoints(geo));
        }
    } else if (queryType.equals(GeoQueryType.NEAR)) {
        final BasicDBObject geoDoc = new BasicDBObject("$geometry", getDBPoint(geo));
        if (queryObj.getMaxDistance() != 0) {
            geoDoc.append("$maxDistance", queryObj.getMaxDistance());
        }
        if (queryObj.getMinDistance() != 0) {
            geoDoc.append("$minDistance", queryObj.getMinDistance());
        }
        query = new BasicDBObject(GEO, new BasicDBObject(queryType.getKeyword(), geoDoc));
    } else {
        final BasicDBObject geoDoc = new BasicDBObject("$geometry", getCorrespondingPoints(geo));
        query = new BasicDBObject(GEO, new BasicDBObject(queryType.getKeyword(), geoDoc));
    }
    return query;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) MalformedQueryException(org.openrdf.query.MalformedQueryException) ArrayList(java.util.ArrayList) List(java.util.List) Point(com.vividsolutions.jts.geom.Point) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 85 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project incubator-rya by apache.

the class GeoMongoDBStorageStrategy method getCorrespondingPoints.

public Document getCorrespondingPoints(final Geometry geo) {
    // Polygons must be a 3 dimensional array.
    // polygons must be a closed loop
    final Document geoDoc = new Document();
    if (geo instanceof Polygon) {
        final Polygon poly = (Polygon) geo;
        final List<List<List<Double>>> DBpoints = new ArrayList<>();
        // outer shell of the polygon
        final List<List<Double>> ring = new ArrayList<>();
        for (final Coordinate coord : poly.getExteriorRing().getCoordinates()) {
            ring.add(getPoint(coord));
        }
        DBpoints.add(ring);
        // each hold in the polygon
        for (int ii = 0; ii < poly.getNumInteriorRing(); ii++) {
            final List<List<Double>> holeCoords = new ArrayList<>();
            for (final Coordinate coord : poly.getInteriorRingN(ii).getCoordinates()) {
                holeCoords.add(getPoint(coord));
            }
            DBpoints.add(holeCoords);
        }
        geoDoc.append("coordinates", DBpoints).append("type", "Polygon");
    } else {
        final List<List<Double>> points = getPoints(geo);
        geoDoc.append("coordinates", points).append("type", "LineString");
    }
    return geoDoc;
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point)

Aggregations

Polygon (com.vividsolutions.jts.geom.Polygon)114 LinearRing (com.vividsolutions.jts.geom.LinearRing)50 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)29 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)21 List (java.util.List)20 PersistenceManager (javax.jdo.PersistenceManager)15