private Ring toRing(Polygon polygon, HashMap<Coordinate, OSMNode> nodeMap) { List<OSMNode> shell = new ArrayList<OSMNode>(); for (Coordinate coord : polygon.getExteriorRing().getCoordinates()) { OSMNode node = nodeMap.get(coord); if (node == null) { throw new RingConstructionException(); } shell.add(node); } Ring ring = new Ring(shell, true); // now the holes for (int i = 0; i < polygon.getNumInteriorRing(); ++i) { LineString interior = polygon.getInteriorRingN(i); List<OSMNode> hole = new ArrayList<OSMNode>(); for (Coordinate coord : interior.getCoordinates()) { OSMNode node = nodeMap.get(coord); if (node == null) { throw new RingConstructionException(); } hole.add(node); } ring.holes.add(new Ring(hole, true)); } return ring; }
LineString merge = (LineString)lineMerger.getMergedLineStrings().iterator().next(); if (merge.isRing()) { LinearRing lr = new LinearRing(merge.getCoordinateSequence(), this.geomFactory); Polygon concaveHull = new Polygon(lr, null, this.geomFactory); return concaveHull;
@Override public Coordinate[] getCoordinates() { com.vividsolutions.jts.geom.Coordinate[] coordinates = super.getCoordinates(); Coordinate[] cs = new Coordinate[coordinates.length]; int count = 0; for (com.vividsolutions.jts.geom.Coordinate c : coordinates) { cs[count++] = new Coordinate(c); } return cs; } }
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; }
holes.add(ring); else shells.add(geomFactory.createPolygon(ring)); shell.setUserData(new ArrayList<LinearRing>()); if (shell.contains(hole)) { ((List<LinearRing>) shell.getUserData()).add(hole); break outer; List<LinearRing> shellHoles = ((List<LinearRing>) shell.getUserData()); punched.add(geomFactory.createPolygon((LinearRing) (shell.getExteriorRing()), shellHoles.toArray(new LinearRing[shellHoles.size()])));
multi = (MultiPolygon) geometry; } else { multi = geometryFactory.createMultiPolygon(new Polygon[] { (Polygon) geometry }); buffer.putDouble(box.getMinX()); buffer.putDouble(box.getMinY()); buffer.putDouble(box.getMaxX()); buffer.putDouble(box.getMaxY()); Polygon p; p = (Polygon) multi.getGeometryN(t); nrings = nrings + 1 + p.getNumInteriorRing(); Polygon p; p = (Polygon) multi.getGeometryN(t); pointsPerRing[u] = p.getExteriorRing().getNumPoints(); u++; for (int v = 0; v < p.getNumInteriorRing(); v++) { pointsPerRing[u] = p.getInteriorRingN(v).getNumPoints(); u++;
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; }
public Polygon call(T spatialObject) { Double x1, x2, y1, y2; LinearRing linear; Coordinate[] coordinates = new Coordinate[5]; GeometryFactory fact = new GeometryFactory(); final Envelope envelope = spatialObject.getEnvelopeInternal(); x1 = envelope.getMinX(); x2 = envelope.getMaxX(); y1 = envelope.getMinY(); y2 = envelope.getMaxY(); coordinates[0] = new Coordinate(x1, y1); coordinates[1] = new Coordinate(x1, y2); coordinates[2] = new Coordinate(x2, y2); coordinates[3] = new Coordinate(x2, y1); coordinates[4] = coordinates[0]; linear = fact.createLinearRing(coordinates); Polygon polygonObject = new Polygon(linear, null, fact); return polygonObject; } });
GeometryFactory geometryfactory = new GeometryFactory(); ArrayList<Coordinate> coordinatesList = new ArrayList<Coordinate>(); LinearRing linear; for (int i = 0; i < spatialObject.getCoordinates().length; i++) { Tuple2<Integer, Integer> pixelCoordinate = null; pixelCoordinate = FindOnePixelCoordinate(resolutionX, resolutionY, datasetBoundary, spatialObject.getCoordinates()[i], reverseSpatialCoordinate); coordinatesList.add(new Coordinate(pixelCoordinate._1, pixelCoordinate._2)); linear = geometryfactory.createLinearRing(coordinatesList.toArray(new Coordinate[coordinatesList.size()])); Polygon polygon = new Polygon(linear, null, geometryfactory); int minPixelX = (int) polygon.getEnvelopeInternal().getMinX(); int maxPixelX = (int) polygon.getEnvelopeInternal().getMaxX(); int minPixelY = (int) polygon.getEnvelopeInternal().getMinY(); int maxPixelY = (int) polygon.getEnvelopeInternal().getMaxY(); for (int j = minPixelY; j <= maxPixelY; j++) { for (int i = minPixelX; i <= maxPixelX; i++) { if (polygon.contains(geometryfactory.createPoint(new Coordinate(i, j)))) { try { Pixel newPixel = new Pixel(i, j, resolutionX, resolutionY);
GeometryFactory geoFactory = new GeometryFactory(); double x = envelope.getMinX() - edgeBuffer; double y = envelope.getMinY() - edgeBuffer; double w = envelope.getWidth() + edgeBuffer * 2; double h = envelope.getHeight() + edgeBuffer * 2; for (int i = 0; i < geometry.getNumGeometries(); i++) { com.vividsolutions.jts.geom.Polygon poly = (com.vividsolutions.jts.geom.Polygon) geometry .getGeometryN(i); LinearRing lr = geoFactory.createLinearRing(poly.getExteriorRing().getCoordinates()); com.vividsolutions.jts.geom.Polygon part = geoFactory.createPolygon(lr, null); drawGeometry(part, bounds, graphics, width, height, flags); for (int j = 0; j < poly.getNumInteriorRing(); j++) { lr = geoFactory.createLinearRing(poly.getInteriorRingN(j).getCoordinates()); part = geoFactory.createPolygon(lr, null); drawGeometry(part, bounds, graphics, width, height, flags);
/** * Returns the polygon surrounding the specified rectangle. * Code lifted from ArcGridDataSource (temporary). */ public static Polygon getPolygon(final Rectangle2D rect, final int srid) { final PrecisionModel pm = new PrecisionModel(); final GeometryFactory gf = new GeometryFactory(pm, srid); final Coordinate[] coord = new Coordinate[] { new Coordinate(rect.getMinX(), rect.getMinY()), new Coordinate(rect.getMaxX(), rect.getMinY()), new Coordinate(rect.getMaxX(), rect.getMaxY()), new Coordinate(rect.getMinX(), rect.getMaxY()), new Coordinate(rect.getMinX(), rect.getMinY()) }; final LinearRing ring = gf.createLinearRing(coord); return new Polygon(ring, null, gf); }
/** * Removes duplicated coordinates within a Polygon. * @param poly * @return */ public static Polygon removeDuplicateCoordinates(Polygon poly) { Coordinate[] shellCoords = CoordinateArrays.removeRepeatedPoints(poly.getExteriorRing().getCoordinates()); LinearRing shell = FACTORY.createLinearRing(shellCoords); ArrayList<LinearRing> holes = new ArrayList<LinearRing>(); for (int i = 0; i < poly.getNumInteriorRing(); i++) { Coordinate[] holeCoords = CoordinateArrays.removeRepeatedPoints(poly.getInteriorRingN(i).getCoordinates()); holes.add(FACTORY.createLinearRing(holeCoords)); } return FACTORY.createPolygon(shell, GeometryFactory.toLinearRingArray(holes)); }
centroid = geom.getCentroid(); } catch (Exception e) { centroid = geom.getExteriorRing().getCentroid(); } catch (Exception ee) { try { centroid = geom.getFactory().createPoint(geom.getCoordinate()); } catch (Exception eee) { return false; // we're hooped Envelope env = geom.getEnvelopeInternal(); double step = 5; int steps = (int) Math.round((env.getMaxX() - env.getMinX()) / step); Coordinate c = new Coordinate(); Point pp = gf.createPoint(c); c.y = centroid.getY(); int max = -1; int maxIdx = -1; int containCounter = -1; for (int i = 0; i < steps; i++) { c.x = env.getMinX() + step * i; pp.geometryChanged(); if(!pg.contains(pp)) { containCounter = 0; pp.geometryChanged(); centroid = pp; } else {
/** * Reverse the polygon to be oriented clockwise * @param polygon * @return */ private static Polygon extractFloor(final Polygon polygon) { GeometryFactory factory = polygon.getFactory(); final LinearRing shell = factory.createLinearRing(getClockWise( polygon.getExteriorRing()).getCoordinates()); final int nbOfHoles = polygon.getNumInteriorRing(); final LinearRing[] holes = new LinearRing[nbOfHoles]; for (int i = 0; i < nbOfHoles; i++) { holes[i] = factory.createLinearRing(getCounterClockWise( polygon.getInteriorRingN(i)).getCoordinates()); } return factory.createPolygon(shell, holes); }
throws Exception GeometryFactory geometryFactory = new GeometryFactory(); List<Tuple2<Object, Double>> result = new ArrayList<Tuple2<Object, Double>>(); if (spatialObject instanceof Point) { if (!datasetBoundary.covers(((Point) spatialObject).getEnvelopeInternal())) { return result.iterator(); } Coordinate coordinate = RasterizationUtils.FindPixelCoordinates(resolutionX, resolutionY, datasetBoundary, ((Point) spatialObject).getCoordinate(), reverseSpatialCoordinate, false, true); Point rasterizedObject = geometryFactory.createPoint(coordinate); if (colorizeOption == ColorizeOption.EARTHOBSERVATION) { result.add(new Tuple2<Object, Double>(rasterizedObject, ((Point) spatialObject).getCoordinate().z)); if (!datasetBoundary.covers(((Polygon) spatialObject).getEnvelopeInternal())) { return result.iterator(); } Coordinate[] spatialCoordinates = RasterizationUtils.FindPixelCoordinates(resolutionX, resolutionY, datasetBoundary, ((Polygon) spatialObject).getCoordinates(), reverseSpatialCoordinate, false, true); result.add(new Tuple2<Object, Double>(geometryFactory.createPolygon(spatialCoordinates), new Double(1.0))); return result.iterator(); if (!datasetBoundary.covers(((LineString) spatialObject).getEnvelopeInternal())) { return result.iterator(); } Coordinate[] spatialCoordinates = RasterizationUtils.FindPixelCoordinates(resolutionX, resolutionY, datasetBoundary, ((LineString) spatialObject).getCoordinates(), reverseSpatialCoordinate, false, true); result.add(new Tuple2<Object, Double>(geometryFactory.createLineString(spatialCoordinates), new Double(1.0))); return result.iterator();
if (getNumInteriorRing() != 0) return false; if (shell == null) return false; if (shell.getNumPoints() != 5) return false; Envelope env = getEnvelopeInternal(); for (int i = 0; i < 5; i++) { double x = seq.getX(i); if (! (x == env.getMinX() || x == env.getMaxX())) return false; double y = seq.getY(i); if (! (y == env.getMinY() || y == env.getMaxY())) return false;
final int glType; if (geo instanceof Polygon) { ls = ((Polygon) geo).getExteriorRing(); glType = GL2.GL_POLYGON; } else if (geo instanceof MultiPolygon) { ls = ((Polygon) geo.getGeometryN(0)).getExteriorRing(); glType = GL2.GL_POLYGON; } else if (geo instanceof LineString) { glType = GL2.GL_LINE_STRIP; } else if (geo instanceof MultiLineString) { ls = (LineString) geo.getGeometryN(0); glType = GL2.GL_LINE_STRIP; } else if (geo instanceof Point) { final GeometryFactory geofac = new GeometryFactory(); ls = geofac.createLineString(new Coordinate[]{geo.getCoordinate()}); glType = GL.GL_POINTS; } else { throw new RuntimeException("Could not read Geometry from Feature!!"); final int npoints = ls.getNumPoints(); final float [] xpoints = new float[npoints]; final float [] ypoints = new float[npoints]; for (int i = 0; i < npoints; i++) { xpoints[i] = (float) (ls.getPointN(i).getCoordinate().x); ypoints[i] = (float) (ls.getPointN(i).getCoordinate().y);
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); }
private boolean intersects(Polygon polygon) { if (intersects(polygon.getExteriorRing())) { return true; } if (polygon.contains(factory.createPoint(centerPoint))) { return true; } if (polygon.getNumInteriorRing() == 0) { return false; } for (int i = 0; i < polygon.getNumInteriorRing(); i++) { if (intersects(polygon.getInteriorRingN(i))) { return true; } } return false; }