/** * Extracts the {@link Polygon} elements from a single {@link Geometry} * and returns them in a {@link List}. * * @param geom the geometry from which to extract */ public static List getPolygons(Geometry geom) { return getPolygons(geom, new ArrayList()); }
public static Geometry toMultiPolygon(Geometry g1, Geometry g2) { Geometry geoms = FunctionsUtil.buildGeometry(g1, g2); List polys = PolygonExtracter.getPolygons(g1); PolygonExtracter.getPolygons(g2, polys); return FunctionsUtil.getFactoryOrDefault(g1, g2) .createMultiPolygon( GeometryFactory.toPolygonArray(polys)); }
private Geometry getPolygonLines(Geometry g) { List lines = new ArrayList(); LinearComponentExtracter lineExtracter = new LinearComponentExtracter(lines); List polys = PolygonExtracter.getPolygons(g); for (Iterator i = polys.iterator(); i.hasNext(); ) { Polygon poly = (Polygon) i.next(); poly.apply(lineExtracter); } return g.getFactory().buildGeometry(lines); }
private Geometry onlyPolygon(Geometry result) { if ((result instanceof Polygon) || (result instanceof MultiPolygon)) { return result; } List polys = org.locationtech.jts.geom.util.PolygonExtracter.getPolygons(result); if (polys.size() == 0) { return null; } if (polys.size() == 1) { return (Polygon) polys.get(0); } // this could, theoretically, produce invalid MULTIPOLYGONS since polygons cannot share // edges. Taking // 2 polygons and putting them in a multipolygon is not always valid. However, many // systems will not correctly // deal with a GeometryCollection with multiple polygons in them. // The best strategy is to just create a (potentially) invalid multipolygon. return new MultiPolygon( (Polygon[]) polys.toArray(new Polygon[polys.size()]), result.getFactory()); }
/** * Computes a {@link Geometry} containing only {@link Polygonal} components. * Extracts the {@link Polygon}s from the input * and returns them as an appropriate {@link Polygonal} geometry. * <p> * If the input is already <tt>Polygonal</tt>, it is returned unchanged. * <p> * A particular use case is to filter out non-polygonal components * returned from an overlay operation. * * @param g the geometry to filter * @return a Polygonal geometry */ private static Geometry restrictToPolygons(Geometry g) { if (g instanceof Polygonal) { return g; } List polygons = PolygonExtracter.getPolygons(g); if (polygons.size() == 1) return (Polygon) polygons.get(0); return g.getFactory().createMultiPolygon(GeometryFactory.toPolygonArray(polygons)); } }
private void computeContainmentDistance(int polyGeomIndex, GeometryLocation[] locPtPoly) { Geometry polyGeom = geom[polyGeomIndex]; // if no polygon then nothing to do if (polyGeom.getDimension() < 2) return; int locationsIndex = 1 - polyGeomIndex; List polys = PolygonExtracter.getPolygons(polyGeom); if (polys.size() > 0) { List insideLocs = ConnectedElementLocationFilter.getLocations(geom[locationsIndex]); computeContainmentDistance(insideLocs, polys, locPtPoly); if (minDistance <= terminateDistance) { // this assigment is determined by the order of the args in the computeInside call above minDistanceLocation[locationsIndex] = locPtPoly[0]; minDistanceLocation[polyGeomIndex] = locPtPoly[1]; return; } } }