关系判断
- 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) }}