当前位置:   article > 正文

JTS Geometry关系判断和分析_jts intersection求取线段时错误

jts intersection求取线段时错误

关系判断

  1. Geometry之间的关系有如下几种:

相等(Equals):

几何形状拓扑上相等。

脱节(Disjoint):

几何形状没有共有的点。

相交(Intersects):

几何形状至少有一个共有点(区别于脱节)

接触(Touches):

几何形状有至少一个公共的边界点,但是没有内部点。

交叉(Crosses):

几何形状共享一些但不是所有的内部点。

内含(Within):

几何形状A的线都在几何形状B内部。

包含(Contains):

几何形状B的线都在几何形状A内部(区别于内含)

重叠(Overlaps):

几何形状共享一部分但不是所有的公共点,而且相交处有他们自己相同的区域。

  1. 如下例子展示了如何使用Equals,Disjoint,Intersects,Within操作:

复制代码

  1. package com.alibaba.autonavi;
  2. import com.vividsolutions.jts.geom.*;
  3. import com.vividsolutions.jts.io.ParseException;
  4. import com.vividsolutions.jts.io.WKTReader;
  5. /**
  6. * gemotry之间的关系
  7. * @author xingxing.dxx
  8. *
  9. */
  10. public class GeometryRelated {
  11. private GeometryFactory geometryFactory = new GeometryFactory();
  12. /**
  13. * 两个几何对象是否是重叠的
  14. * @return
  15. * @throws ParseException
  16. */
  17. public boolean equalsGeo() throws ParseException{
  18. WKTReader reader = new WKTReader( geometryFactory );
  19. LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
  20. LineString geometry2 = (LineString) reader.read("LINESTRING(5 0, 0 0)");
  21. return geometry1.equals(geometry2);//true
  22. }
  23. /**
  24. * 几何对象没有交点(相邻)
  25. * @return
  26. * @throws ParseException
  27. */
  28. public boolean disjointGeo() throws ParseException{
  29. WKTReader reader = new WKTReader( geometryFactory );
  30. LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
  31. LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)");
  32. return geometry1.disjoint(geometry2);
  33. }
  34. /**
  35. * 至少一个公共点(相交)
  36. * @return
  37. * @throws ParseException
  38. */
  39. public boolean intersectsGeo() throws ParseException{
  40. WKTReader reader = new WKTReader( geometryFactory );
  41. LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
  42. LineString geometry2 = (LineString) reader.read("LINESTRING(0 0, 0 2)");
  43. Geometry interPoint = geometry1.intersection(geometry2);//相交点
  44. System.out.println(interPoint.toText());//输出 POINT (0 0)
  45. return geometry1.intersects(geometry2);
  46. }
  47. /**
  48. * 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中
  49. * @param x
  50. * @param y
  51. * @param geometry wkt格式
  52. * @return
  53. */
  54. public boolean withinGeo(double x,double y,String geometry) throws ParseException {
  55. Coordinate coord = new Coordinate(x,y);
  56. Point point = geometryFactory.createPoint( coord );
  57. WKTReader reader = new WKTReader( geometryFactory );
  58. Polygon polygon = (Polygon) reader.read(geometry);
  59. return point.within(polygon);
  60. }
  61. /**
  62. * @param args
  63. * @throws ParseException
  64. */
  65. public static void main(String[] args) throws ParseException {
  66. GeometryRelated gr = new GeometryRelated();
  67. System.out.println(gr.equalsGeo());
  68. System.out.println(gr.disjointGeo());
  69. System.out.println(gr.intersectsGeo());
  70. System.out.println(gr.withinGeo(5,5,"POLYGON((0 0, 10 0, 10 10, 0 10,0 0))"));
  71. }
  72. }

复制代码

 

关系分析

  1. 关系分析有如下几种

缓冲区分析(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. 我们来看看具体的例子

复制代码

  1. package com.alibaba.autonavi;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.vividsolutions.jts.geom.Coordinate;
  5. import com.vividsolutions.jts.geom.Geometry;
  6. import com.vividsolutions.jts.geom.GeometryFactory;
  7. import com.vividsolutions.jts.geom.LineString;
  8. /**
  9. * gemotry之间的关系分析
  10. *
  11. * @author xingxing.dxx
  12. */
  13. public class Operation {
  14. private GeometryFactory geometryFactory = new GeometryFactory();
  15. /**
  16. * create a Point
  17. *
  18. * @param x
  19. * @param y
  20. * @return
  21. */
  22. public Coordinate point(double x, double y) {
  23. return new Coordinate(x, y);
  24. }
  25. /**
  26. * create a line
  27. *
  28. * @return
  29. */
  30. public LineString createLine(List<Coordinate> points) {
  31. Coordinate[] coords = (Coordinate[]) points.toArray(new Coordinate[points.size()]);
  32. LineString line = geometryFactory.createLineString(coords);
  33. return line;
  34. }
  35. /**
  36. * 返回a指定距离内的多边形和多多边形
  37. *
  38. * @param a
  39. * @param distance
  40. * @return
  41. */
  42. public Geometry bufferGeo(Geometry a, double distance) {
  43. return a.buffer(distance);
  44. }
  45. /**
  46. * 返回(A)与(B)中距离最近的两个点的距离
  47. *
  48. * @param a
  49. * @param b
  50. * @return
  51. */
  52. public double distanceGeo(Geometry a, Geometry b) {
  53. return a.distance(b);
  54. }
  55. /**
  56. * 两个几何对象的交集
  57. *
  58. * @param a
  59. * @param b
  60. * @return
  61. */
  62. public Geometry intersectionGeo(Geometry a, Geometry b) {
  63. return a.intersection(b);
  64. }
  65. /**
  66. * 几何对象合并
  67. *
  68. * @param a
  69. * @param b
  70. * @return
  71. */
  72. public Geometry unionGeo(Geometry a, Geometry b) {
  73. return a.union(b);
  74. }
  75. /**
  76. * 在A几何对象中有的,但是B几何对象中没有
  77. *
  78. * @param a
  79. * @param b
  80. * @return
  81. */
  82. public Geometry differenceGeo(Geometry a, Geometry b) {
  83. return a.difference(b);
  84. }
  85. public static void main(String[] args) {
  86. Operation op = new Operation();
  87. //创建一条线
  88. List<Coordinate> points1 = new ArrayList<Coordinate>();
  89. points1.add(op.point(0, 0));
  90. points1.add(op.point(1, 3));
  91. points1.add(op.point(2, 3));
  92. LineString line1 = op.createLine(points1);
  93. //创建第二条线
  94. List<Coordinate> points2 = new ArrayList<Coordinate>();
  95. points2.add(op.point(3, 0));
  96. points2.add(op.point(3, 3));
  97. points2.add(op.point(5, 6));
  98. LineString line2 = op.createLine(points2);
  99. System.out.println(op.distanceGeo(line1, line2));//out 1.0
  100. System.out.println(op.intersectionGeo(line1, line2));//out GEOMETRYCOLLECTION EMPTY
  101. System.out.println(op.unionGeo(line1, line2)); //out MULTILINESTRING ((0 0, 1 3, 2 3), (3 0, 3 3, 5 6))
  102. System.out.println(op.differenceGeo(line1, line2));//out LINESTRING (0 0, 1 3, 2 3)
  103. }
  104. }

复制代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/941056
推荐阅读
相关标签
  

闽ICP备14008679号