Relationship Judgment
- There are several types of relationships between geometry:
Equal (Equals): |
Geometric shapes are topological equal. |
Disconnect (disjoint): |
There are no common points for geometric shapes. |
Intersect (intersects): |
Geometric shapes have at least one common point (different from disconnection) |
Contact (touches): |
The geometry has at least one common boundary point, but no inner point. |
Cross (crosses): |
Geometric shapes share some but not all of the interior points. |
Included (within): |
The lines of geometry A are inside the geometry B. |
Included (Contains): |
The lines of geometry B are inside the geometry a (different from the included) |
Overlap (overlaps): |
Geometric shapes share part but not all common points, and intersect with their own area. |
- The following example shows how to use the Equals,disjoint,intersects,within operation:
PackageCom.alibaba.autonavi;Importcom.vividsolutions.jts.geom.*;Importcom.vividsolutions.jts.io.ParseException;ImportCom.vividsolutions.jts.io.WKTReader;/*** The relationship between Gemotry *@authorxingxing.dxx **/ Public classgeometryrelated {PrivateGeometryfactory geometryfactory =Newgeometryfactory (); /*** Two Geometry objects are overlapping *@return * @throwsparseexception*/ Public BooleanEqualsgeo ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); LineString Geometry1= (LineString) reader.read ("LineString (0 0, 2 0, 5 0)"); LineString Geometry2= (LineString) reader.read ("LineString (5 0, 0 0)"); returnGeometry1.equals (Geometry2);//true } /*** Geometric objects have no intersection (adjacent) *@return * @throwsparseexception*/ Public BooleanDisjointgeo ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); LineString Geometry1= (LineString) reader.read ("LineString (0 0, 2 0, 5 0)"); LineString Geometry2= (LineString) reader.read ("LineString (0 1, 0 2)"); returnGeometry1.disjoint (Geometry2); } /*** At least one common point (intersection) *@return * @throwsparseexception*/ Public BooleanIntersectsgeo ()throwsparseexception{Wktreader Reader=NewWktreader (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 PointSystem.out.println (Interpoint.totext ());//output Point (0 0) returngeometry1.intersects (Geometry2); } /*** Determine if point (x, Y) with x, y coordinates is in the polygon represented by geometry *@paramx *@paramy *@paramGeometry wkt Format *@return */ Public BooleanWithingeo (DoubleXDoubleY,string geometry)throwsparseexception {coordinate coord=Newcoordinate (x, y); Point Point=Geometryfactory.createpoint (coord); Wktreader Reader=NewWktreader (geometryfactory); Polygon Polygon=(Polygon) reader.read (geometry); returnPoint.within (Polygon); } /** * @paramargs *@throwsparseexception*/ Public Static voidMain (string[] args)throwsparseexception {geometryrelated gr=Newgeometryrelated (); 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 (buffering) |
Contains all the points within a specified distance of the polygon and multi-polygon |
Convex hull Analysis (convexhull) |
Minimum convex hull polygon (outsourced polygon) that contains all points of the geometric shape |
Cross-analysis (intersection) |
A∩B Cross Operation is a collection of all the commonalities in polygon AB |
Joint Analysis (Union) |
The joint operation of AUB AB is a collection of all ab points. |
Variance Analysis (difference) |
(A-A∩B) The differential analysis of the AB shape is a collection of all points in a where B does not. |
Symmetric difference analysis (symdifference) |
(AUB-A∩B) Symmetry difference analysis of ab shape is the set of all points in a or B but not at the same time in AB |
2. Let's take a look at specific examples
PackageCom.alibaba.autonavi;Importjava.util.ArrayList;Importjava.util.List;Importcom.vividsolutions.jts.geom.Coordinate;ImportCom.vividsolutions.jts.geom.Geometry;Importcom.vividsolutions.jts.geom.GeometryFactory;Importcom.vividsolutions.jts.geom.LineString;/*** Analysis of the relationship between Gemotry * *@authorxingxing.dxx*/ Public classOperation {PrivateGeometryfactory geometryfactory =Newgeometryfactory (); /*** Create a point * *@paramx *@paramy *@return */ PublicCoordinate Point (DoubleXDoubley) {return Newcoordinate (x, y); } /*** Create a line * *@return */ PublicLineString Createline (list<coordinate>points) {coordinate[] coords= (coordinate[]) Points.toarray (Newcoordinate[points.size ()]); LineString Line=geometryfactory.createlinestring (coords); returnLine ; } /*** Returns polygons and poly-polygons within a specified distance * *@paramA *@paramDistance *@return */ PublicGeometry Buffergeo (Geometry A,Doubledistance) { returnA.buffer (distance); } /*** Returns the distance between (A) and (B) the nearest two points * *@paramA *@paramb *@return */ Public DoubleDistancegeo (Geometry A, Geometry b) {returnA.distance (b); } /*** Intersection of two geometric objects * *@paramA *@paramb *@return */ PublicGeometry Intersectiongeo (Geometry A, Geometry b) {returnA.intersection (b); } /*** Geometric Object Merging * *@paramA *@paramb *@return */ PublicGeometry Uniongeo (Geometry A, Geometry b) {returnA.union (b); } /*** There are some in a geometry, but there is no * * in the B geometry object@paramA *@paramb *@return */ PublicGeometry Differencegeo (Geometry A, Geometry b) {returnA.difference (b); } Public Static voidMain (string[] args) {operation op=Newoperation (); //Create a lineList<coordinate> Points1 =NewArraylist<coordinate>(); Points1.add (Op.point (0, 0)); Points1.add (Op.point (1, 3)); Points1.add (Op.point (2, 3)); LineString line1=Op.createline (POINTS1); //Create a second lineList<coordinate> points2 =NewArraylist<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.0System.out.println (Op.intersectiongeo (line1, line2));//Out geometrycollection EMPTYSystem.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) }}
Judgement and analysis of JTS geometry relationship