zhaowenyu
2026-02-04 1f6e8a56efec18d9343f6ac0f93d4cf21e9ab668
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.ard.data.noflyzone.utils;
 
import com.ard.data.noflyzone.domain.ArdNoFlyBox;
import com.ard.data.noflyzone.domain.ArdNoFlyZone;
import com.ard.data.noflyzone.domain.FeatureCollection;
import com.ard.data.noflyzone.domain.Vertex;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Polygon;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class NoFlyZoneUtils {
 
 
    /**
     * 计算点到多边形的最近距离(米)
     */
    public static double pointToPolygonDistance(Vertex point, List<Vertex> polygon) {
 
        // 如果在多边形内部,距离为 0
        if (isPointInPolygon(point, polygon)) {
            return 0;
        }
 
        double minDistance = Double.MAX_VALUE;
        int n = polygon.size();
 
        for (int i = 0; i < n; i++) {
            Vertex a = polygon.get(i);
            Vertex b = polygon.get((i + 1) % n); // 下一点,最后一条边连接第一个点
 
            double dist = pointToSegmentDistanceHaversine(point, a, b);
            minDistance = Math.min(minDistance, dist);
        }
 
        return minDistance;
    }
 
    /**
     * 计算点到线段的距离(米)使用Haversine公式
     */
    public static double pointToSegmentDistanceHaversine(Vertex p, Vertex a, Vertex b) {
        // 将经纬度换算成以 p 点为参考的局部平面米坐标
        double[] pa = latLonToXY(a, p.y, p.x);
        double[] pb = latLonToXY(b, p.y, p.x);
        double[] pp = latLonToXY(p, p.y, p.x);
 
        double dx = pb[0] - pa[0];
        double dy = pb[1] - pa[1];
 
        double t = ((pp[0] - pa[0]) * dx + (pp[1] - pa[1]) * dy) / (dx * dx + dy * dy);
 
        double nearestX, nearestY;
        if (t < 0) {
            nearestX = pa[0];
            nearestY = pa[1];
        } else if (t > 1) {
            nearestX = pb[0];
            nearestY = pb[1];
        } else {
            nearestX = pa[0] + t * dx;
            nearestY = pa[1] + t * dy;
        }
 
        double distX = pp[0] - nearestX;
        double distY = pp[1] - nearestY;
 
        return Math.sqrt(distX * distX + distY * distY);
    }
 
    /**
     * 将经纬度转换为局部平面坐标(米)
     * originLat、originLon 是参考点,经纬度转换到以参考点为原点的平面坐标
     */
    public static double[] latLonToXY(Vertex point, double originLat, double originLon) {
        double R = 6371000; // 地球半径,单位米
        double dLat = Math.toRadians(point.y - originLat);
        double dLon = Math.toRadians(point.x - originLon);
        double x = R * dLon * Math.cos(Math.toRadians(originLat));
        double y = R * dLat;
        return new double[]{x, y};
    }
 
 
 
    /**
     * 判断点是否在多边形内(射线法)
     */
    public static boolean isPointInPolygon(Vertex point, List<Vertex> polygon) {
        int n = polygon.size();
        boolean inside = false;
        for (int i = 0, j = n - 1; i < n; j = i++) {
            Vertex vi = polygon.get(i);
            Vertex vj = polygon.get(j);
            boolean intersect = ((vi.y > point.y) != (vj.y > point.y)) &&
                    (point.x < (vj.x - vi.x) * (point.y - vi.y) / (vj.y - vi.y) + vi.x);
            if (intersect) inside = !inside;
        }
        return inside;
    }
 
    /**
     * 判断线段是否与多边形相交
     */
    public static boolean isLineIntersectPolygon(Vertex p1, Vertex p2, List<Vertex> polygon) {
        int n = polygon.size();
        for (int i = 0; i < n; i++) {
            Vertex a = polygon.get(i);
            Vertex b = polygon.get((i + 1) % n);
            if (isSegmentsIntersect(p1, p2, a, b)) {
                return true;
            }
        }
        return false;
    }
 
    /**
     * 判断两条线段是否相交
     */
    private static boolean isSegmentsIntersect(Vertex p1, Vertex p2, Vertex p3, Vertex p4) {
        double d1 = cross(p3, p4, p1);
        double d2 = cross(p3, p4, p2);
        double d3 = cross(p1, p2, p3);
        double d4 = cross(p1, p2, p4);
 
        if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) &&
                ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0))) {
            return true;
        }
 
        // 处理共线情况
        if (d1 == 0 && onSegment(p3, p4, p1)) return true;
        if (d2 == 0 && onSegment(p3, p4, p2)) return true;
        if (d3 == 0 && onSegment(p1, p2, p3)) return true;
        if (d4 == 0 && onSegment(p1, p2, p4)) return true;
 
        return false;
    }
 
    private static double cross(Vertex p1, Vertex p2, Vertex p3) {
        return (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y);
    }
 
    private static boolean onSegment(Vertex p1, Vertex p2, Vertex q) {
        return q.x <= Math.max(p1.x, p2.x) && q.x >= Math.min(p1.x, p2.x)
                && q.y <= Math.max(p1.y, p2.y) && q.y >= Math.min(p1.y, p2.y);
    }
 
 
    /**
     * 判断点是否在矩形内
     */
    public static boolean isInsideBox(Vertex point, ArdNoFlyBox box) {
        if (point == null || box == null) return false;
        return point.x >= box.getMinx() &&
                point.x <= box.getMaxx() &&
                point.y >= box.getMiny() &&
                point.y <= box.getMaxy();
    }
 
 
    /**
     * 根据 GeoJSON(FeatureCollection)计算外接矩形
     */
    @SuppressWarnings("unchecked")
    public static ArdNoFlyBox getArdNoFlyBox(FeatureCollection featureCollection) {
        if (featureCollection == null || featureCollection.getFeatures() == null) {
            return null;
        }
 
        double minx = Double.MAX_VALUE;
        double miny = Double.MAX_VALUE;
        double maxx = Double.MIN_VALUE;
        double maxy = Double.MIN_VALUE;
 
        for (FeatureCollection.Feature feature : featureCollection.getFeatures()) {
            FeatureCollection.Geometry geometry = feature.getGeometry();
            if (geometry == null || geometry.getCoordinates() == null) {
                continue;
            }
 
            String type = geometry.getType();
            Object coords = geometry.getCoordinates();
 
            if ("Polygon".equalsIgnoreCase(type)) {
                // Polygon: [[[lon, lat], [lon, lat], ...]]
                List<List<List<Double>>> rings = (List<List<List<Double>>>) coords;
                for (List<List<Double>> ring : rings) {
                    for (List<Double> point : ring) {
                        if (point.size() < 2) {
                            continue;
                        }
                        double x = point.get(0);
                        double y = point.get(1);
                        minx = Math.min(minx, x);
                        miny = Math.min(miny, y);
                        maxx = Math.max(maxx, x);
                        maxy = Math.max(maxy, y);
                    }
                }
            }
            else if ("MultiPolygon".equalsIgnoreCase(type)) {
                // MultiPolygon: [[[[lon, lat], ...]], ...]
                List<List<List<List<Double>>>> polygons = (List<List<List<List<Double>>>>) coords;
                for (List<List<List<Double>>> polygon : polygons) {
                    for (List<List<Double>> ring : polygon) {
                        for (List<Double> point : ring) {
                            if (point.size() < 2) {
                                continue;
                            }
                            double x = point.get(0);
                            double y = point.get(1);
                            minx = Math.min(minx, x);
                            miny = Math.min(miny, y);
                            maxx = Math.max(maxx, x);
                            maxy = Math.max(maxy, y);
                        }
                    }
                }
            }
        }
 
        return new ArdNoFlyBox(minx, miny, maxx, maxy);
    }
 
    /**
     * 将 GeoJSON Polygon 的外环坐标转换成 Vertex 列表
     */
    public static List<Vertex> toVertexes(FeatureCollection featureCollection) {
        List<Vertex> vertexes = new ArrayList<>();
        if (featureCollection == null || featureCollection.getFeatures() == null || featureCollection.getFeatures().isEmpty()) {
            return vertexes;
        }
        FeatureCollection.Feature feature = featureCollection.getFeatures().get(0);
        if (feature.getGeometry() != null) {
            List<List<List<Double>>> coords = feature.getGeometry().getCoordinates();
            if (coords != null && !coords.isEmpty()) {
                List<List<Double>> outerRing = coords.get(0);
                for (List<Double> point : outerRing) {
                    if (point.size() >= 2) {
                        vertexes.add(new Vertex(point.get(0), point.get(1)));
                    }
                }
            }
        }
        return vertexes;
    }
 
    public static double degDistanceToMeter(double deg, double lat) {
        // 1° ≈ 111320 米,但是经度和纬度不一样,这里用纬度做修正
        return deg * 111320.0 * Math.cos(Math.toRadians(lat));
    }
}