使用 Spring Boot + Geodesy 实现地理空间高精度坐标处理及多样化的距离计算功能
Geodesy 框架介绍与特性
Geodesy 是一个专注于地理空间计算的强大框架。
它具有以下突出特性:
-
高精度坐标处理:能够精确处理和转换不同的地理坐标系统,确保坐标数据的准确性和一致性。
-
多样化的距离计算方法:支持多种距离计算模式,如直线距离、测地线距离等,满足各种复杂的业务需求。
-
强大的空间分析能力:可以进行空间关系的判断,如点与区域的包含关系、区域之间的相交关系等。
-
高效性能:在处理大规模地理数据时,能保持高效的计算速度和低内存消耗。
-
跨平台兼容性:可以在不同的操作系统和开发环境中稳定运行。
这些特性使得 Geodesy 在地理信息系统、导航应用、物流规划等领域具有广泛的应用价值。
在许多应用场景中,需要计算两个地点之间的距离。本文将介绍如何使用 Spring Boot 结合 Geodesy 库来实现距离计算功能。
项目创建及依赖配置(pom.xml)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>distance-calculation-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Distance Calculation System</name>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
服务类:
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import java.util.ArrayList;
import java.util.List;
public class GeospatialCalculationService {
public double calculateStraightLineDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
GlobalCoordinates point1 = new GlobalCoordinates(latitude1, longitude1);
GlobalCoordinates point2 = new GlobalCoordinates(latitude2, longitude2);
double distance = Math.sqrt(Math.pow((latitude2 - latitude1), 2) + Math.pow((longitude2 - longitude1), 2));
return distance;
}
public double calculateGeodesicDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
GlobalCoordinates point1 = new GlobalCoordinates(latitude1, longitude1);
GlobalCoordinates point2 = new GlobalCoordinates(latitude2, longitude2);
GeodeticCalculator calculator = new GeodeticCalculator();
GeodeticCurve curve = calculator.calculateGeodeticCurve(Ellipsoid.WGS84, point1, point2);
return curve.getEllipsoidalDistance();
}
public boolean isPointInRegion(double pointLatitude, double pointLongitude, List<GlobalCoordinates> regionPoints) {
// 假设区域是凸多边形
int n = regionPoints.size();
boolean inside = false;
for (int i = 0, j = n - 1; i < n; j = i++) {
if (((regionPoints.get(i).getLatitude() > pointLatitude)!= (regionPoints.get(j).getLatitude() > pointLatitude))
&& (pointLongitude < (regionPoints.get(j).getLongitude() - regionPoints.get(i).getLongitude())
* (pointLatitude - regionPoints.get(i).getLatitude())
/ (regionPoints.get(j).getLatitude() - regionPoints.get(i).getLatitude())
+ regionPoints.get(i).getLongitude())) {
inside =!inside;
}
}
return inside;
}
public boolean isRegionIntersect(List<GlobalCoordinates> region1Points, List<GlobalCoordinates> region2Points) {
// 简单判断,假设两个区域都是凸多边形且边界不相交
for (GlobalCoordinates point1 : region1Points) {
if (isPointInRegion(point1.getLatitude(), point1.getLongitude(), region2Points)) {
return true;
}
}
for (GlobalCoordinates point2 : region2Points) {
if (isPointInRegion(point2.getLatitude(), point2.getLongitude(), region1Points)) {
return true;
}
}
return false;
}
public double calculateArea(List<GlobalCoordinates> polygonPoints) {
double area = 0.0;
int n = polygonPoints.size();
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
area += polygonPoints.get(i).getLongitude() * polygonPoints.get(j).getLatitude()
- polygonPoints.get(j).getLongitude() * polygonPoints.get(i).getLatitude();
}
area /= 2.0;
return Math.abs(area);
}
}
总结
本文详细阐述了如何利用 Spring Boot 与 Geodesy 库相结合来实现距离计算等地理空间计算功能。通过完整的代码示例,包括项目的依赖配置、服务类和控制器类的实现,展示了如何使用该库进行距离、点与多边形关系判断以及多边形面积计算等操作。希望本文能为大家在相关领域的开发工作提供有价值的参考和帮助。
作者: 原文:https://mp.weixin.qq.com/s/pzP12ETiL30BZEiRTS8utQ
没有回复内容