JTS = Java Topology Suite
几何计算:
1. 前端js就用这个 Turfjs的类库。参考网站: 计算两线段相交点 | Turf.js中文网
2. 后端java语言就可以用 JTS这个类库,参考网站:
JTS参考网站:
1. https://github.com/locationtech/jts
GitHub - locationtech/jts: The JTS Topology Suite is a Java library for creating and manipulating vector geometry.The JTS Topology Suite is a Java library for creating and manipulating vector geometry. - GitHub - locationtech/jts: The JTS Topology Suite is a Java library for creating and manipulating vector geometry.https://github.com/locationtech/jts 2. https://locationtech.github.io/jts/JTS | Documentationhttps://locationtech.github.io/jts/
1. https://github.com/locationtech/jts
2. https://locationtech.github.io/jts/
POM文件:
<dependency> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> <version>1.18.0</version> </dependency>
实例代码:
可以通过JTS
要使用JTS(Java Topology Suite)库计算
1. 某个点是否在另外一个闭合的空间内
2. 计算某个闭合的空间的中心的的位置
3. 已知两个点的经纬度,计算他们之间的距离
4. 已知某点的经纬度坐标,计算其他点的经纬度坐标
简单实例如下:
package com.simulate.jts;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.linearref.LengthIndexedLine;
public class JtsExample {
public static void main(String[] args) throws ParseException {
// 1. 某个点是否在另外一个闭合的空间内
// isInside();
// 2. 计算某个闭合的空间的中心的的位置
// calcCentPoint();
//3. 已知两个点的经纬度,计算他们之间的距离
// pointDistance();
// 4. 已知某点的经纬度坐标,计算其他点的经纬度坐标
calcCoordinate();
// 5.
}
// 1. 某个点是否在另外一个闭合的空间内
static void isInside() throws ParseException{
// 创建 GeometryFactory 对象
GeometryFactory geometryFactory = new GeometryFactory();
// 创建点对象
Coordinate pointCoord = new Coordinate(2.0, 2.0);
Point point = geometryFactory.createPoint(pointCoord);
// 创建多边形对象
WKTReader wktReader = new WKTReader(geometryFactory);
Polygon polygon = (Polygon) wktReader.read("POLYGON((0 0, 0 4, 4 4, 4 0, 0 0))");
// 判断点是否在多边形内部
boolean isInside = polygon.contains(point);
// 输出结果
System.out.println("Point: " + point.toText());
System.out.println("Polygon: " + polygon.toText());
System.out.println("Is inside: " + isInside);
}
// 2. 计算某个闭合的空间的中心的的位置
static void calcCentPoint(){
int pointCount = 5;
Coordinate[] coordinates = new Coordinate[pointCount];
// 填充Coordinate数组
coordinates[0] = new Coordinate(1.0, 1.0);
coordinates[1] = new Coordinate(2.0, 3.0);
coordinates[2] = new Coordinate(4.0, 1.0);
coordinates[3] = new Coordinate(3.0, 4.0);
coordinates[4] = new Coordinate(1.0, 1.0);
// 创建Polygon对象
GeometryFactory factory = new GeometryFactory();
Polygon polygon = factory.createPolygon(coordinates);
polygon.getDimension();
// 计算中心点坐标
Coordinate centerCoordinate = polygon.getCentroid().getCoordinate();
double centerX = centerCoordinate.x;
double centerY = centerCoordinate.y;
// 输出结果
System.out.println("Center point: (" + centerX + ", " + centerY + ")");
}
//3. 已知两个点的经纬度,计算他们之间的距离
static void pointDistance(){
double lon1 = 115.8575; // 第一个点的经度
double lat1 = 28.6829; // 第一个点的纬度
double lon2 = 116.4074; // 第二个点的经度
double lat2 = 39.9042; // 第二个点的纬度
// 创建GeometryFactory对象
GeometryFactory factory = new GeometryFactory();
// 创建Coordinate对象
Coordinate coordinate1 = new Coordinate(lon1, lat1);
Coordinate coordinate2 = new Coordinate(lon2, lat2);
// 创建Point对象
Point point1 = factory.createPoint(coordinate1);
Point point2 = factory.createPoint(coordinate2);
// 计算两点之间的距离
double distance = point1.distance(point2);
// 输出结果
System.out.println("Distance between the two points: " + distance);
}
/
// 4. 已知某点的经纬度坐标,计算其他点的经纬度坐标
static void calcCoordinate() {
// 假设已知的参考点的坐标
double lat1 = 40.7128; // 参考点的纬度
double lon1 = -74.0060; // 参考点的经度
// 假设要计算的距离和方向
double distanceInMeters = 1000; // 距离为 1000 米
double bearingInDegrees = 45; // 方向为 45 度
// 创建参考点的坐标对象
Coordinate referenceCoord = new Coordinate(lon1, lat1);
// 计算目标点的坐标
Coordinate targetCoord = calculateCoordinate(referenceCoord, distanceInMeters, bearingInDegrees);
// 打印目标点的经纬度
System.out.println("目标点的经度:" + targetCoord.x);
System.out.println("目标点的纬度:" + targetCoord.y);
}
// 使用 JTS 计算目标坐标
static Coordinate calculateCoordinate(Coordinate referenceCoord, double distance, double bearing) {
// 将距离转换为度数
double distanceInDegrees = Math.toDegrees(distance / 6371000.0); // 假设地球是一个球体,半径为 6371000 米
// 根据参考点和距离创建线段对象
// LengthIndexedLine line = new LengthIndexedLine(new Coordinate[] { referenceCoord });
GeometryFactory factory = new GeometryFactory();
Point referencePoint = factory.createPoint(referenceCoord);
LengthIndexedLine line = new LengthIndexedLine(referencePoint);
// 在线段上根据方向和距离计算目标点的索引
double targetIndex = line.project(referenceCoord) + distanceInDegrees;
// 根据目标索引获取目标点的坐标
Coordinate targetCoord = line.extractPoint(targetIndex);
return targetCoord;
}
}