aix
2024-08-13 128183e176aab3003a04b517b56162ba2ef780b0
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
package com.dji.sample.wayline.plane;
 
import lombok.Data;
 
import java.util.List;
 
/**
 * @Author AIX
 * @Date 2024/7/11 11:12
 * @Version 1.0
 */
@Data
public class BoundingRectangle {
 
    public MapLatLng topLeft;
    public MapLatLng topRight;
    public MapLatLng bottomRight;
    public MapLatLng bottomLeft;
 
    // 构造器
    // 构造函数,使用四个角点初始化外接矩形
    public BoundingRectangle(MapLatLng topLeft, MapLatLng topRight, MapLatLng bottomRight, MapLatLng bottomLeft) {
        this.topLeft = topLeft;
        this.topRight = topRight;
        this.bottomRight = bottomRight;
        this.bottomLeft = bottomLeft;
    }
 
    /**
     * 静态方法,根据多边形点列表计算外接矩形并返回BoundingRectangle实例
     * @param polygon
     * @return
     */
    public static BoundingRectangle fromPolygon(List<double[]> polygon) {
        double maxLat = Double.NEGATIVE_INFINITY;
        double minLat = Double.POSITIVE_INFINITY;
        double maxLon = Double.NEGATIVE_INFINITY;
        double minLon = Double.POSITIVE_INFINITY;
 
        // 遍历多边形点以找到边界
        for (double[] point : polygon) {
            double lat = point[1]; // 假设每个点的数组是先经度[0]后纬度[1]
            double lon = point[0];
 
            if (lat > maxLat) {
                maxLat = lat;
            }
            if (lat < minLat) {
                minLat = lat;
            }
            if (lon > maxLon) {
                maxLon = lon;
            }
            if (lon < minLon) {
                minLon = lon;
            }
        }
 
        // 创建外接矩形的四个角点
        MapLatLng topLeft = new MapLatLng(maxLat, minLon);
        MapLatLng topRight = new MapLatLng(maxLat, maxLon);
        MapLatLng bottomRight = new MapLatLng(minLat, maxLon);
        MapLatLng bottomLeft = new MapLatLng(minLat, minLon);
 
        // 返回BoundingRectangle实例
        return new BoundingRectangle(topLeft, topRight, bottomRight, bottomLeft);
    }
 
}