/** * make sure outer ring is CCW and holes are CW * * @param p polygon to check */ Polygon makeGoodSHAPEPolygon(Polygon p) { LinearRing outer; LinearRing[] holes = new LinearRing[p.getNumInteriorRing()]; Coordinate[] coords; coords = p.getExteriorRing().getCoordinates(); if (cga.isCCW(coords)) { outer = reverseRing((LinearRing) p.getExteriorRing()); } else { outer = (LinearRing) p.getExteriorRing(); } for (int t = 0; t < p.getNumInteriorRing(); t++) { coords = p.getInteriorRingN(t).getCoordinates(); if (!(cga.isCCW(coords))) { holes[t] = reverseRing((LinearRing) p.getInteriorRingN(t)); } else { holes[t] = (LinearRing) p.getInteriorRingN(t); } } return new Polygon(outer, holes, new PrecisionModel(), 0); }
protected Geometry transformPolygon(Polygon geom, Geometry parent) { boolean isAllValidLinearRings = true; Geometry shell = transformLinearRing((LinearRing) geom.getExteriorRing(), geom); if (shell == null || !(shell instanceof LinearRing) || shell.isEmpty()) isAllValidLinearRings = false; // return factory.createPolygon(null, null); ArrayList holes = new ArrayList(); for (int i = 0; i < geom.getNumInteriorRing(); i++) { Geometry hole = transformLinearRing((LinearRing) geom.getInteriorRingN(i), geom); if (hole == null || hole.isEmpty()) { continue; } if (!(hole instanceof LinearRing)) isAllValidLinearRings = false; holes.add(hole); } if (isAllValidLinearRings) return factory.createPolygon( (LinearRing) shell, (LinearRing[]) holes.toArray(new LinearRing[] {})); else { List components = new ArrayList(); if (shell != null) components.add(shell); components.addAll(holes); return factory.buildGeometry(components); } }
public Polygon addHole(Polygon poly, LinearRing hole) { int nOrigHoles = poly.getNumInteriorRing(); LinearRing[] newHoles = new LinearRing[nOrigHoles + 1]; for (int i = 0; i < nOrigHoles; i++) { newHoles[i] = (LinearRing) poly.getInteriorRingN(i); } newHoles[nOrigHoles] = hole; return geomFactory.createPolygon((LinearRing) poly.getExteriorRing(), newHoles); }
public Geobuf.Data.Geometry polyToGeobuf(Polygon poly) { Geobuf.Data.Geometry.Builder builder = Geobuf.Data.Geometry.newBuilder().setType(Geobuf.Data.Geometry.Type.POLYGON); Stream<LineString> interiorRings = IntStream.range(0, poly.getNumInteriorRing()).mapToObj(poly::getInteriorRingN); Stream.concat(Stream.of(poly.getExteriorRing()), interiorRings) .forEach(r -> addRing(r, builder)); return builder.build(); }
public Geobuf.Data.Geometry multiPolyToGeobuf(MultiPolygon poly) { Geobuf.Data.Geometry.Builder builder = Geobuf.Data.Geometry.newBuilder().setType(Geobuf.Data.Geometry.Type.MULTIPOLYGON); // first we specify the number of polygons builder.addLengths(poly.getNumGeometries()); for (int i = 0; i < poly.getNumGeometries(); i++) { Polygon p = (Polygon) poly.getGeometryN(i); // how many rings there are builder.addLengths(p.getNumInteriorRing() + 1); Stream<LineString> interiorRings = IntStream.range(0, p.getNumInteriorRing()).<LineString>mapToObj(p::getInteriorRingN); Stream.concat(Stream.of(p.getExteriorRing()), interiorRings) .forEach(r -> addRing(r, builder)); } return builder.build(); }