JTS =?Java Topology Suite
幾何計(jì)算:
1.?前端js就用這個(gè) Turfjs的類庫。參考網(wǎng)站:?計(jì)算兩線段相交點(diǎn) | Turf.js中文網(wǎng)
2. 后端java語言就可以用 JTS這個(gè)類庫,參考網(wǎng)站:
?JTS參考網(wǎng)站:
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>
實(shí)例代碼:
可以通過JTS
要使用JTS(Java Topology Suite)庫計(jì)算
1. 某個(gè)點(diǎn)是否在另外一個(gè)閉合的空間內(nèi)
2. 計(jì)算某個(gè)閉合的空間的中心的的位置
3. 已知兩個(gè)點(diǎn)的經(jīng)緯度,計(jì)算他們之間的距離
4. 已知某點(diǎn)的經(jīng)緯度坐標(biāo),計(jì)算其他點(diǎn)的經(jīng)緯度坐標(biāo)文章來源:http://www.zghlxwxcb.cn/news/detail-485784.html
?簡單實(shí)例如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-485784.html
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. 某個(gè)點(diǎn)是否在另外一個(gè)閉合的空間內(nèi)
// isInside();
// 2. 計(jì)算某個(gè)閉合的空間的中心的的位置
// calcCentPoint();
//3. 已知兩個(gè)點(diǎn)的經(jīng)緯度,計(jì)算他們之間的距離
// pointDistance();
// 4. 已知某點(diǎn)的經(jīng)緯度坐標(biāo),計(jì)算其他點(diǎn)的經(jīng)緯度坐標(biāo)
calcCoordinate();
// 5.
}
// 1. 某個(gè)點(diǎn)是否在另外一個(gè)閉合的空間內(nèi)
static void isInside() throws ParseException{
// 創(chuàng)建 GeometryFactory 對象
GeometryFactory geometryFactory = new GeometryFactory();
// 創(chuàng)建點(diǎn)對象
Coordinate pointCoord = new Coordinate(2.0, 2.0);
Point point = geometryFactory.createPoint(pointCoord);
// 創(chuàng)建多邊形對象
WKTReader wktReader = new WKTReader(geometryFactory);
Polygon polygon = (Polygon) wktReader.read("POLYGON((0 0, 0 4, 4 4, 4 0, 0 0))");
// 判斷點(diǎn)是否在多邊形內(nèi)部
boolean isInside = polygon.contains(point);
// 輸出結(jié)果
System.out.println("Point: " + point.toText());
System.out.println("Polygon: " + polygon.toText());
System.out.println("Is inside: " + isInside);
}
// 2. 計(jì)算某個(gè)閉合的空間的中心的的位置
static void calcCentPoint(){
int pointCount = 5;
Coordinate[] coordinates = new Coordinate[pointCount];
// 填充Coordinate數(shù)組
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);
// 創(chuàng)建Polygon對象
GeometryFactory factory = new GeometryFactory();
Polygon polygon = factory.createPolygon(coordinates);
polygon.getDimension();
// 計(jì)算中心點(diǎn)坐標(biāo)
Coordinate centerCoordinate = polygon.getCentroid().getCoordinate();
double centerX = centerCoordinate.x;
double centerY = centerCoordinate.y;
// 輸出結(jié)果
System.out.println("Center point: (" + centerX + ", " + centerY + ")");
}
//3. 已知兩個(gè)點(diǎn)的經(jīng)緯度,計(jì)算他們之間的距離
static void pointDistance(){
double lon1 = 115.8575; // 第一個(gè)點(diǎn)的經(jīng)度
double lat1 = 28.6829; // 第一個(gè)點(diǎn)的緯度
double lon2 = 116.4074; // 第二個(gè)點(diǎn)的經(jīng)度
double lat2 = 39.9042; // 第二個(gè)點(diǎn)的緯度
// 創(chuàng)建GeometryFactory對象
GeometryFactory factory = new GeometryFactory();
// 創(chuàng)建Coordinate對象
Coordinate coordinate1 = new Coordinate(lon1, lat1);
Coordinate coordinate2 = new Coordinate(lon2, lat2);
// 創(chuàng)建Point對象
Point point1 = factory.createPoint(coordinate1);
Point point2 = factory.createPoint(coordinate2);
// 計(jì)算兩點(diǎn)之間的距離
double distance = point1.distance(point2);
// 輸出結(jié)果
System.out.println("Distance between the two points: " + distance);
}
/
// 4. 已知某點(diǎn)的經(jīng)緯度坐標(biāo),計(jì)算其他點(diǎn)的經(jīng)緯度坐標(biāo)
static void calcCoordinate() {
// 假設(shè)已知的參考點(diǎn)的坐標(biāo)
double lat1 = 40.7128; // 參考點(diǎn)的緯度
double lon1 = -74.0060; // 參考點(diǎn)的經(jīng)度
// 假設(shè)要計(jì)算的距離和方向
double distanceInMeters = 1000; // 距離為 1000 米
double bearingInDegrees = 45; // 方向?yàn)?45 度
// 創(chuàng)建參考點(diǎn)的坐標(biāo)對象
Coordinate referenceCoord = new Coordinate(lon1, lat1);
// 計(jì)算目標(biāo)點(diǎn)的坐標(biāo)
Coordinate targetCoord = calculateCoordinate(referenceCoord, distanceInMeters, bearingInDegrees);
// 打印目標(biāo)點(diǎn)的經(jīng)緯度
System.out.println("目標(biāo)點(diǎn)的經(jīng)度:" + targetCoord.x);
System.out.println("目標(biāo)點(diǎn)的緯度:" + targetCoord.y);
}
// 使用 JTS 計(jì)算目標(biāo)坐標(biāo)
static Coordinate calculateCoordinate(Coordinate referenceCoord, double distance, double bearing) {
// 將距離轉(zhuǎn)換為度數(shù)
double distanceInDegrees = Math.toDegrees(distance / 6371000.0); // 假設(shè)地球是一個(gè)球體,半徑為 6371000 米
// 根據(jù)參考點(diǎn)和距離創(chuàng)建線段對象
// LengthIndexedLine line = new LengthIndexedLine(new Coordinate[] { referenceCoord });
GeometryFactory factory = new GeometryFactory();
Point referencePoint = factory.createPoint(referenceCoord);
LengthIndexedLine line = new LengthIndexedLine(referencePoint);
// 在線段上根據(jù)方向和距離計(jì)算目標(biāo)點(diǎn)的索引
double targetIndex = line.project(referenceCoord) + distanceInDegrees;
// 根據(jù)目標(biāo)索引獲取目標(biāo)點(diǎn)的坐標(biāo)
Coordinate targetCoord = line.extractPoint(targetIndex);
return targetCoord;
}
}
到了這里,關(guān)于【Turfjs的java版本JTS】前面講了Turfjs可以實(shí)現(xiàn)幾何計(jì)算,空間計(jì)算的功能,如果后端要做這項(xiàng)功能也有類似的類庫,JTS的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!