guoshilong
2023-11-13 68f80a7451a126335153ec00bb6cb520a5ae1f8d
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
package com.dji.sample.utils;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dji.sample.wayline.dao.IWaylineJobMapper;
import com.dji.sample.wayline.model.dto.WaylineJobDTO;
import com.dji.sample.wayline.model.entity.WaylineJobEntity;
import com.dji.sample.wayline.model.param.PointPOJO;
import com.dji.sample.wayline.service.IWaylineJobService;
import com.dji.sample.wayline.service.impl.WaylineJobServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.List;
import java.util.Optional;
 
/**
 * 空间计算工具类
 */
public class GeoUtils {
    /**
     * 默认地球半径
     */
    private static double EARTH_RADIUS = 6371000;//赤道半径(单位m)
 
    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
 
 
    /**
     * 计算中心经纬度与目标经纬度的距离(米)
     *
     * @param centerLon 中心精度
     * @param centerLat 中心纬度
     * @param targetLon 需要计算的精度
     * @param targetLat 需要计算的纬度
     * @return 米
     */
    public static double distance(double centerLon, double centerLat, double targetLon, double targetLat) {
 
        double jl_jd = 102834.74258026089786013677476285;// 每经度单位米;
        double jl_wd = 111712.69150641055729984301412873;// 每纬度单位米;
        double b = Math.abs((centerLat - targetLat) * jl_jd);
        double a = Math.abs((centerLon - targetLon) * jl_wd);
        return Math.sqrt((a * a + b * b));
    }
 
    /**
     * 根据多个点计算中心点
     * @param list
     * @return
     */
    public static PointPOJO getCenterPoint(List<PointPOJO> list) {
 
        int total = list.size();
        double X = 0, Y = 0, Z = 0;
 
        for (int i = 0; i < list.size(); i++) {
            PointPOJO point = list.get(i);
            double lat, lon, x, y, z;
            lon = point.getLon() * Math.PI / 180;
            lat = point.getLat() * Math.PI / 180;
            x = Math.cos(lat) * Math.cos(lon);
            y = Math.cos(lat) * Math.sin(lon);
            z = Math.sin(lat);
            X += x;
            Y += y;
            Z += z;
        }
 
 
        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double Lon = Math.atan2(Y, X);
        double Hyp = Math.sqrt(X * X + Y * Y);
        double Lat = Math.atan2(Z, Hyp);
 
        PointPOJO centerPoint = new PointPOJO();
        centerPoint.setLon( Lon * 180 / Math.PI);
        centerPoint.setLat(Lat * 180 / Math.PI);
        return centerPoint;
    }
}