Refine search
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 GeometryCollection geometryCollectionFromVertices(Graph graph) { GeometryFactory gf = GeometryUtils.getGeometryFactory(); Collection<Vertex> vertices = graph.getVertices(); Geometry[] points = new Geometry[vertices.size()]; int i = 0; for (Vertex v : vertices) { points[i++] = gf.createPoint(v.getCoordinate()); } GeometryCollection geometries = new GeometryCollection(points, gf); return geometries; }
/** * Returns a {@link Geometry} that represents the concave hull of the input * geometry according to the threshold. * The returned geometry contains the minimal number of points needed to * represent the concave hull. * * @return if the concave hull contains 3 or more points, a {@link Polygon}; * 2 points, a {@link LineString}; * 1 point, a {@link Point}; * 0 points, an empty {@link GeometryCollection}. */ public Geometry getConcaveHull() { if (this.geometries.getNumGeometries() == 0) { return this.geomFactory.createGeometryCollection(null); } if (this.geometries.getNumGeometries() == 1) { return this.geometries.getGeometryN(0); } if (this.geometries.getNumGeometries() == 2) { return this.geomFactory.createLineString(this.geometries.getCoordinates()); } return concaveHull(); }
/** * Transform into GeometryCollection. * * @param geom * input geometry * @return * a geometry collection */ private static GeometryCollection transformIntoPointGeometryCollection(GeometryCollection gc) { UniqueCoordinateArrayFilter filter = new UniqueCoordinateArrayFilter(); gc.apply(filter); Coordinate[] coord = filter.getCoordinates(); Geometry[] geometries = new Geometry[coord.length]; for (int i = 0 ; i < coord.length ; i++) { Coordinate[] c = new Coordinate[] { coord[i] }; CoordinateArraySequence cs = new CoordinateArraySequence(c); geometries[i] = new Point(cs, gc.getFactory()); } return new GeometryCollection(geometries, gc.getFactory()); }
Geometry u = geometryFactory.createMultiPolygon(allRings.toArray(new Polygon[allRings .size()])); u = u.union(); for (int i = 0; i < mp.getNumGeometries(); ++i) { Geometry poly = mp.getGeometryN(i); if (!(poly instanceof Polygon)) { LOG.warn("Unexpected non-polygon when merging areas: " + poly);
.getCoordinates())); } else if (geom instanceof MultiLineString) { MultiLineString multiLineString = (MultiLineString) geom; } else if (geom instanceof GeometryCollection) { GeometryCollection collection = (GeometryCollection) geom; Geometry[] inverted = new Geometry[collection.getNumGeometries()]; for (int count = 0; count < collection.getNumGeometries(); count++) { inverted[count] = invertGeometryCoordinates(collection .getGeometryN(count)); + geom.getGeometryType());
public final void decimateTransformGeneralize(Geometry geometry, MathTransform transform) throws TransformException { if (geometry instanceof GeometryCollection) { GeometryCollection collection = (GeometryCollection) geometry; final int length = collection.getNumGeometries(); for (int i = 0; i < length; i++) { decimateTransformGeneralize(collection.getGeometryN(i), transform); } } else if (geometry instanceof Point) { LiteCoordinateSequence seq = (LiteCoordinateSequence) ((Point) geometry) .getCoordinateSequence(); decimateTransformGeneralize(seq, transform); } else if (geometry instanceof Polygon) { Polygon polygon = (Polygon) geometry; decimateTransformGeneralize(polygon.getExteriorRing(), transform); final int length = polygon.getNumInteriorRing(); for (int i = 0; i < length; i++) { decimateTransformGeneralize(polygon.getInteriorRingN(i), transform); } } else if (geometry instanceof LineString) { LiteCoordinateSequence seq = (LiteCoordinateSequence) ((LineString) geometry) .getCoordinateSequence(); decimateTransformGeneralize(seq, transform); } }
if (geom instanceof GeometryCollection) { GeometryCollection geom_coll = (GeometryCollection) geom; for (int i = 0; i < geom_coll.getNumGeometries(); i++) { Geometry sub_geom = geom_coll.getGeometryN(i); com.vividsolutions.jts.geom.Polygon poly = (com.vividsolutions.jts.geom.Polygon) geom; for (int i = 0; i < poly.getNumInteriorRing(); i++) { LineString ring = poly.getInteriorRingN(i); drawJTSGeom(g, ring, xscale, yscale, fill); drawJTSGeom(g, poly.getExteriorRing(), xscale, yscale, fill); } else if (geom instanceof LineString) { LineString line = (LineString) geom; double geom_alpha = line.getLength() * (xscale + yscale) / 2.0; int color_alpha = geom_alpha > 1.0 ? 255 : (int) Math.round(geom_alpha * 255); if (color_alpha == 0) return; int[] xpoints = new int[line.getNumPoints()]; int[] ypoints = new int[line.getNumPoints()]; int n = 0; double px = line.getPointN(i).getX(); double py = line.getPointN(i).getY();
/** * Adds the linear components of by a Geometry to the centroid total. * If the geometry has no linear components it does not contribute to the centroid, * * @param geom the geometry to add */ public void add(Geometry geom) { if (geom instanceof LineString) { add(geom.getCoordinates()); } else if (geom instanceof Polygon) { Polygon poly = (Polygon) geom; // add linear components of a polygon add(poly.getExteriorRing().getCoordinates()); for (int i = 0; i < poly.getNumInteriorRing(); i++) { add(poly.getInteriorRingN(i).getCoordinates()); } } else if (geom instanceof GeometryCollection) { GeometryCollection gc = (GeometryCollection) geom; for (int i = 0; i < gc.getNumGeometries(); i++) { add(gc.getGeometryN(i)); } } }
for (int i = 0; i < collectionForType.getNumGeometries(); i++) { Geometry geometry = edit(collectionForType.getGeometryN(i), operation); if (geometry == null || geometry.isEmpty()) { continue; return factory.createMultiPoint((Point[]) geometries.toArray( new Point[] { })); return factory.createMultiLineString((LineString[]) geometries.toArray( new LineString[] { })); return factory.createMultiPolygon((Polygon[]) geometries.toArray( new Polygon[] { }));
str.append("<Point>"); str.append("<coordinates>"); str.append(pt.getX()+","+pt.getY()); str.append("</coordinates>"); str.append("</Point>"); str.append("<LineString>"); str.append("<coordinates>"); for (Coordinate coord : linestring.getCoordinates()) str.append(coord.x+","+coord.y+" "); str.append("</coordinates>"); geometryToKML(str, polygon.getExteriorRing()); str.append("</outerBoundaryIs>"); for (int n = 0; n < polygon.getNumInteriorRing(); n++) { str.append("<innerBoundaryIs>"); geometryToKML(str, polygon.getInteriorRingN(n)); str.append("</innerBoundaryIs>"); GeometryCollection geomCollection = (GeometryCollection) geom; str.append("<GeometryCollection>"); for (int n = 0; n < geomCollection.getNumGeometries(); n++) { geometryToKML(str, geomCollection.getGeometryN(n));
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); }
@Override public boolean covers(Geometry other) { // short-circuit test Envelope otherEnvelope = other.getEnvelopeInternal(); if (!getEnvelopeInternal().covers(otherEnvelope)) { return false; } if (other instanceof Point) { return covers((Point) other); } if (other instanceof LineString) { return covers((LineString) other); } if (other instanceof Polygon) { return covers(((Polygon) other).getExteriorRing()); } if (other instanceof GeometryCollection) { GeometryCollection collection = (GeometryCollection) other; for (int i = 0; i < collection.getNumGeometries(); i++) { if (!covers(collection.getGeometryN(i))) { return false; } } return true; } throw new IllegalArgumentException("Circle.covers() doesn't support geometry type " + other.getGeometryType()); }
/** * Constructs a LINESTRING from the given collection of POINTs and/or * MULTIPOINTs * * @param points Points * @return The LINESTRING constructed from the given collection of POINTs * and/or MULTIPOINTs * @throws SQLException */ public static LineString createLine(GeometryCollection points) throws SQLException { if(points == null) { return null; } final int size = points.getNumGeometries(); if (!atLeastTwoPoints(points)) { throw new SQLException("At least two points are required to make a line."); } List<Coordinate> coordinateList = new LinkedList<Coordinate>(); for (int i = 0; i < size; i++) { coordinateList.addAll(Arrays.asList(points.getGeometryN(i).getCoordinates())); } return points.getGeometryN(0).getFactory().createLineString( coordinateList.toArray(new Coordinate[size])); }
/** * @param distanceTolerance * @return */ private Coordinate[] createConvexHull( RenderableQualityModel rqm ) { // get 2D projection of 3D object required for calculating convex hull Coordinate[] coordinates = projectToPlane( rqm ); // calculate convex hull using JTS ConvexHull ch = new ConvexHull( coordinates, new com.vividsolutions.jts.geom.GeometryFactory() ); Geometry dp = DouglasPeuckerSimplifier.simplify( ch.getConvexHull(), 0.1 ); if ( dp instanceof Polygon ) { return ( (Polygon) dp ).getExteriorRing().getCoordinates(); } else if ( dp instanceof GeometryCollection ) { GeometryCollection collection = (GeometryCollection) dp; List<Coordinate> allCoordinates = new LinkedList<Coordinate>(); for ( int i = 0; i < collection.getNumGeometries(); ++i ) { Geometry geom = collection.getGeometryN( i ); if ( geom != null ) { if ( geom instanceof Polygon ) { Coordinate[] pC = ( (Polygon) geom ).getExteriorRing().getCoordinates(); allCoordinates.addAll( Arrays.asList( pC ) ); } } } return allCoordinates.toArray( new Coordinate[0] ); } return dp.getCoordinates(); }
/** * Creates a similar {@link GeometryCollection} as in setup() but have * triplicates of points 7 & 8. * @return */ private GeometryCollection setupWithDuplicates(){ GeometryFactory gf = new GeometryFactory(); Geometry[] ga = new Geometry[12]; ga[0] = gf.createPoint(new Coordinate(0, 0)); // 1 ga[1] = gf.createPoint(new Coordinate(4, 0)); // 2 ga[2] = gf.createPoint(new Coordinate(0, 4)); // 3 ga[3] = gf.createPoint(new Coordinate(4, 4)); // 4 ga[4] = gf.createPoint(new Coordinate(2, 1)); // 5 ga[5] = gf.createPoint(new Coordinate(3, 2)); // 6 ga[6] = gf.createPoint(new Coordinate(2, 3)); // 7 ga[7] = gf.createPoint(new Coordinate(1, 2)); // 8 ga[8] = gf.createPoint(new Coordinate(2, 3)); // 7 ga[9] = gf.createPoint(new Coordinate(2, 3)); // 7 ga[10] = gf.createPoint(new Coordinate(1, 2)); // 8 ga[11] = gf.createPoint(new Coordinate(1, 2)); // 8 return new GeometryCollection(ga, gf); }
protected static int getGeometrySize(Geometry geom) throws ExecException { if (geom instanceof Point) { return 1; } else if (geom instanceof LineString) { return ((LineString)geom).getNumPoints(); } else if (geom instanceof Polygon) { Polygon poly = (Polygon) geom; int size = 0; size += getGeometrySize(poly.getExteriorRing()) - 1; for (int i = 0; i < poly.getNumInteriorRing(); i++) size += getGeometrySize(poly.getInteriorRingN(i)) - 1; return size; } else if (geom instanceof GeometryCollection) { int size = 0; GeometryCollection coll = (GeometryCollection) geom; for (int i = 0; i < coll.getNumGeometries(); i++) size += getGeometrySize(coll.getGeometryN(i)); return size; } else { throw new GeoException("size() not defined for shapes of type: "+geom.getClass()); } }
/** * Finds the centroid of the input geometry if input = point, line, polygon * --> return a point that represents the centroid of that geom if input = * geometry collection --> return a multipoint that represents the centoid * of each sub-geom * * @param g */ public static Geometry getCentroid(Geometry g) { if (g instanceof GeometryCollection) { final GeometryCollection gc = (GeometryCollection) g; final Coordinate[] pts = new Coordinate[gc.getNumGeometries()]; final int length = gc.getNumGeometries(); for (int t = 0; t < length; t++) { pts[t] = pointInGeometry(gc.getGeometryN(t)).getCoordinate(); } return g.getFactory().createMultiPoint(pts); } else if (g != null) { return pointInGeometry(g); } return null; }
if (gc.getNumGeometries() == 1) { return clip(gc.getGeometryN(0), ensureValid); } else { List<Geometry> result = new ArrayList<Geometry>(gc.getNumGeometries()); for (int i = 0; i < gc.getNumGeometries(); i++) { Geometry clipped = clip(gc.getGeometryN(i), ensureValid); if (clipped != null) { result.add(clipped); 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()]));
List toList(GeometryCollection mgeom) { ArrayList list = new ArrayList(mgeom.getNumGeometries()); for (int i = 0; i < mgeom.getNumGeometries(); i++) { Geometry g = mgeom.getGeometryN(i); if (g instanceof Polygon) { list.add(toList((Polygon)g)); } else if (g instanceof LineString){ list.add(new CoordinateSequenceEncoder(((LineString)g).getCoordinateSequence(), scale)); } else if (g instanceof Point) { list.add(new CoordinateSequenceEncoder(((Point)g).getCoordinateSequence(), scale)); } } return list; } static class CoordinateSequenceEncoder implements JSONAware /*, JSONStreamAware*/ {