当前位置:   article > 正文

JTS Geometry合并、裁切、叠加分析等_geotools geometry裁切操作

geotools geometry裁切操作

Geometry 空间分析方法几何图形操作包,在operation包内,包含buffer、distance、linemerge、overlap、polygonize、predicate、relate、valide八个子包。分别对应着计算图形的缓冲、距离、线段融合、图形覆盖、多边形化、断言、关联、有效性等的操作。所有的操作针对的都是在geom包中定义的Geometry对象。
由于在计算机中,所有的图形都是离散的点组成,所以所有的操作都是在组成图形的点的集合上进行的,一个图形(Geometry)的

缓冲(buffer)距离操作(distance)是个二元操作,操作对象Geometry A、B,返回(A)与(B)中距离最近的两个点的距离。
线段的融合(linemerge)是将Geometry A中相互连接的线段进行连接。


多边形化操作(polygonize)对Geometry A进行计算,返回一个多边形(Polygon)。将由许多个点表示的图形,用少量的点来表示,减少图形的信息,即对图形进行降维。

断言(predicate)是一个二维的操作,对Geometry之间的关系进行判断的操作。
关联(relate) 根据DE-9IM(The Dimensionally Extended Nine-Intersection Model),该方法返回两个Geometry A与B的相交矩阵IM(Intersections Matrix)。这个矩阵在计算图形关系上用到。

1.distance,intersection,union,difference 示例代码:

  1. package com.mapbar.geo.jts.operation;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.geotools.geometry.jts.JTSFactoryFinder;
  5. import com.vividsolutions.jts.geom.Coordinate;
  6. import com.vividsolutions.jts.geom.Geometry;
  7. import com.vividsolutions.jts.geom.GeometryFactory;
  8. import com.vividsolutions.jts.geom.LineString;
  9. /**
  10. * Class Operation.java
  11. * Description 几何对象操作
  12. * Company mapbar
  13. * author Chenll E-mail: Chenll@mapbar.com
  14. * Version 1.0
  15. * Date 2012-2-21 上午10:47:47
  16. */
  17. public class Operation {
  18. private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
  19. /**
  20. * create a Point
  21. * @param x
  22. * @param y
  23. * @return
  24. */
  25. public Coordinate point(double x,double y){
  26. return new Coordinate(x,y);
  27. }
  28. /**
  29. * create a line
  30. * @return
  31. */
  32. public LineString createLine(List<Coordinate> points){
  33. Coordinate[] coords = (Coordinate[]) points.toArray(new Coordinate[points.size()]);
  34. LineString line = geometryFactory.createLineString(coords);
  35. return line;
  36. }
  37. /**
  38. * 返回(A)与(B)中距离最近的两个点的距离
  39. * @param a
  40. * @param b
  41. * @return
  42. */
  43. public double distanceGeo(Geometry a,Geometry b){
  44. return a.distance(b);
  45. }
  46. /**
  47. * 两个几何对象的交集
  48. * @param a
  49. * @param b
  50. * @return
  51. */
  52. public Geometry intersectionGeo(Geometry a,Geometry b){
  53. return a.intersection(b);
  54. }
  55. /**
  56. * 几何对象合并
  57. * @param a
  58. * @param b
  59. * @return
  60. */
  61. public Geometry unionGeo(Geometry a,Geometry b){
  62. return a.union(b);
  63. }
  64. /**
  65. * 在A几何对象中有的,但是B几何对象中没有
  66. * @param a
  67. * @param b
  68. * @return
  69. */
  70. public Geometry differenceGeo(Geometry a,Geometry b){
  71. return a.difference(b);
  72. }
  73. public static void main(String[] args){
  74. Operation op = new Operation();
  75. //创建一条线
  76. List<Coordinate> points1 = new ArrayList<Coordinate>();
  77. points1.add(op.point(0,0));
  78. points1.add(op.point(1,3));
  79. points1.add(op.point(2,3));
  80. LineString line1 = op.createLine(points1);
  81. //创建第二条线
  82. List<Coordinate> points2 = new ArrayList<Coordinate>();
  83. points2.add(op.point(3,0));
  84. points2.add(op.point(3,3));
  85. points2.add(op.point(5,6));
  86. LineString line2 = op.createLine(points2);
  87. System.out.println(op.distanceGeo(line1,line2));//out 1.0
  88. System.out.println(op.intersectionGeo(line1,line2));//out GEOMETRYCOLLECTION EMPTY
  89. System.out.println(op.unionGeo(line1,line2)); //out MULTILINESTRING ((0 0, 1 3, 2 3), (3 0, 3 3, 5 6))
  90. System.out.println(op.differenceGeo(line1,line2));//out LINESTRING (0 0, 1 3, 2 3)
  91. }
  92. }

2.一些高级操作, Buffer,LineMerger,Polygonization,UnionLine,凹壳分析,Overlays

  1. package com.mapbar.jst;
  2. import com.vividsolutions.jts.geom.Geometry;
  3. import com.vividsolutions.jts.io.ParseException;
  4. import com.vividsolutions.jts.io.WKTReader;
  5. public class GeometryFactory {
  6. private WKTReader reader;
  7. private GeometryFactory instance = null;
  8. public static synchronized GeometryFactory getInstance(){
  9. if(instance==null){
  10. instance = new GeometryFactory();
  11. }
  12. return instance;
  13. }
  14. public void getReader(){
  15. reader = new WKTReader();
  16. }
  17. public Geometry buildGeo(String str){
  18. try {
  19. if(reader==null){
  20. reader = new WKTReader();
  21. }
  22. return reader.read(str);
  23. } catch (ParseException e) {
  24. throw new RuntimeException("buildGeometry Error",e);
  25. }
  26. }
  27. }

2.1 缓冲区操作:

  1. package com.mapbar.jst;
  2. import com.vividsolutions.jts.geom.Geometry;
  3. import com.vividsolutions.jts.operation.buffer.BufferOp;
  4. public class Buffers {
  5. private GeometryFactory factory = GeometryFactory.getInstance();
  6. public Geometry buildGeo(String str) {
  7. return factory.buildGeo(str);
  8. }
  9. public static void main(String[] args) {
  10. Buffers bs = new Buffers();
  11. String line1 = "LINESTRING (0 0, 1 1, 2 2,3 3)";
  12. Geometry g1 = bs.buildGeo(line1);
  13. //方式(一)
  14. Geometry g = g1.buffer(2);
  15. 方式(二) BufferOP
  16. BufferOp bufOp = new BufferOp(g1);
  17. bufOp.setEndCapStyle(BufferOp.CAP_BUTT);
  18. Geometry bg = bufOp.getResultGeometry(2);
  19. }
  20. }

bufOp.setEndCapStyle 缓冲样式的设置,总共有三种CAP_ROUND,CAP_BUTT,CAP_SQUARE 对应如下三种情况

2.2 Polygonization 

多边形化是由线条包围区域形成多边形的过程,各线段不能交叉,只能在端点接触,且完全闭合。

  1. package com.mapbar.jst;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Collection;
  5. import com.vividsolutions.jts.geom.Geometry;
  6. import com.vividsolutions.jts.operation.polygonize.Polygonizer;
  7. public class Polygonization {
  8. private static GeometryFactory factory = GeometryFactory.getInstance();
  9. public static void main(String[] args) {
  10. List<Geometry> list = new ArrayList<Geometry>();
  11. list.add(factory.buildGeo("LINESTRING (0 0,1 1)"));
  12. list.add(factory.buildGeo("LINESTRING (6 3,6 10)"));
  13. list.add(factory.buildGeo("LINESTRING (2 2,4 4,6 3)"));
  14. list.add(factory.buildGeo("LINESTRING (2 2,5 1,6 3)"));
  15. list.add(factory.buildGeo("LINESTRING (6 3,6 4)"));
  16. list.add(factory.buildGeo("LINESTRING (9 5,7 1,6 4)"));
  17. list.add(factory.buildGeo("LINESTRING (9 5,8 8,6 4)"));
  18. Polygonizer p = new Polygonizer();
  19. p.add(list);
  20. Collection<Geometry> polys = p.getPolygons(); //面
  21. Collection<Geometry> dangles = p.getDangles();//悬挂线
  22. Collection<Geometry> cuts = p.getCutEdges(); //面和面的连接线
  23. System.out.println(polys.size()+":"+polys.toString());
  24. System.out.println(dangles.size()+":"+dangles.toString());
  25. System.out.println(cuts.size()+":"+cuts.toString());
  26. }
  27. }

输出结果:

  1. 2:[POLYGON ((2 2, 4 4, 6 3, 5 1, 2 2)), POLYGON ((6 4, 8 8, 9 5, 7 1, 6 4))]
  2. 2:[LINESTRING (6 3, 6 10), LINESTRING (0 0, 1 1)]
  3. 1:[LINESTRING (6 3, 6 4)]

2.3 LineMerger 线路合并,线路之间不能有交点,并且只在线路末尾有公共交点

  1. package com.mapbar.jst;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import com.vividsolutions.jts.geom.Geometry;
  6. import com.vividsolutions.jts.operation.linemerge.LineMerger;
  7. public class MergerLine {
  8. private static GeometryFactory factory = GeometryFactory.getInstance();
  9. public static void main(String[] args) {
  10. LineMerger lineMerger = new LineMerger();
  11. List<Geometry> list = new ArrayList<Geometry>();
  12. list.add(factory.buildGeo("LINESTRING (3 3,2 2,0 0)"));
  13. list.add(factory.buildGeo("LINESTRING (3 3,6 6,0 10)"));
  14. list.add(factory.buildGeo("LINESTRING (0 10,3 1,10 1)"));
  15. lineMerger.add(list);
  16. Collection<Geometry> mergerLineStrings = lineMerger.getMergedLineStrings();
  17. for (Geometry g : mergerLineStrings) {
  18. System.out.println(g.toText());
  19. }
  20. }
  21. }
  1. 输出结果:LINESTRING (0 0, 2 2, 3 3, 6 6, 0 10, 3 1, 10 1)
  2. lineMerger 和union区别,union可以在两条相交的线中生成交点(noded)

2.4 union 线路合并,并且生成交叉点

  1. package com.mapbar.jst;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.vividsolutions.jts.geom.Geometry;
  5. public class UnionLine {
  6. private static GeometryFactory factory = GeometryFactory.getInstance();
  7. public static void main(String[] args) {
  8. List<Geometry> list = new ArrayList<Geometry>();
  9. list.add(factory.buildGeo("LINESTRING (10 10,2 2,0 0)"));
  10. list.add(factory.buildGeo("LINESTRING (10 0,6 6,0 10)"));
  11. list.add(factory.buildGeo("LINESTRING (1 1,3 1,10 1)"));
  12. Geometry nodedLine = list.get(0);
  13. for (int i = 1; i < list.size(); i++) {
  14. nodedLine = nodedLine.union(list.get(i));
  15. }
  16. int num = nodedLine.getNumGeometries();
  17. for (int j = 0; j < num; j++) {
  18. Geometry eachG = nodedLine.getGeometryN(j);
  19. System.out.println(eachG.toText());
  20. }
  21. }
  22. }

2.5 凹壳分析  包含几何形体的所有点的最小凸壳多边形(外包多边形)

2.6 叠加操作  叠加可以用来确定任何几何图形的布尔组合

通过对两个数据进行的一系列集合运算,产生新数据的过程。叠加分析的目的就是通过对空间数据的加工或分析,提取用户需要的新的空间几何信息。
叠加分析类型包括:
交叉分析(Intersection) 交叉操作就是多边形AB中所有共同点的集合。
联合分析(Union) AB的联合操作就是AB所有点的集合。
差异分析(Difference) AB形状的差异分析就是A里有B里没有的所有点的集合。
对称差异分析(SymDifference) AB形状的对称差异分析就是位于A中或者B中但不同时在AB中的所有点的集合

  1. public void overlaps() throws ParseException, FileNotFoundException{
  2. WKTReader reader = new WKTReader(geometryFactory);
  3. Polygon geometry1 = (Polygon) reader.read("POLYGON((0 0, 2 0 ,2 2, 0 2,0 0))");
  4. Polygon geometry2 = (Polygon) reader.read("POLYGON((0 0, 4 0 , 4 1, 0 1, 0 0))");
  5. OverlayOp op = new OverlayOp(geometry1,geometry2);
  6. Geometry g =op.getResultGeometry(OverlayOp.INTERSECTION);//POLYGON ((2 0, 0 0, 0 1, 2 1, 2 0))
  7. Geometry g2 = op.getResultGeometry(OverlayOp.UNION);
  8. Geometry g3 = op.getResultGeometry(OverlayOp.DIFFERENCE);
  9. Geometry g4 = op.getResultGeometry(OverlayOp.SYMDIFFERENCE);
  10. PlanarGraph p = op.getGraph(); //图<v,e>
  11. }

 

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

闽ICP备14008679号