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