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());
}
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()]));
}
}
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)));
}
}
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;
}
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;
}
Aggregations