/** * JTS 1.14 does not support intersection on a {@link GeometryCollection}. This function works around this * by performing intersection on a flat list of geometry. The resulting list is pre-filtered for invalid * or empty geometry (outside of bounds). Invalid geometry are logged as errors. * * @param envelope non-list geometry defines bounding area * @param dataGeoms geometry pre-passed through {@link #flatFeatureList(Geometry)} * @return list of geometry from {@code data} intersecting with {@code envelope}. */ private static List<Geometry> flatIntersection(Geometry envelope, List<Geometry> dataGeoms) { final List<Geometry> intersectedGeoms = new ArrayList<>(dataGeoms.size()); Geometry nextIntersected; for(Geometry nextGeom : dataGeoms) { try { // AABB intersection culling if(envelope.getEnvelopeInternal().intersects(nextGeom.getEnvelopeInternal())) { nextIntersected = envelope.intersection(nextGeom); if(!nextIntersected.isEmpty()) { nextIntersected.setUserData(nextGeom.getUserData()); intersectedGeoms.add(nextIntersected); } } } catch (TopologyException e) { //LoggerFactory.getLogger(JtsAdapter.class).error(e.getMessage(), e); } } return intersectedGeoms; }
GeometryFactory gf = new GeometryFactory(); Coordinate dropPoint = new Coordinate(lon, lat); pathToStreetCoords[0] = dropPoint; pathToStreetCoords[1] = origin.getCoordinate(); LineString pathToStreet = gf.createLineString(pathToStreetCoords); coords = circleShape.getCoordinates(); geometryJSON.write(gf.createMultiPoint(coords), sw); LOG.debug("done"); } else if (output.equals(SIsochrone.RESULT_TYPE_SHED)) { outputHull = hull.getConcaveHull(); } catch (Exception e) { outputHull = gc.convexHull(); LOG.debug("Could not generate ConcaveHull for WalkShed, using ConvexHull instead.");
public void setGeom(Geometry geom) throws EmptyPolygonException, UnsupportedGeometryException { if (geom instanceof MultiPolygon) { if (geom.isEmpty()) { throw new EmptyPolygonException(); } if (geom.getNumGeometries() > 1) { // LOG.warn("Multiple polygons in MultiPolygon, using only the first."); // TODO percolate this warning up somehow } this.geom = geom.getGeometryN(0); } else if( geom instanceof Point || geom instanceof Polygon){ this.geom = geom; } else { throw new UnsupportedGeometryException( "Non-point, non-polygon Geometry, not supported." ); } // cache a representative point Point point = geom.getCentroid(); this.lat = point.getY(); this.lon = point.getX(); }
private static FeatureStats pointStats(Geometry geom) { final FeatureStats featureStats = new FeatureStats(); final HashSet<Point> pointSet = new HashSet<>(geom.getNumPoints()); featureStats.totalPts = geom.getNumPoints(); for(int i = 0; i < geom.getNumGeometries(); ++i) { final Point p = (Point) geom.getGeometryN(i); featureStats.repeatedPts += pointSet.add(p) ? 0 : 1; } return featureStats; }
private Geometry removeDuplicatePoints(Geometry routeGeometry) { List<Coordinate> coords = new ArrayList<Coordinate>(); Coordinate last = null; for (Coordinate c : routeGeometry.getCoordinates()) { if (!c.equals(last)) { last = c; coords.add(c); } } if (coords.size() < 2) { return null; } Coordinate[] coordArray = new Coordinate[coords.size()]; return routeGeometry.getFactory().createLineString(coords.toArray(coordArray)); }
public List<Coordinate> getCoordinateList(SimpleFeatureCollection inputFeatures) { List<Coordinate> pointList = new ArrayList<Coordinate>(); SimpleFeatureIterator featureIter = inputFeatures.features(); try { while (featureIter.hasNext()) { SimpleFeature feature = featureIter.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); pointList.add(geometry.getCentroid().getCoordinate()); } } finally { featureIter.close(); } return pointList; } }
/** * Computes and returns the area of validity for the given CoordinateReferenceSystem. * <p> * This method returns the prescribed area of validity for the CRS as computed by * {@link CRS#getEnvelope(CoordinateReferenceSystem)} with the exception that the following * {@code EPSG:900913} compatible CRS's return the GeoWebCache prescribed bounds so that they * align with Google Map tiles: {@code EPSG:900913, EPSG:3857, EPSG:3785}. * </p> * * @param coordSys * the CRS to compute the area of validity for * @return the aov for the CRS, or {@code null} if the CRS does not provide such information * (with the exception of EPSG:900913, see above). */ public ReferencedEnvelope getAreaOfValidity(final CoordinateReferenceSystem coordSys) { Geometry aovGeom = getAreaOfValidityAsGeometry(coordSys, gridSetBroker); if (aovGeom == null) { return null; } Envelope envelope = aovGeom.getEnvelopeInternal(); double x1 = envelope.getMinX(); double x2 = envelope.getMaxX(); double y1 = envelope.getMinY(); double y2 = envelope.getMaxY(); ReferencedEnvelope aov = new ReferencedEnvelope(coordSys); aov.init(x1, x2, y1, y2); return aov; }
@Test public void testShapeFileWriter() throws IOException{ String inFile = "src/test/resources/" + utils.getInputDirectory() + "test.shp"; String outFile = utils.getOutputDirectory() + "/test.shp"; SimpleFeatureSource s = ShapeFileReader.readDataFile(inFile); SimpleFeatureCollection fts = s.getFeatures(); SimpleFeatureIterator it = fts.features(); SimpleFeature ft = it.next(); Geometry g = (Geometry) ft.getDefaultGeometry(); List<SimpleFeature> fc = new ArrayList<>(); fc.add(ft); ShapeFileWriter.writeGeometries(fc, outFile); SimpleFeatureSource s1 = ShapeFileReader.readDataFile(outFile); SimpleFeatureCollection fts1 = s1.getFeatures(); SimpleFeatureIterator it1 = fts1.features(); SimpleFeature ft1 = it1.next(); Geometry g1 = (Geometry) ft1.getDefaultGeometry(); Assert.assertEquals(g.getCoordinates().length, g1.getCoordinates().length); }
SimpleFeatureType ft = b.buildFeatureType(); GeometryFactory geofac = new GeometryFactory(); LinearRing lr = geofac.createLinearRing(new Coordinate[]{new Coordinate(0,0),new Coordinate(0,1),new Coordinate(1,1),new Coordinate(0,0)}); Polygon p = geofac.createPolygon(lr,null); MultiPolygon mp = geofac.createMultiPolygon(new Polygon[]{p}); Collection<SimpleFeature> features = new ArrayList<SimpleFeature>(); Geometry g0 = (Geometry) features.iterator().next().getDefaultGeometry(); SimpleFeatureCollection fts1 = s1.getFeatures(); SimpleFeatureIterator it1 = fts1.features(); SimpleFeature ft1 = it1.next(); Geometry g1 = (Geometry) ft1.getDefaultGeometry(); Assert.assertEquals(g0.getCoordinates().length, g1.getCoordinates().length);
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; } });
private void buildSpatialIndex(SimpleFeatureCollection features) { spatialIndex = new STRtree(); SimpleFeatureIterator featureIter = features.features(); try { while (featureIter.hasNext()) { SimpleFeature feature = featureIter.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); NeighborFeature nearFeature = new NeighborFeature(geometry, feature.getID()); spatialIndex.insert(geometry.getEnvelopeInternal(), nearFeature); } } finally { featureIter.close(); } }
/** * DOCUMENT ME! * * @param featureWriter DOCUMENT ME! * @param ft DOCUMENT ME! * * @throws IOException DOCUMENT ME! */ public void startFeature(SVGFeatureWriter featureWriter, SimpleFeature ft) throws IOException { handler.startFeature(featureWriter, ft); Geometry geom = (Geometry) ft.getDefaultGeometry(); Envelope env = geom.getEnvelopeInternal(); write(" bounds=\""); write(env.getMinX()); write(' '); write(env.getMinY()); write(' '); write(env.getMaxX()); write(' '); write(env.getMaxY()); write('\"'); } }
protected LineString horizontalBisector(Geometry geometry) { Envelope envelope = geometry.getEnvelopeInternal(); // Assert: for areas, minx <> maxx double avgY = avg(envelope.getMinY(), envelope.getMaxY()); return factory.createLineString(new Coordinate[] { new Coordinate(envelope.getMinX(), avgY), new Coordinate(envelope.getMaxX(), avgY) }); }
GeometryFactory geomFactory = new GeometryFactory(); Coordinate[] coordinates = new Coordinate[] { edge.getFromVertex().getCoordinate(), edge.getToVertex().getCoordinate() }; edgeGeom = GeometryUtils.getGeometryFactory().createLineString(coordinates); hasGeom = false; OffsetCurveBuilder offsetBuilder = new OffsetCurveBuilder(new PrecisionModel(), bufParams); Coordinate[] coords = offsetBuilder.getOffsetCurve(midLineGeom.getCoordinates(), lineWidth * 0.4); if (coords.length < 2) continue; // Can happen for very small edges (<1mm) LineString offsetLine = geomFactory.createLineString(coords); Shape midLineShape = shapeWriter.toShape(midLineGeom); Shape offsetShape = shapeWriter.toShape(offsetLine); vvAttrs.color = null; vvAttrs.label = null; Point point = geomFactory.createPoint(new Coordinate(vertex.getLon(), vertex.getLat())); boolean render = evRenderer.renderVertex(vertex, vvAttrs); if (!render) context.graphics.draw(shape); if (vvAttrs.label != null && lineWidth > 6.0f && context.bbox.contains(point.getCoordinate())) { context.graphics.setColor(Color.BLACK); int labelWidth = largeFontMetrics.stringWidth(vvAttrs.label);
SimpleFeatureIterator r = features(); try { Envelope newBBox = new Envelope(); Envelope internal; SimpleFeature feature; while (r.hasNext()) { feature = r.next(); final Geometry geom = ((Geometry)feature.getDefaultGeometry()); if(geom != null) { internal = geom.getEnvelopeInternal(); newBBox.expandToInclude(internal); return new ReferencedEnvelope(newBBox, target); } catch (Exception e) { throw new RuntimeException( "Exception occurred while computing reprojected bounds", e); } finally { r.close();
public IndexedFeatureResults(SimpleFeatureCollection results) throws IOException, IllegalAttributeException { // copy results attributes super(null,results.getSchema()); // load features into the index SimpleFeatureIterator reader = null; bounds = new Envelope(); count = 0; try { reader = results.features(); SimpleFeature f; Envelope env; while (reader.hasNext()) { f = reader.next(); env = ((Geometry) f.getDefaultGeometry()).getEnvelopeInternal(); bounds.expandToInclude(env); count++; index.insert(env, f); } } finally { if(reader != null) reader.close(); } }
/** * Get the union. * * @param r the other geometry * @return the union of this geometry envelope and another geometry envelope */ public Value getEnvelopeUnion(ValueGeometry r) { GeometryFactory gf = new GeometryFactory(); Envelope mergedEnvelope = new Envelope(getGeometryNoCopy().getEnvelopeInternal()); mergedEnvelope.expandToInclude(r.getGeometryNoCopy().getEnvelopeInternal()); return get(gf.toGeometry(mergedEnvelope)); }
private static Geometry clipGeometryCollection(Geometry geom, Envelope clipEnv) { Geometry clipPoly = geom.getFactory().toGeometry(clipEnv); List clipped = new ArrayList(); for (int i = 0; i < geom.getNumGeometries(); i++) { Geometry g = geom.getGeometryN(i); Geometry result = null; // don't clip unless necessary if (clipEnv.contains(g.getEnvelopeInternal())) result = g; else if (clipEnv.intersects(g.getEnvelopeInternal())) { result = clipPoly.intersection(g); // keep vertex key info result.setUserData(g.getUserData()); } if (result != null && ! result.isEmpty()) { clipped.add(result); } } return geom.getFactory().createGeometryCollection(GeometryFactory.toGeometryArray(clipped)); } }
GeometryType geometryType = (GeometryType) featureSource.getSchema().getDescriptor(sourceIndex).getType(); GeometryConverter converter = new GeometryConverter(geometryType); SimpleFeatureIterator it = featureSource.getFeatures().features(); while(it.hasNext()) { SimpleFeature feature = it.next(); ResourceId sourceId = ResourceId.valueOf(feature.getID()); ResourceId targetId = idMap.get(sourceId); Geometry geometry = converter.toWgs84(feature.getAttribute(sourceIndex)); System.out.print("Updating geometry for " + targetId + " [" + geometry.getGeometryType() + "] ... ");
Geometry intersection = cropToGeometry.intersection(meg0.getBoundingBox()); ReferencedEnvelope bb0 = StandardSizeGrid.makeBoundingBox(meg0.getBoundingBox().getEnvelopeInternal(), cellSize); ReferencedEnvelope bb1 = StandardSizeGrid.makeBoundingBox(intersection.getEnvelopeInternal(), cellSize); assert bb0.covers(bb1); Point offset = cellSize.getCell(bb0, new Coordinate(bb1.getMinX(), bb1.getMaxY())); Dimension dim0 = cellSize.getDimension(bb0); Dimension dim1 = cellSize.getDimension(bb1); .setValue(String.valueOf(dim1.height)); meg1PM.getAt(MeasurementGrid.PROP_BOUNDING_BOX) .setValue(JTS.toGeometry(bb1).toText()); meg1PM.getAt(MeasurementGrid.PROP_MEASUREMENTS) .setValue(new MeasurementsConverter().toString(val1)); obs1Envelope.expandToInclude(bb1);