Relationship judgment
- The relationship between Geometry is as follows:
Equals: |
The geometric shapes are topologically equal. |
Disjoint: |
The geometry has no points in common. |
Intersects: |
Geometric shapes have at least one common point (different from disjoint) |
Touches: |
The geometry has at least one common boundary point, but no internal points. |
Crosses: |
The geometric shapes share some but not all internal points. |
With (Within): |
The lines of geometry A are all inside geometry B. |
Contains: |
Lines of geometry B are all inside geometry A (different from inclusion) |
Overlaps: |
The geometric shapes share some but not all common points, and they have their own same area at the intersection. |
- The following example shows how to use Equals, Disjoint, Intersects, Within operations:
package com.alibaba.autonavi; import com.vividsolutions.jts.geom.*; import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; /** * gemotry之间的关系 * @author xingxing.dxx * */ public class GeometryRelated { private GeometryFactory geometryFactory = new GeometryFactory(); /** * 两个几何对象是否是重叠的 * @return * @throws ParseException */ public boolean equalsGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(5 0, 0 0)"); return geometry1.equals(geometry2);//true } /** * 几何对象没有交点(相邻) * @return * @throws ParseException */ public boolean disjointGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)"); return geometry1.disjoint(geometry2); * @throws ParseException * @return * At least one common point (intersection) / ** } * / public boolean intersectsGeo() throws ParseException{ WKTReader reader = new WKTReader (geometryFactory); LineString geometry1 = (LineString) reader.read ("LINESTRING (0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read ("LINESTRING (0 0, 0 2) "); Geometry interPoint = geometry1.intersection (geometry2); // Intersection point System.out.println (interPoint.toText ()); // Output POINT (0 0) return geometry1.intersects (geometry2) ; } / ** * Determine whether the point (x, y) with x and y coordinates is in the Polygon represented by geometry * @param x * @param y * @param geometry wkt format * @return * / public boolean withinGeo(double x,double y,String geometry) throws ParseException { Coordinate coord = new Coordinate(x,y); Point point = geometryFactory.createPoint( coord ); WKTReader reader = new WKTReader( geometryFactory ); Polygon polygon = (Polygon) reader.read(geometry); return point.within(polygon); } /** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { GeometryRelated gr = new GeometryRelated(); System.out.println(gr.equalsGeo()); System.out.println(gr.disjointGeo()); System.out.println(gr.intersectsGeo()); System.out.println(gr.withinGeo(5,5,"POLYGON((0 0, 10 0, 10 10, 0 10,0 0))")); } }
Relationship analysis
- There are several types of relationship analysis
Buffer analysis (Buffer) |
Contain all polygons and multi-polygons within a specified distance |
Convex Hull Analysis (ConvexHull) |
Convex hull polygon containing all points of geometric shape (outsourcing polygon) |
Intersection |
A∩B intersection operation is the set of all common points in polygon AB |
Joint Analysis (Union) |
The joint operation of AUB AB is the set of all points of AB |
Difference Analysis (Difference) |
(AA∩B) The difference analysis of AB shape is the set of all points in A that are not in B |
Symmetric difference analysis (SymDifference) |
(AUB-A∩B) The symmetric difference analysis of AB shape is the set of all points located in A or B but not in AB |
2. Let's take a look at specific examples
package com.alibaba.autonavi; import java.util.ArrayList; import java.util.List; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.LineString; /** * gemotry之间的关系分析 * * @author xingxing.dxx */ public class Operation { private GeometryFactory geometryFactory = new GeometryFactory(); /** * create a Point * * @param x * @param y * @return */ public Coordinate point(double x, double y) { return new Coordinate(x, y); } / ** * create a line * * @return * / public LineString createLine (List <Coordinate> points) { Coordinate [] coords = (Coordinate []) points.toArray (new Coordinate [points.size ()]); LineString line = geometryFactory.createLineString (coords); return line; } / ** * return polygons and multi-polygons within a specified distance * * @param a * @param distance * @return * / public Geometry bufferGeo (Geometry a, double distance ) { return a.buffer (distance); } * /** * Return the distance between the closest two points in (A) and (B) * @param a * @param b * @return * / public double distanceGeo (Geometry a, Geometry b) { return a.distance (b); } / ** * The intersection of two geometric objects * * @param a * @param b * @return * / public Geometry intersectionGeo (Geometry a, Geometry b) { return a.intersection (b); } / ** * Geometry Object merge * * @param a * @param b * @return * / public Geometry unionGeo (Geometry a, Geometry b) { return a .union (b); } / ** * Yes in A geometry object, but not in B geometry object * * @param a * @param b * @return */ public Geometry differenceGeo(Geometry a, Geometry b) { return a.difference(b); } public static void main(String[] args) { Operation op = new Operation(); //创建一条线 List<Coordinate> points1 = new ArrayList<Coordinate>(); points1.add(op.point(0, 0)); points1.add(op.point(1, 3)); points1.add(op.point(2, 3)); LineString line1 = op.createLine(points1); //创建第二条线 List<Coordinate> points2 = new ArrayList<Coordinate>(); points2.add(op.point(3, 0)); points2.add(op.point(3, 3)); points2.add(op.point(5, 6)); LineString line2 = op.createLine(points2); System.out.println(op.distanceGeo(line1, line2));//out 1.0 System.out.println(op.intersectionGeo(line1, line2));//out GEOMETRYCOLLECTION EMPTY System.out.println(op.unionGeo(line1, line2)); //out MULTILINESTRING ((0 0, 1 3, 2 3), (3 0, 3 3, 5 6)) System.out.println(op.differenceGeo(line1, line2));//out LINESTRING (0 0, 1 3, 2 3) } }