puzhibing
2023-02-15 2811bab657aab4145b65a45a824fb63e93b58e30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.stylefeng.guns.modular.system.util;
 
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 计算两个金纬度坐标之间的直线距离
 */
@Component
public class GeodesyUtil {
 
 
    /**
     * 获取直线距离
     * @param fromLonLat
     * @param toLonLat
     * @return
     */
    public Map<String, Double> getDistance(String fromLonLat, String toLonLat){
        String[] from = fromLonLat.split(",");
        String[] to = toLonLat.split(",");
        GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(from[1]), Double.valueOf(from[0]));
        GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(to[1]), Double.valueOf(to[0]));
        double Sphere = getDistanceMeter(source, target, Ellipsoid.Sphere);
        double WGS84 = getDistanceMeter(source, target, Ellipsoid.WGS84);
        double GRS80 = getDistanceMeter(source, target, Ellipsoid.GRS80);
        double GRS67 = getDistanceMeter(source, target, Ellipsoid.GRS67);
        double ANS = getDistanceMeter(source, target, Ellipsoid.ANS);
        double WGS72 = getDistanceMeter(source, target, Ellipsoid.WGS72);
        double Clarke1858 = getDistanceMeter(source, target, Ellipsoid.Clarke1858);
        double Clarke1880 = getDistanceMeter(source, target, Ellipsoid.Clarke1880);
//        System.out.println("Sphere坐标系计算结果:"+Sphere + "米");
//        System.out.println("WGS84坐标系计算结果:"+WGS84 + "米");
//        System.out.println("GRS80坐标系计算结果:"+GRS80 + "米");
//        System.out.println("GRS67坐标系计算结果:"+GRS67 + "米");
//        System.out.println("ANS坐标系计算结果:"+ANS + "米");
//        System.out.println("WGS72坐标系计算结果:"+WGS72 + "米");
//        System.out.println("Clarke1858坐标系计算结果:"+Clarke1858 + "米");
//        System.out.println("Clarke1880坐标系计算结果:"+Clarke1880 + "米");
        Map<String, Double> map = new HashMap<>();
        map.put("Sphere", Sphere);
        map.put("WGS84", WGS84);
        map.put("GRS80", GRS80);
        map.put("GRS67", GRS67);
        map.put("ANS", ANS);
        map.put("WGS72", WGS72);
        map.put("Clarke1858", Clarke1858);
        map.put("Clarke1880", Clarke1880);
        return map;
    }
 
 
    private double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
        //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
 
 
 
    public static void main(String[] ages){
        GeodesyUtil geodesyUtil = new GeodesyUtil();
        geodesyUtil.getDistance("115.481028,39.989643", "114.465302,40.004717");
    }
}