JTS Topology Suite (JTS) 拓扑运算函数库介绍
JTS拓扑套件是一个用于创建和操作矢量几何体的Java库。它还提供了一套全面的几何测试用例,以及用于处理和可视化几何和JTS功能的TestBuilder GUI应用程序。在GIS中是非常重要的可以使用与图形缓冲、解析、抽稀
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);