Java对于JTS Topology Suite (JTS)使用

JTS拓扑套件是一个Java库,专注于矢量几何体的创建和操作。它提供了丰富的几何测试用例和TestBuilderGUI应用。文章展示了如何使用JTS进行空间计算,包括计算最近点和距离、图形抽稀及图形缓冲,并提供了代码示例来说明其功能。
摘要由CSDN通过智能技术生成

JTS Topology Suite (JTS) 拓扑运算函数库介绍

   JTS拓扑套件是一个用于创建和操作矢量几何体的Java库。它还提供了一套全面的几何测试用例,以及用于处理和可视化几何和JTS功能的TestBuilder GUI应用程序。在GIS中是非常重要的可以使用与图形缓冲、解析、抽稀

GitHub地址:GitHub - locationtech/jts: The JTS Topology Suite is a Java library for creating and manipulating vector geometry.

官网:JTS Topology Suite - OSGeo

Java使用

maven依赖

官网maven仓库,可能使用国内镜像拉取不到,需要进行修改

 jts/USING.md at master · locationtech/jts · GitHub

<dependency>
 <groupId>org.locationtech.jts</groupId>
 <artifactId>jts</artifactId>
 <version>1.19.0</version>
 <type>pom</type>
 </dependency>

 空间计算

计算最近点和距离
 public static void main(String[] args) {
 List<Point> point =new ArrayList<>();
 double lngmin=80.711213;
 double lngmax=119.124769;

 double latmin =39.844964;
 double latmax =40.989945;

 int ptCount =3000;

 double dx = (lngmax-lngmin)/ptCount;
 double dy = (latmax-latmin)/ptCount;
 GeometryFactory geometryFactory = new GeometryFactory();

 Coordinate[] coordinates =
 new Coordinate[ptCount];

 for (int i = 0; i < ptCount; i++) {
 double latRandom= latmin +dy*i;
 double lngRandom= lngmin+dx*i;
 coordinates[i]=new Coordinate(lngRandom,latRandom);


 latRandom= latmin+Math.random()*(latmax-latmin+0.000001);
 lngRandom= lngmin+Math.random()*(lngmax-lngmin+0.000001);
 Point pt =new Point(lngRandom,latRandom);
 point.add(pt);
 }

 LineString lineString = geometryFactory.createLineString(coordinates);
 PointPairDistance ppd = new PointPairDistance();
 long timeStamp = CommonUtils.getTimeStamp();
 for (int i = 0; i < point.size(); i++) {
 //计算任意两个图形之间的距离
 double distance = lineString.distance(geometryFactory.createPoint(new Co
 org.locationtech.jts.geom.Point pt =geometryFactory.createPoint(new Coor
 //计算任意的最近点
 Coordinate[] coordinates1 = DistanceOp.nearestPoints(lineString, pt);
 }
 long endStamp = CommonUtils.getTimeStamp();
 System.out.print("cost :"+(endStamp-timeStamp));
 }
图形抽稀&图形缓冲
private List<GeoPoint> getCoordinate(List<Point> routeLines,double rangeMeters)
 Coordinate[] coordinates =
 new Coordinate[routeLines.size()];
 for (int i = 0; i < routeLines.size(); i++) {
 coordinates[i]=new Coordinate(routeLines.get(i).getLon(),routeLines.get(i).getLat())
 }

 //构造图形对象
 LineString lineString = geometryFactory.createLineString(coordinates);
 //图形抽稀
 Geometry simplify = TopologyPreservingSimplifier.simplify(lineString, 0.000001 )
 //图形缓冲
 Geometry buffer = simplify.buffer(rangeMeters*0.00001141);

 List<GeoPoint> points =new ArrayList<>();
 for (int i = 0; i < buffer.getCoordinates().length; i++) {
 GeoPoint geoPoint=new GeoPoint(buffer.getCoordinates()[i].y,buffer.getCoordinates()[i].x)
 points.add(geoPoint);
 }
 return points;
 }
设置缓冲参数
 BufferParameters bufferParameters = new BufferParameters();
 //设置为单向缓冲,不设置的话默认双向缓冲
 bufferParameters.setSingleSided(true);
 BufferOp bufOp = new BufferOp(geometry, bufferParameters);

 //向线路左侧缓冲
 Geometry geometryLeft = bufOp.getResultGeometry(kmToDegreeLeft); //左
 //向线路右侧缓冲
 Geometry geometryRight = bufOp.getResultGeometry(-kmToDegreeRight); //右 -1

截取指定⻓度的线段  

 //从线段的起点开始计算,获取⻓度为500⽶的点位置索引
 LinearLocation linearLocation = LengthLocationMap.getLocation(lineString, 500);
 //根据索引拿到具体的点位
Coordinate pt = locationIndexedLine.extractPoint(linearLocation);

 LinearLocation start = locationIndexedLine.indexOf(prePt);
 LinearLocation end = locationIndexedLine.indexOf(pt);
 //截取线段
 Geometry geometry = locationIndexedLine.extractLine(start, end);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值