public int size(Shape shape) { switch(getType(shape)) { case LINE: case POLYGON: case MULTIPOINT: case MULTILINESTRING: case MULTIPOLYGON: return ((JtsGeometry) shape).getGeom().getCoordinates().length; case POINT: return 1; case CIRCLE: return 1; case BOX: return 2; case GEOMETRYCOLLECTION: return ((ShapeCollection<?>) shape).getShapes().stream().map(s -> (Shape) s).mapToInt(s -> size(s)).sum(); default: throw new IllegalStateException("size() not supported for type: " + getType(shape)); } }
private boolean testVerticesEquality(Geometry geom1, Geometry geom2) { if ( geom1.getNumPoints() != geom2.getNumPoints() ) { return false; } for ( int i = 0; i < geom1.getNumPoints(); i++ ) { Coordinate cn1 = geom1.getCoordinates()[i]; Coordinate cn2 = geom2.getCoordinates()[i]; if ( !cn1.equals2D( cn2 ) ) { return false; } } return true; } }
private boolean testVerticesEquality(Geometry geom1, Geometry geom2) { if ( geom1.getNumPoints() != geom2.getNumPoints() ) { return false; } for ( int i = 0; i < geom1.getNumPoints(); i++ ) { Coordinate cn1 = geom1.getCoordinates()[i]; Coordinate cn2 = geom2.getCoordinates()[i]; if ( !cn1.equals2D( cn2 ) ) { return false; } } return true; } }
case MULTILINESTRING: case MULTIPOLYGON: Coordinate coordinate = ((JtsGeometry) shape).getGeom().getCoordinates()[position]; return new Geoshape.Point(coordinate.y, coordinate.x); case POINT:
protected boolean testTypeAndVertexEquality(Geometry geom1, Geometry geom2) { if ( !geom1.getGeometryType().equals( geom2.getGeometryType() ) ) { return false; } if ( geom1.getNumGeometries() != geom2.getNumGeometries() ) { return false; } if ( geom1.getNumPoints() != geom2.getNumPoints() ) { return false; } Coordinate[] coordinates1 = geom1.getCoordinates(); Coordinate[] coordinates2 = geom2.getCoordinates(); for ( int i = 0; i < coordinates1.length; i++ ) { Coordinate c1 = coordinates1[i]; Coordinate c2 = coordinates2[i]; if ( !testCoordinateEquality( c1, c2 ) ) { return false; } } return true; }
public Coordinate[] getCoordinates() { return geometry.getCoordinates(); }
private static void writePolyline(Geometry geometry, SliceOutput output, boolean multitype) { int numParts; int numPoints = geometry.getNumPoints(); if (multitype) { numParts = geometry.getNumGeometries(); output.writeByte(GeometrySerializationType.MULTI_LINE_STRING.code()); } else { numParts = numPoints > 0 ? 1 : 0; output.writeByte(GeometrySerializationType.LINE_STRING.code()); } output.writeInt(EsriShapeType.POLYLINE.code); writeEnvelope(geometry, output); output.writeInt(numParts); output.writeInt(numPoints); int partIndex = 0; for (int i = 0; i < numParts; i++) { output.writeInt(partIndex); partIndex += geometry.getGeometryN(i).getNumPoints(); } writeCoordinates(geometry.getCoordinates(), output); }
/** * This method fills the edgeIds hash with edgeIds found inside the specified geometry */ public void fillEdgeIDs(GHIntHashSet edgeIds, Geometry geometry, EdgeFilter filter) { if (geometry instanceof Point) { GHPoint point = GHPoint.create((Point) geometry); findClosestEdgeToPoint(edgeIds, point, filter); } else if (geometry instanceof LineString) { PointList pl = PointList.fromLineString((LineString) geometry); // TODO do map matching or routing int lastIdx = pl.size() - 1; if (pl.size() >= 2) { double meanLat = (pl.getLatitude(0) + pl.getLatitude(lastIdx)) / 2; double meanLon = (pl.getLongitude(0) + pl.getLongitude(lastIdx)) / 2; findClosestEdge(edgeIds, meanLat, meanLon, filter); } } else if (geometry instanceof MultiPoint) { for (Coordinate coordinate : geometry.getCoordinates()) { findClosestEdge(edgeIds, coordinate.y, coordinate.x, filter); } } }
private static List<Integer> toCoordinateList(Geometry jtsGeometry) { Coordinate[] jtsCoords = jtsGeometry.getCoordinates(); ArrayList<Integer> result = new ArrayList<>(); for (int j = 0; j < jtsCoords.length; j++) { LatLong latLong = new LatLong(jtsCoords[j].y, jtsCoords[j].x); result.add(Integer.valueOf(LatLongUtils.degreesToMicrodegrees(latLong.latitude))); result.add(Integer.valueOf(LatLongUtils.degreesToMicrodegrees(latLong.longitude))); } return result; }
Coordinate[] coordinates = geometry.getCoordinates(); canonicalizePolygonCoordinates(coordinates, partIndexes, shellPart); writeCoordinates(coordinates, output);
public ReferencedEnvelope3D get3DEnvelope(Geometry geom) { Coordinate[] coordinates = geom.getCoordinates(); ReferencedEnvelope3D env = new ReferencedEnvelope3D(); for (Coordinate coordinate : coordinates) { env.expandToInclude(coordinate); } return env; }
private double pointToPolygonDist(Point point, MultiPolygon polygon) { boolean inside = false; double minDistSq = Double.POSITIVE_INFINITY; for (int k = 0; k < polygon.getNumGeometries(); k++) { Coordinate[] ring = polygon.getGeometryN(k).getCoordinates(); for (int i = 0, len = ring.length, j = len - 1; i < len; j = i++) { Coordinate a = ring[i]; Coordinate b = ring[j]; if ((a.y > y != b.y > y) && (x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x)) inside = !inside; minDistSq = Math.min(minDistSq, getSegDistSq(x, y, a, b)); } } // Points outside has a negative distance and thus will be weighted down later. return (inside ? 1 : -1) * Math.sqrt(minDistSq); } // get squared distance from a point to a segment
private Geometry adaptGeometry(final Geometry value, Class<? extends Geometry> targetType) { final Class<? extends Geometry> currentClass = value.getClass(); final GeometryFactory factory = value.getFactory(); Geometry adapted; if (MultiPoint.class == targetType && Point.class == currentClass) { adapted = factory.createMultiPoint(value.getCoordinates()); } else if (MultiLineString.class == targetType && LineString.class == currentClass) { adapted = factory.createMultiLineString(new LineString[] {(LineString) value}); } else if (MultiPolygon.class == targetType && Polygon.class == currentClass) { adapted = factory.createMultiPolygon(new Polygon[] {(Polygon) value}); } else { throw new IllegalArgumentException( "Don't know how to adapt " + currentClass.getName() + " to " + targetType.getName()); } return adapted; }
private Geometry buildShiftedGeometry(Geometry g, double shiftX, double shiftY) { Geometry clone = g.copy(); Coordinate[] coords = clone.getCoordinates(); final int length = coords.length; for (int i = 0; i < length; i++) { Coordinate coord = coords[i]; coord.x += shiftX; coord.y += shiftY; } return clone; }
public static Geometry reverseGeometry(Geometry geom, boolean modify) { if (geom instanceof Point) return (geom); if (geom instanceof LineString) { Coordinate[] reversed = reverseCoordinates(geom.getCoordinates(), modify); if (modify) return (geom); else return (gf().createLineString(reversed)); } return (null); }
@Test public void toGeometry_Shape_Poly() { Shape shape = new java.awt.Polygon(XPOINTS, YPOINTS, NPOINTS); Geometry geom = JTS.toGeometry(shape); assertTrue(geom instanceof LinearRing); Coordinate[] coords = geom.getCoordinates(); assertEquals(NPOINTS + 1, coords.length); CoordList list = new CoordList(coords); Coordinate c = new Coordinate(); for (int i = 0; i < NPOINTS; i++) { c.x = XPOINTS[i]; c.y = YPOINTS[i]; assertTrue(list.contains(c)); } }
List<?> toList(Polygon p) { BasicDBList l = new BasicDBList(); if (!CGAlgorithms.isCCW(p.getExteriorRing().getCoordinates())) { l.add(toList(p.getExteriorRing().reverse().getCoordinates())); } else { l.add(toList(p.getExteriorRing().getCoordinateSequence())); } for (int i = 0; i < p.getNumInteriorRing(); i++) { l.add(toList(p.getInteriorRingN(i).getCoordinateSequence())); } return l; } }
@Test public void smoothLineString() { Coordinate[] coords = getLineCoords(); LineString line = factory.createLineString(coords); Geometry smoothed = JTS.smooth(line, 0); assertTrue(smoothed instanceof LineString); CoordList list = new CoordList(smoothed.getCoordinates()); assertTrue(list.containsAll(coords)); Envelope lineEnv = line.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(lineEnv)); }
@Test public void smoothLinearRing() { Coordinate[] coords = getPolyCoords(); LineString line = factory.createLinearRing(coords); Geometry smoothed = JTS.smooth(line, 0); assertTrue(smoothed instanceof LinearRing); CoordList list = new CoordList(smoothed.getCoordinates()); assertTrue(list.containsAll(coords)); Envelope lineEnv = line.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(lineEnv)); }
@Test public void smoothPolygon() { Coordinate[] coords = getPolyCoords(); Polygon poly = factory.createPolygon(factory.createLinearRing(coords), null); Geometry smoothed = JTS.smooth(poly, 0); assertTrue(smoothed instanceof Polygon); CoordList list = new CoordList(smoothed.getCoordinates()); assertTrue(list.containsAll(coords)); Envelope polyEnv = poly.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(polyEnv)); }