/** * 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; }
public Geometry map(Geometry g) { return g.intersection(g2); } });
for (NamedArea area : areas) { Geometry polygon = area.getPolygon(); Geometry intersection = polygon.intersection(line); if (intersection.getLength() > 0.000001) { intersects.add(area);
public void intersection(String wktA, String wktB, PrecisionModel pm) throws ParseException { System.out.println("Running example using Precision Model = " + pm); GeometryFactory fact = new GeometryFactory(pm); WKTReader wktRdr = new WKTReader(fact); Geometry A = wktRdr.read(wktA); Geometry B = wktRdr.read(wktB); Geometry C = A.intersection(B); System.out.println("A intersection B = " + C); }
public static void main(String[] args) throws Exception { // read a geometry from a WKT string (using the default geometry factory) Geometry g1 = new WKTReader().read("LINESTRING (0 0, 10 10, 20 20)"); System.out.println("Geometry 1: " + g1); // create a geometry by specifying the coordinates directly Coordinate[] coordinates = new Coordinate[]{new Coordinate(0, 0), new Coordinate(10, 10), new Coordinate(20, 20)}; // use the default factory, which gives full double-precision Geometry g2 = new GeometryFactory().createLineString(coordinates); System.out.println("Geometry 2: " + g2); // compute the intersection of the two geometries Geometry g3 = g1.intersection(g2); System.out.println("G1 intersection G2: " + g3); } }
/** * Computes the set-theoretic intersection of two {@link Geometry}s, using enhanced precision. * @param geom0 the first Geometry * @param geom1 the second Geometry * @return the Geometry representing the set-theoretic intersection of the input Geometries. */ public Geometry intersection(Geometry geom0, Geometry geom1) { Geometry[] geom = removeCommonBits(geom0, geom1); return computeResultPrecision(geom[0].intersection(geom[1])); }
public void run() throws ParseException { GeometryFactory fact = new GeometryFactory(); WKTReader wktRdr = new WKTReader(fact); String wktA = "POLYGON((40 100, 40 20, 120 20, 120 100, 40 100))"; String wktB = "LINESTRING(20 80, 80 60, 100 140)"; Geometry A = wktRdr.read(wktA); Geometry B = wktRdr.read(wktB); Geometry C = A.intersection(B); System.out.println("A = " + A); System.out.println("B = " + B); System.out.println("A intersection B = " + C); System.out.println("A relate C = " + A.relate(B)); }
public double measure(Geometry g1, Geometry g2) { double areaInt = g1.intersection(g2).getArea(); double areaUnion = g1.union(g2).getArea(); return areaInt / areaUnion; }
void run() throws Exception { String wkt1, wkt2; // two geometries which cause robustness problems wkt1 = "POLYGON ((708653.498611049 2402311.54647056, 708708.895756966 2402203.47250014, 708280.326454234 2402089.6337791, 708247.896591321 2402252.48269854, 708367.379593851 2402324.00761653, 708248.882609455 2402253.07294874, 708249.523621829 2402244.3124463, 708261.854734465 2402182.39086576, 708262.818392579 2402183.35452387, 708653.498611049 2402311.54647056))"; wkt2 = "POLYGON ((708258.754920656 2402197.91172757, 708257.029447455 2402206.56901508, 708652.961095455 2402312.65463437, 708657.068786251 2402304.6356364, 708258.754920656 2402197.91172757))"; Geometry g1 = reader.read(wkt1); Geometry g2 = reader.read(wkt2); System.out.println("This call to intersection will throw a topology exception due to robustness problems:"); try { Geometry result = g1.intersection(g2); } catch (TopologyException ex) { ex.printStackTrace(); } System.out.println("Using EnhancedPrecisionOp allows the intersection to be performed with no errors:"); Geometry result = EnhancedPrecisionOp.intersection(g1, g2); System.out.println(result); }
Geometry result = geom0.intersection(geom1); return result;
System.out.println("Internal rep for g2: " + ((Polygon) g2).getExteriorRing().getCoordinateSequence()); Geometry gInt = g1.intersection(g2);
/** * @param a Geometry instance. * @param b Geometry instance * @return the intersection between two geometries */ public static Geometry intersection(Geometry a,Geometry b) { if(a==null || b==null) { return null; } return a.intersection(b); } }
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)); } }
static Collection<Geometry> cutPolygonsByBoundary(Collection<Polygon> polygons, Polygon boundingPolygon) { Collection<Geometry> cutPolygons = new LinkedList<>(); for (Polygon polygon : polygons) { // Need to determine convex hull of geometry to prevent it from staying self-intersecting Geometry hull = polygon.convexHull(); Geometry intersection = hull.intersection(boundingPolygon); cutPolygons.add(intersection); } return cutPolygons; } }
public void intersection(String wktA, String wktB, PrecisionModel pm) throws ParseException { System.out.println("Running example using Precision Model = " + pm); GeometryFactory fact = new GeometryFactory(pm); WKTReader wktRdr = new WKTReader(fact); Geometry A = wktRdr.read(wktA); Geometry B = wktRdr.read(wktB); Geometry C = A.intersection(B); System.out.println("A intersection B = " + C); }
/** * Helper method for {@link #intersection(Geometry, Geometry) intersection(Geometry, Geometry)} */ private static List<Geometry> intersection(GeometryCollection gc, Geometry g) { List<Geometry> ret = new ArrayList<Geometry>(); final int size=gc.getNumGeometries(); for (int i = 0; i < size; i++) { Geometry g1 = (Geometry)gc.getGeometryN(i); collect(g1.intersection(g), ret); } return ret; }
/** * Returns the intersection of the specified shapes. * * @return the intersection */ @Override public JtsGeometry apply() { Geometry result = shapes.get(0).apply().getGeom(); for (int i = 1; i < shapes.size(); i++) { result = result.intersection(shapes.get(i).apply().getGeom()); } return CONTEXT.makeShape(result); }
@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { return toElement(checkGeo(0, qc).intersection(checkGeo(1, qc)), qc); } }
public double measure(Geometry g1, Geometry g2) { double areaInt = g1.intersection(g2).getArea(); double areaUnion = g1.union(g2).getArea(); return areaInt / areaUnion; }
public static GeometryType intersection(GeometryType geom1, GeometryType geom2) throws FunctionExecutionException { Geometry g1 = getGeometry(geom1); Geometry g2 = getGeometry(geom2); return getGeometryType(g1.intersection(g2), geom1.getSrid()); }