使用 Spring Boot + Geodesy 实现地理空间高精度坐标处理及多样化的距离计算功能

前言

之前我们计算地理空间点位之间的距离等操作,是使用mysql自带的函数进行处理,这样的话需要每个操作都要单独编写一个sql语句,非常的麻烦并且时间查询较慢,效率比较低,今天我们使用 Spring Boot + Geodesy 实现地理空间高精度坐标处理及多样化的距离计算功能,更加方便快捷。

这是之前使用mysql进行简单空间操作的文章:mysql中point的使用

一、Geodesy 框架介绍与特性

Geodesy 是一个专注于地理空间计算的强大框架。它具有以下突出特性:

1.高精度坐标处理:能够精确处理和转换不同的地理坐标系统,确保坐标数据的准确性和一致性。

2.多样化的距离计算方法:支持多种距离计算模式,如直线距离、测地线距离等,满足各种复杂的业务需求。
3.强大的空间分析能力:可以进行空间关系的判断,如点与区域的包含关系、区域之间的相交关系等。
4.高效性能:在处理大规模地理数据时,能保持高效的计算速度和低内存消耗。
5.跨平台兼容性:可以在不同的操作系统和开发环境中稳定运行。

这些特性使得 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>

服务类

package com.icoderoad.service;

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);
    }
}

控制器类

package com.icoderoad.controller;

import com.icoderoad.service.GeospatialCalculationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/geospatial")
public class GeospatialController {

    @Autowired
    private GeospatialCalculationService geospatialCalculationService;

//计算直线距离
    @GetMapping("/calculateStraightLineDistance")
    public double calculateStraightLineDistance(@RequestParam double latitude1, @RequestParam double longitude1,
                                               @RequestParam double latitude2, @RequestParam double longitude2) {
        return geospatialCalculationService.calculateStraightLineDistance(latitude1, longitude1, latitude2, longitude2);
    }
//计算测地距离(三维空间中最短距离)
    @GetMapping("/calculateGeodesicDistance")
    public double calculateGeodesicDistance(@RequestParam double latitude1, @RequestParam double longitude1,
                                           @RequestParam double latitude2, @RequestParam double longitude2) {
        return geospatialCalculationService.calculateGeodesicDistance(latitude1, longitude1, latitude2, longitude2);
    }
//是否为区域内的点
    @GetMapping("/isPointInRegion")
    public boolean isPointInRegion(@RequestParam double pointLatitude, @RequestParam double pointLongitude,
                                  @RequestParam List<Double> regionLatitudes, @RequestParam List<Double> regionLongitudes) {
        List<GlobalCoordinates> regionPoints = new ArrayList<>();
        for (int i = 0; i < regionLatitudes.size(); i++) {
            regionPoints.add(new GlobalCoordinates(regionLatitudes.get(i), regionLongitudes.get(i)));
        }
        return geospatialCalculationService.isPointInRegion(pointLatitude, pointLongitude, regionPoints);
    }
//区域是否相交
    @GetMapping("/isRegionIntersect")
    public boolean isRegionIntersect(@RequestParam List<Double> region1Latitudes, @RequestParam List<Double> region1Longitudes,
                                    @RequestParam List<Double> region2Latitudes, @RequestParam List<Double> region2Longitudes) {
        List<GlobalCoordinates> region1Points = new ArrayList<>();
        List<GlobalCoordinates> region2Points = new ArrayList<>();

        for (int i = 0; i < region1Latitudes.size(); i++) {
            region1Points.add(new GlobalCoordinates(region1Latitudes.get(i), region1Longitudes.get(i)));
        }

        for (int i = 0; i < region2Latitudes.size(); i++) {
            region2Points.add(new GlobalCoordinates(region2Latitudes.get(i), region2Longitudes.get(i)));
        }

        return geospatialCalculationService.isRegionIntersect(region1Points, region2Points);
    }
//计算面积
    @GetMapping("/calculateArea")
    public double calculateArea(@RequestParam List<Double> polygonLatitudes, @RequestParam List<Double> polygonLongitudes) {
        List<GlobalCoordinates> polygonPoints = new ArrayList<>();
        for (int i = 0; i < polygonLatitudes.size(); i++) {
            polygonPoints.add(new GlobalCoordinates(polygonLatitudes.get(i), polygonLongitudes.get(i)));
        }
        return geospatialCalculationService.calculateArea(polygonPoints);
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值