關系判斷
- Geometry之間的關系有如下幾種:
相等(Equals): |
幾何形狀拓撲上相等。 |
脫節(Disjoint): |
幾何形狀沒有共有的點。 |
相交(Intersects): |
幾何形狀至少有一個共有點(區別於脫節) |
接觸(Touches): |
幾何形狀有至少一個公共的邊界點,但是沒有內部點。 |
交叉(Crosses): |
幾何形狀共享一些但不是所有的內部點。 |
內含(Within): |
幾何形狀A的線都在幾何形狀B內部。 |
包含(Contains): |
幾何形狀B的線都在幾何形狀A內部(區別於內含) |
重疊(Overlaps): |
幾何形狀共享一部分但不是所有的公共點,而且相交處有他們自己相同的區域。 |
- 如下例子展示了如何使用Equals,Disjoint,Intersects,Within操作:
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); } /** * 至少一個公共點(相交) * @return * @throws ParseException */ 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);//相交點 System.out.println(interPoint.toText());//輸出 POINT (0 0) return geometry1.intersects(geometry2); } /** * 判斷以x,y為坐標的點point(x,y)是否在geometry表示的Polygon中 * @param x * @param y * @param geometry wkt格式 * @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))")); } }
關系分析
- 關系分析有如下幾種
緩沖區分析(Buffer) |
包含所有的點在一個指定距離內的多邊形和多多邊形 |
凸殼分析(ConvexHull) |
包含幾何形體的所有點的最小凸殼多邊形(外包多邊形) |
交叉分析(Intersection) |
A∩B 交叉操作就是多邊形AB中所有共同點的集合 |
聯合分析(Union) |
AUB AB的聯合操作就是AB所有點的集合 |
差異分析(Difference) |
(A-A∩B) AB形狀的差異分析就是A里有B里沒有的所有點的集合 |
對稱差異分析(SymDifference) |
(AUB-A∩B) AB形狀的對稱差異分析就是位於A中或者B中但不同時在AB中的所有點的集合 |
2. 我們來看看具體的例子
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; } /** * 返回a指定距離內的多邊形和多多邊形 * * @param a * @param distance * @return */ public Geometry bufferGeo(Geometry a, double distance) { return a.buffer(distance); } /** * 返回(A)與(B)中距離最近的兩個點的距離 * * @param a * @param b * @return */ public double distanceGeo(Geometry a, Geometry b) { return a.distance(b); } /** * 兩個幾何對象的交集 * * @param a * @param b * @return */ public Geometry intersectionGeo(Geometry a, Geometry b) { return a.intersection(b); } /** * 幾何對象合並 * * @param a * @param b * @return */ public Geometry unionGeo(Geometry a, Geometry b) { return a.union(b); } /** * 在A幾何對象中有的,但是B幾何對象中沒有 * * @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) } }