月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-14 236b037b0d8d2673bb8f1f00013983459c6e669c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.moon.server.helper;
 
import com.moon.server.entity.all.StaticData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.osr.SpatialReference;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.referencing.CRS;
import org.geotools.referencing.GeodeticCalculator;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
import java.awt.geom.Point2D;
 
/**
 * Geo帮助类
 * @author WWW
 * @date 2023-09-14
 */
public class GeoHelper {
    public static SpatialReference sr104903;
 
    public final static double MOON_RADIUS = 1738000;
 
    public static CoordinateReferenceSystem crs104903;
 
    private final static Log log = LogFactory.getLog(GeoHelper.class);
 
    /**
     * 初始化坐标系
     */
    public static void initSr() {
        try {
            sr104903 = new SpatialReference(StaticData.MOON_2000_WKT);
 
            crs104903 = CRS.parseWKT(StaticData.MOON_2000_WKT);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 获取距离
     */
    public static double getDistance(double x1, double y1, double x2, double y2) {
        GeodeticCalculator geodeticCalculator = new GeodeticCalculator(crs104903);
        geodeticCalculator.setStartingGeographicPoint(x1, y1);
        geodeticCalculator.setDestinationGeographicPoint(x2, y2);
 
        return geodeticCalculator.getOrthodromicDistance();
    }
 
    /**
     * 获取距离2
     */
    public static double getDistance2(double lon1, double lat1, double lon2, double lat2) {
        double radLat1 = Math.toRadians(lat1);
        double radLat2 = Math.toRadians(lat2);
        double a = radLat1 - radLat2;
        double b = Math.toRadians(lon1) - Math.toRadians(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
 
        return s * MOON_RADIUS;
    }
 
    /**
     * 获取方向角
     */
    public static double getBearing(double x1, double y1, double x2, double y2) {
        return (90 - Math.toDegrees(Math.atan2(x2 - x1, y2 - y1)) + 360) % 360;
    }
 
    /**
     * 获取角度
     */
    public static double getAngle(double x1, double y1, double x2, double y2) {
        DirectPosition2D p1 = new DirectPosition2D(crs104903, x1, y1);
        DirectPosition2D p2 = new DirectPosition2D(crs104903, x2, y2);
 
        GeodeticCalculator gc = new GeodeticCalculator();
        gc.setStartingGeographicPoint(p1);
        gc.setDestinationGeographicPoint(p2);
 
        return gc.getAzimuth();
    }
 
    /**
     * 获取角度2
     */
    public static double getAngle2(double x1, double y1, double x2, double y2) {
        DirectPosition2D p1 = new DirectPosition2D(x1, y1);
        DirectPosition2D p2 = new DirectPosition2D(x2, y2);
 
        GeodeticCalculator gc = new GeodeticCalculator();
        gc.setStartingGeographicPoint(p1);
        gc.setDestinationGeographicPoint(p2);
 
        return gc.getAzimuth();
    }
 
    /**
     * 根据距离和角度获取目标点
     */
    public static Point2D getPointByDistanceAndAngle(double x, double y, double angle, double distance) {
        DirectPosition2D p1 = new DirectPosition2D(x, y);
 
        GeodeticCalculator gc = new GeodeticCalculator();
        gc.setStartingGeographicPoint(p1);
        gc.setDirection(angle, distance);
 
        return gc.getDestinationGeographicPoint();
    }
}