for (int i = 0; i < geom.getNumGeometries(); ++i) { mvtGeom.addAll(linesToGeomCmds(geom.getGeometryN(i), mvtClosePath, cursor, 1)); for(int i = 0; i < geom.getNumGeometries(); ++i) { final Polygon nextPoly = (Polygon) geom.getGeometryN(i); final LineString exteriorRing = nextPoly.getExteriorRing(); final double exteriorArea = CGAlgorithms.signedArea(exteriorRing.getCoordinates()); if(((int) Math.round(exteriorArea)) == 0) { continue; CoordinateArrays.reverse(exteriorRing.getCoordinates()); for(int ringIndex = 0; ringIndex < nextPoly.getNumInteriorRing(); ++ringIndex) { final LineString nextInteriorRing = nextPoly.getInteriorRingN(ringIndex); final double interiorArea = CGAlgorithms.signedArea(nextInteriorRing.getCoordinates()); if(((int)Math.round(interiorArea)) == 0) { continue; CoordinateArrays.reverse(nextInteriorRing.getCoordinates());
@Override public int hashCode() { CoordinateSequence cs = ls.getCoordinateSequence(); int maxIdx = cs.size() - 1; int x = (int) (cs.getX(0) * 1000000) + (int) (cs.getX(maxIdx) * 1000000); int y = (int) (cs.getY(0) * 1000000) + (int) (cs.getY(maxIdx) * 1000000); return x + y * 101149 + maxIdx * 7883; }
private static FeatureStats polyStats(Geometry geom) { final FeatureStats featureStats = new FeatureStats(); for(int i = 0; i < geom.getNumGeometries(); ++i) { final Polygon nextPoly = (Polygon) geom.getGeometryN(i); // Stats: exterior ring final LineString exteriorRing = nextPoly.getExteriorRing(); featureStats.totalPts += exteriorRing.getNumPoints(); featureStats.repeatedPts += checkRepeatedPoints2d(exteriorRing); // Stats: interior rings for(int ringIndex = 0; ringIndex < nextPoly.getNumInteriorRing(); ++ringIndex) { final LineString nextInteriorRing = nextPoly.getInteriorRingN(ringIndex); featureStats.totalPts += nextInteriorRing.getNumPoints(); featureStats.repeatedPts += checkRepeatedPoints2d(nextInteriorRing); } } return featureStats; }
/** project this linestring to an equirectangular projection */ private static LineString equirectangularProject(LineString geometry, double xscale) { Coordinate[] coords = new Coordinate[geometry.getNumPoints()]; for (int i = 0; i < coords.length; i++) { Coordinate c = geometry.getCoordinateN(i); c = (Coordinate) c.clone(); c.x *= xscale; coords[i] = c; } return geometryFactory.createLineString(coords); }
/** * Gets the second {@link Coordinate} of the current segment. (the coordinate of the next * vertex). If the iterator is at the end of a line, <code>null</code> is returned. * * @return a {@link Coordinate} or <code>null</code> */ public Coordinate getSegmentEnd() { if (vertexIndex < getLine().getNumPoints() - 1) return currentLine.getCoordinateN(vertexIndex + 1); return null; }
/** * Computes the angle of the first segment of a LineString or MultiLineString in radians clockwise from North * in the range (-PI, PI). * @param geometry a LineString or a MultiLineString */ public static synchronized double getFirstAngle(Geometry geometry) { LineString line; if (geometry instanceof MultiLineString) { line = (LineString) geometry.getGeometryN(0); } else { assert geometry instanceof LineString; line = (LineString) geometry; } Coordinate coord0 = line.getCoordinateN(0); Coordinate coord1 = line.getCoordinateN(1); int i = 2; int minDistance = 10; // Meters while (SphericalDistanceLibrary.fastDistance(coord0, coord1) < minDistance && i < line.getNumPoints()) { coord1 = line.getCoordinateN(i++); } double az = getAzimuth(coord0, coord1); return az * Math.PI / 180; }
private Collection<Geometry> restoreDim4(Collection<Geometry> geoms, Map<Coordinate, Double> map) { GeometryFactory factory = new GeometryFactory( new PackedCoordinateSequenceFactory(PackedCoordinateSequenceFactory.DOUBLE, 4)); Collection<Geometry> result = new ArrayList<>(); for (Geometry geom : geoms) { if (geom instanceof Point) { result.add(factory.createPoint(restoreDim4( ((Point) geom).getCoordinateSequence(), map))); } else if (geom instanceof LineString) { result.add(factory.createLineString(restoreDim4( ((LineString) geom).getCoordinateSequence(), map))); } else if (geom instanceof Polygon) { LinearRing outer = factory.createLinearRing(restoreDim4( ((Polygon) geom).getExteriorRing().getCoordinateSequence(), map)); LinearRing[] inner = new LinearRing[((Polygon) geom).getNumInteriorRing()]; for (int i = 0; i < ((Polygon) geom).getNumInteriorRing(); i++) { inner[i] = factory.createLinearRing(restoreDim4( ((Polygon) geom).getInteriorRingN(i).getCoordinateSequence(), map)); } result.add(factory.createPolygon(outer, inner)); } else { for (int i = 0; i < geom.getNumGeometries(); i++) { result.addAll(restoreDim4(Collections.singleton(geom.getGeometryN(i)), map)); } } } return result; }
GeometryFactory gFac=new GeometryFactory(geom.getPrecisionModel(),geom.getSRID()); if (spanx == -1) return geom; Geometry[] result=new Geometry[collection.getDimension()]; final int numGeometries = collection.getNumGeometries(); for (int i = 0; i < numGeometries; i++) { result[i]=decimate(collection.getGeometryN(i)); return gFac.createGeometryCollection(result); .getCoordinateSequence(); LiteCoordinateSequence lseq=new LiteCoordinateSequence(seq.toCoordinateArray()); return gFac.createLineString(lseq); final int numRings = line.getNumInteriorRing(); List<LinearRing> rings=new ArrayList<LinearRing>(); Coordinate[] interior=decimate(line.getInteriorRingN(i)).getCoordinates(); forceClosed(interior); if(interior.length>3)
private static LineString reverse(LineString line) { Coordinate[] pts = line.getCoordinates(); Coordinate[] revPts = new Coordinate[pts.length]; int len = pts.length; for (int i = 0; i < len; i++) { revPts[len - 1 - i] = new Coordinate(pts[i]); } return line.getFactory().createLineString(revPts); }
LineString line = geometryFactory.createLineString(new Coordinate[] { from.getCoordinate(), to.getCoordinate() }); area.getPermission(), false, this); forward.setStreetClass(area.getStreetClass()); AreaEdge backward = new AreaEdge(to, from, (LineString) line.reverse(), area.getRawName(), length, area.getPermission(), true, this); backward.setStreetClass(area.getStreetClass()); Point startPoint = geometryFactory.createPoint(startCoordinate); for (NamedArea area : intersects) { Geometry polygon = area.getPolygon(); if (!polygon.intersects(startPoint)) continue; Geometry lineParts = line.intersection(polygon); if (lineParts.getLength() > 0.000001) { Coordinate edgeCoordinate = null; for (int i = 0; i < mls.getNumGeometries(); ++i) { LineString segment = (LineString) mls.getGeometryN(i); if (segment.contains(startPoint) || segment.getBoundary().contains(startPoint)) { edgeCoordinate = segment.getEndPoint().getCoordinate(); edgeCoordinate = ((LineString) lineParts).getEndPoint().getCoordinate(); } else { continue;
/** * 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)); } } }
private void gatherDim4(Geometry geometry, Map<Coordinate, Double> map) { if (geometry instanceof Point) { gatherDim4(((Point) geometry).getCoordinateSequence(), map); } else if (geometry instanceof LineString) { gatherDim4(((LineString) geometry).getCoordinateSequence(), map); } else if (geometry instanceof Polygon) { Polygon polygon = (Polygon) geometry; gatherDim4(polygon.getExteriorRing().getCoordinateSequence(), map); for (int i = 0; i < polygon.getNumInteriorRing(); i++) { gatherDim4(polygon.getInteriorRingN(i).getCoordinateSequence(), map); } } else { for (int i = 0; i < geometry.getNumGeometries(); i++) { gatherDim4(geometry.getGeometryN(i), map); } } }
/** * Remove holes from a polygon * * @param polygon */ private Polygon simplifyPoly(Polygon polygon) { if(polygon.getNumInteriorRing() == 0) return polygon; LineString outer = polygon.getExteriorRing(); if (outer.getStartPoint().distance(outer.getEndPoint()) != 0) { List clist = new ArrayList(Arrays.asList(outer.getCoordinates())); clist.add(outer.getStartPoint().getCoordinate()); outer = outer.getFactory().createLinearRing( (Coordinate[]) clist.toArray(new Coordinate[clist.size()])); } LinearRing r = (LinearRing) outer; return outer.getFactory().createPolygon(r, null); }
CoordinateSequence sequence = lineString.getCoordinateSequence(); CoordinateSequence sequenceWithoutDuplicates = makeSequenceValid(sequence, false, false); GeometryFactory factory = lineString.getFactory(); if (sequenceWithoutDuplicates.size() == 0) { return factory.createLineString(factory.getCoordinateSequenceFactory().create(0, sequence.getDimension())); } else if (sequenceWithoutDuplicates.size() == 1) { return factory.createLineString(factory.getCoordinateSequenceFactory().create(0, sequence.getDimension())); } else { return factory.createPoint(sequenceWithoutDuplicates);
}); GeometryFactory fact = new GeometryFactory( ExtendedCoordinateSequenceFactory.instance()); Geometry g1 = fact.createPolygon(fact.createLinearRing(seq1), null); Geometry g2 = fact.createPolygon(fact.createLinearRing(seq2), null); System.out.println("Internal rep for g1: " + ((Polygon) g1).getExteriorRing().getCoordinateSequence()); System.out.println("Internal rep for g2: " + ((Polygon) g2).getExteriorRing().getCoordinateSequence()); Geometry gInt = g1.intersection(g2); System.out.println("Internal rep for gInt: " + ((Polygon) gInt).getExteriorRing().getCoordinateSequence());
if(geom.isValid()){ geom.normalize(); // validate does not pick up rings in the wrong order - this will fix that return geom; // If the polygon is valid just return it return toPolygonGeometry(polygonizer.getPolygons(), geom.getFactory()); }else if(geom instanceof MultiPolygon){ if(geom.isValid()){ addLineString(polygon.getExteriorRing(), polygonizer); for(int n = polygon.getNumInteriorRing(); n-- > 0;){ addLineString(polygon.getInteriorRingN(n), polygonizer); lineString = lineString.getFactory().createLineString(lineString.getCoordinateSequence()); Point point = lineString.getFactory().createPoint(lineString.getCoordinateN(0)); Geometry toAdd = lineString.union(point);
/** * Removes duplicated coordinates within a Polygon. * * @param poly * @return */ public static Polygon removeCoordinates(Polygon poly) { Coordinate[] shellCoords = CoordinateUtils.removeDuplicatedCoordinates(poly.getExteriorRing().getCoordinates(),true); LinearRing shell = FACTORY.createLinearRing(shellCoords); ArrayList<LinearRing> holes = new ArrayList<LinearRing>(); for (int i = 0; i < poly.getNumInteriorRing(); i++) { Coordinate[] holeCoords = CoordinateUtils.removeDuplicatedCoordinates(poly.getInteriorRingN(i).getCoordinates(),true); holes.add(FACTORY.createLinearRing(holeCoords)); } return FACTORY.createPolygon(shell, GeometryFactory.toLinearRingArray(holes)); }
/** * @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(); }
private static void resolveLineStringSegments(LineString geom, List<Geometry> result) { for (int i = 0; i < geom.getCoordinateSequence().size() - 1; i++) { result.add(geom.getFactory().createLineString(new Coordinate[] { geom.getCoordinateSequence().getCoordinate(i), geom.getCoordinateSequence().getCoordinate(i+1) })); } }
for (int i = 0 ; i < shape.getNumPoints() - 1; ++i) { segments.add(new IndexedLineSegment(i, shape.getCoordinateN(i), shape.getCoordinateN(i + 1))); for (int i = 0; i < stopTimes.size() ; ++i) { Stop stop = stopTimes.get(i).getStop(); Coordinate coord = new Coordinate(stop.getLon(), stop.getLat()); List<IndexedLineSegment> stopSegments = new ArrayList<IndexedLineSegment>(); double bestDistance = Double.MAX_VALUE; Coordinate from = shape.getCoordinateN(j); Coordinate to = shape.getCoordinateN(j + 1); double xd = from.x - to.x; double yd = from.y - to.y; Coordinate from = shape.getCoordinateN(j); Coordinate to = shape.getCoordinateN(j + 1); double xd = from.x - to.x; double yd = from.y - to.y; .getCoordinates(), 2); geometry = _geometryFactory.createLineString(sequence);