/** * Attempts to retype a geometry collection under the following circumstances, returning * null if the collection can not be retyped. * <ul> * <li>Single object collections are collapsed down to the object.</li> * <li>Homogenous collections are recast as the appropriate subclass.</li> * </ul> * * @see GeometryFactory#buildGeometry(Collection) */ private Geometry narrowCollectionIfPossible(GeometryCollection gc) { List<Geometry> geoms = new ArrayList<>(); for (int i = 0; i < gc.getNumGeometries(); i++) { geoms.add(gc.getGeometryN(i)); } Geometry result = gc.getFactory().buildGeometry(geoms); return !result.getClass().equals(GeometryCollection.class) ? result : null; }
/** * Helper method for {@link #intersection(Geometry, Geometry) intersection(Geometry, Geometry)} */ private static GeometryCollection intersection(GeometryCollection gc1, GeometryCollection gc2) { List<Geometry> ret = new ArrayList<Geometry>(); final int size = gc1.getNumGeometries(); for (int i = 0; i < size; i++) { Geometry g1 = gc1.getGeometryN(i); List<Geometry> partial = intersection(gc2, g1); ret.addAll(partial); } return gc1.getFactory().createGeometryCollection(GeometryFactory.toGeometryArray(ret)); }
return gc.getFactory() .createMultiPoint((Point[]) result.toArray(new Point[result.size()])); } else if (gc instanceof MultiLineString) { return gc.getFactory() .createMultiLineString( (LineString[]) result.toArray(new LineString[result.size()])); } else if (gc instanceof MultiPolygon) { return gc.getFactory() .createMultiPolygon((Polygon[]) result.toArray(new Polygon[result.size()])); } else { return gc.getFactory() .createGeometryCollection( (Geometry[]) result.toArray(new Geometry[result.size()]));
Point[] points = new Point[elements.length]; System.arraycopy(elements, 0, points, 0, elements.length); return collection.getFactory().createMultiPoint(points); } else if (elementType == LineString.class) { LineString[] lines = new LineString[elements.length]; System.arraycopy(elements, 0, lines, 0, elements.length); return collection.getFactory().createMultiLineString(lines); } else if (elementType == Polygon.class) { Polygon[] polys = new Polygon[elements.length]; System.arraycopy(elements, 0, polys, 0, elements.length); return collection.getFactory().createMultiPolygon(polys); } else { return collection.getFactory().createGeometryCollection(elements);
/** * Creates a {@link GeometryCollection} with * every component reversed. * The order of the components in the collection are not reversed. * * @return a {@link GeometryCollection} in the reverse order */ public Geometry reverse() { int n = geometries.length; Geometry[] revGeoms = new Geometry[n]; for (int i = 0; i < geometries.length; i++) { revGeoms[i] = geometries[i].reverse(); } return getFactory().createGeometryCollection(revGeoms); } }
private Geometry collectionClip(GeometryCollection geom) throws Exception { ArrayList<Geometry> result = new ArrayList<Geometry>(); for (int t = 0; t < geom.getNumGeometries(); t++) { Geometry g = geom.getGeometryN(0); Geometry clipped = _run(g); // gets the non-degenerative of the result if ((clipped != null) && (!clipped.isEmpty())) { result.add(clipped); } } if (result.size() == 0) { return null; } return new GeometryCollection( (Geometry[]) result.toArray(new Geometry[result.size()]), geom.getFactory()); }
public GeometryCollection map(GeometryCollection gc) { List mapped = new ArrayList(); for (int i = 0; i < gc.getNumGeometries(); i++) { Geometry g = mapOp.map(gc.getGeometryN(i)); if (!g.isEmpty()) mapped.add(g); } return gc.getFactory().createGeometryCollection( GeometryFactory.toGeometryArray(mapped)); } }
private static MultiLineString mergeTrianglesEdges(GeometryCollection polygons) { GeometryFactory factory = polygons.getFactory(); Set<LineSegment> segments = new HashSet<LineSegment>(polygons.getNumGeometries()); SegmentMerge segmentMerge = new SegmentMerge(segments); for(int idGeom = 0; idGeom < polygons.getNumGeometries(); idGeom++) { Geometry polygonGeom = polygons.getGeometryN(idGeom); if(polygonGeom instanceof Polygon) { Polygon polygon = (Polygon)polygonGeom; segmentMerge.reset(); polygon.getExteriorRing().apply(segmentMerge); } } // Convert segments into multilinestring LineString[] lineStrings = new LineString[segments.size()]; int idLine = 0; for(LineSegment lineSegment : segments) { lineStrings[idLine++] = factory.createLineString(new Coordinate[] {lineSegment.p0, lineSegment.p1}); } segments.clear(); return factory.createMultiLineString(lineStrings); }