GeoTools应用-(JTS Geometry Operations)(二)

2024-03-02 19:32

本文主要是介绍GeoTools应用-(JTS Geometry Operations)(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

(1)、Buffer,返回的结果是一个Polygon或者 MultiPolygon

buffering is an operation which in GIS is used to compute the area containing all
points within a given distance of a Geometry.
You can use JTS to compute the buffer of a Geometry using the Geometry buffer method
or the BufferOp class. The input Geometry to the buffer operation may be of any type
(including arbitrary GeometryCollections).  The result of a buffer operation is always an area
type (Polygon or MultiPolygon)
.  The result may be empty (for example, a negative buffer
of a LineString).

GeometryFactory.java

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

Buffers.java

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

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

(2)、Polygonization 面处理类

Polygonization is the process of forming polygons from linework which
encloses areas. Linework to be formed into polygons must be fully noded –
that is, linestrings must not cross and must touch only at endpoints.
  JTS provides the Polygonizer class to perform Polygonization. The Polygonizer
takes a set of fully noded LineStrings and forms all the polygons which are
enclosed by the lines. Polygonization errors such as dangling lines or cut
lines can be identified and reported.

Polygonization.java

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

输出结果:

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

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

Sometimes a spatial operation such as #union will produce
chains of small LineStrings. The JTS LineMerger is a simple utility
to sew these small LineStrings together. NOTE:they do not cross; only
their endpoints can touch. If LineStrings to be merged do not have
the same direction, the direction of the resulting LineString will be
that of the majority.

MergerLine.java

[java] view plain copy print ?
  1. package com.mapbar.jst; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.Collection; 
  5. import java.util.List; 
  6.  
  7. import com.vividsolutions.jts.geom.Geometry; 
  8. import com.vividsolutions.jts.operation.linemerge.LineMerger; 
  9.  
  10. public class MergerLine { 
  11.  
  12.     private static GeometryFactory factory = GeometryFactory.getInstance(); 
  13.  
  14.     public static void main(String[] args) { 
  15.         LineMerger lineMerger = new LineMerger(); 
  16.         List<Geometry> list = new ArrayList<Geometry>(); 
  17.         list.add(factory.buildGeo("LINESTRING (3 3,2 2,0 0)")); 
  18.         list.add(factory.buildGeo("LINESTRING (3 3,6 6,0 10)")); 
  19.         list.add(factory.buildGeo("LINESTRING (0 10,3 1,10 1)")); 
  20.         lineMerger.add(list); 
  21.         Collection<Geometry> mergerLineStrings = lineMerger.getMergedLineStrings(); 
  22.         for (Geometry g : mergerLineStrings) { 
  23.             System.out.println(g.toText()); 
  24.         } 
  25.     } 
package com.mapbar.jst;import java.util.ArrayList;
import java.util.Collection;
import java.util.List;import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.operation.linemerge.LineMerger;public class MergerLine {private static GeometryFactory factory = GeometryFactory.getInstance();public static void main(String[] args) {LineMerger lineMerger = new LineMerger();List<Geometry> list = new ArrayList<Geometry>();list.add(factory.buildGeo("LINESTRING (3 3,2 2,0 0)"));list.add(factory.buildGeo("LINESTRING (3 3,6 6,0 10)"));list.add(factory.buildGeo("LINESTRING (0 10,3 1,10 1)"));lineMerger.add(list);Collection<Geometry> mergerLineStrings = lineMerger.getMergedLineStrings();for (Geometry g : mergerLineStrings) {System.out.println(g.toText());}}
}

输出结果:LINESTRING (0 0, 2 2, 3 3, 6 6, 0 10, 3 1, 10 1)

lineMerger 和union区别,union可以在两条相交的线中生成交点(noded)

(4)、union 线路合并,并且生成交叉点。

The noding process splits LineStrings that cross into smaller LineStrings  that meet at a point, or node

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


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



(6)、叠加操作  叠加可以用来确定任何几何图形的布尔组合。
The overlay can be used to determine any boolean combination of the geometries.
通过对两个数据进行的一系列集合运算,产生新数据的过程。叠加分析的目的就是通过对空间数据的加工或分析,提取用户需要的新的空间几何信息。
叠加分析类型包括:
交叉分析(Intersection) 交叉操作就是多边形AB中所有共同点的集合。
联合分析(Union) AB的联合操作就是AB所有点的集合。
差异分析(Difference) AB形状的差异分析就是A里有B里没有的所有点的集合。
对称差异分析(SymDifference) AB形状的对称差异分析就是位于A中或者B中但不同时在AB中的所有点的集合

[java] view plain copy print ?
  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> 
	public void overlaps() throws ParseException, FileNotFoundException{WKTReader reader = new WKTReader(geometryFactory);Polygon geometry1 = (Polygon) reader.read("POLYGON((0 0, 2 0 ,2 2, 0 2,0 0))");Polygon geometry2 = (Polygon) reader.read("POLYGON((0 0, 4 0 , 4 1, 0 1, 0 0))");OverlayOp op = new OverlayOp(geometry1,geometry2);Geometry g =op.getResultGeometry(OverlayOp.INTERSECTION);//POLYGON ((2 0, 0 0, 0 1, 2 1, 2 0))Geometry g2 = op.getResultGeometry(OverlayOp.UNION);Geometry g3 = op.getResultGeometry(OverlayOp.DIFFERENCE);Geometry g4 = op.getResultGeometry(OverlayOp.SYMDIFFERENCE);PlanarGraph p = op.getGraph(); //图<v,e>}

这篇关于GeoTools应用-(JTS Geometry Operations)(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/767197

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

zoj3820(树的直径的应用)

题意:在一颗树上找两个点,使得所有点到选择与其更近的一个点的距离的最大值最小。 思路:如果是选择一个点的话,那么点就是直径的中点。现在考虑两个点的情况,先求树的直径,再把直径最中间的边去掉,再求剩下的两个子树中直径的中点。 代码如下: #include <stdio.h>#include <string.h>#include <algorithm>#include <map>#

【区块链 + 人才服务】可信教育区块链治理系统 | FISCO BCOS应用案例

伴随着区块链技术的不断完善,其在教育信息化中的应用也在持续发展。利用区块链数据共识、不可篡改的特性, 将与教育相关的数据要素在区块链上进行存证确权,在确保数据可信的前提下,促进教育的公平、透明、开放,为教育教学质量提升赋能,实现教育数据的安全共享、高等教育体系的智慧治理。 可信教育区块链治理系统的顶层治理架构由教育部、高校、企业、学生等多方角色共同参与建设、维护,支撑教育资源共享、教学质量评估、

AI行业应用(不定期更新)

ChatPDF 可以让你上传一个 PDF 文件,然后针对这个 PDF 进行小结和提问。你可以把各种各样你要研究的分析报告交给它,快速获取到想要知道的信息。https://www.chatpdf.com/

【区块链 + 人才服务】区块链集成开发平台 | FISCO BCOS应用案例

随着区块链技术的快速发展,越来越多的企业开始将其应用于实际业务中。然而,区块链技术的专业性使得其集成开发成为一项挑战。针对此,广东中创智慧科技有限公司基于国产开源联盟链 FISCO BCOS 推出了区块链集成开发平台。该平台基于区块链技术,提供一套全面的区块链开发工具和开发环境,支持开发者快速开发和部署区块链应用。此外,该平台还可以提供一套全面的区块链开发教程和文档,帮助开发者快速上手区块链开发。

【C++高阶】C++类型转换全攻略:深入理解并高效应用

📝个人主页🌹:Eternity._ ⏩收录专栏⏪:C++ “ 登神长阶 ” 🤡往期回顾🤡:C++ 智能指针 🌹🌹期待您的关注 🌹🌹 ❀C++的类型转换 📒1. C语言中的类型转换📚2. C++强制类型转换⛰️static_cast🌞reinterpret_cast⭐const_cast🍁dynamic_cast 📜3. C++强制类型转换的原因📝

基于 YOLOv5 的积水检测系统:打造高效智能的智慧城市应用

在城市发展中,积水问题日益严重,特别是在大雨过后,积水往往会影响交通甚至威胁人们的安全。通过现代计算机视觉技术,我们能够智能化地检测和识别积水区域,减少潜在危险。本文将介绍如何使用 YOLOv5 和 PyQt5 搭建一个积水检测系统,结合深度学习和直观的图形界面,为用户提供高效的解决方案。 源码地址: PyQt5+YoloV5 实现积水检测系统 预览: 项目背景