rain
2024-04-10 28e82b20499f6659d2b20d504d3fb987a646e535
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
package com.dji.sample.patches.utils;
 
import com.dji.sample.patches.model.entity.LotInfo;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
 
import java.util.*;
 
/**
 * @PROJECT_NAME: drone
 * @DESCRIPTION:
 * @USER: aix
 * @DATE: 2024/3/25 10:35
 */
public class GeoToolsUtil {
 
    /**
     * 获取中心点位置
     *
     * @param geometry Geometry
     * @return
     */
    public static Coordinate getCentro(Geometry geometry) {
        return geometry.getCentroid().getCoordinate();
    }
 
    /**
     * 获取4个点的经纬度
     *
     * @param geometry Geometry
     * @return
     */
    public static List<Coordinate> getExtremePoints(Geometry geometry) {
        List<Coordinate> extremePoints = new ArrayList<>();
 
        Envelope envelope = geometry.getEnvelopeInternal();
 
        // 获取4个点经纬度
        extremePoints.add(new Coordinate(envelope.getMinX(), envelope.getMaxY()));
        extremePoints.add(new Coordinate(envelope.getMinX(), envelope.getMinY()));
        extremePoints.add(new Coordinate(envelope.getMaxX(), envelope.getMinY()));
        extremePoints.add(new Coordinate(envelope.getMaxX(), envelope.getMaxY()));
 
        return extremePoints;
    }
 
    /**
     * 航线点排序
     *
     * @return
     */
    public static Coordinate[] getRoutePointOrder(List<LotInfo> list, double airportLat, double airportLon) {
        List<Coordinate> centros = new ArrayList<>();
        List<List<Coordinate>> extremePoints = new ArrayList<>();
        Map<Coordinate, List<Coordinate>> points = new HashMap<>();
        list.forEach(patche -> {
            String wkt = patche.getDkfw();
            // 解析WKT字符串为多边形
            WKTReader wktReader = new WKTReader(JTSFactoryFinder.getGeometryFactory());
            Geometry polygon = null;
            try {
                polygon = wktReader.read(wkt);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
 
            // 开始封装中心点坐标
            Coordinate coordinateCentro = getCentro(polygon);
            centros.add(coordinateCentro);
 
            // 开始封装图斑4个航线
            List<Coordinate> coordinatePointList = getExtremePoints(polygon);
            extremePoints.add(coordinatePointList);
            points.put(coordinateCentro, coordinatePointList);
 
        });
 
        // 开始排序
        Coordinate[] coordinates = new Coordinate[centros.size() + 1];
        List<Coordinate> coordinateList = new ArrayList<>();
        coordinates[0] = new Coordinate(airportLon, airportLat); //第一个为机场经纬度
        coordinateList.add(new Coordinate(airportLon, airportLat)); //第一个为机场经纬度)
        for (int i = 1; i < centros.size() + 1; i++) {
            coordinates[i] = centros.get(i - 1);
            coordinateList.add(centros.get(i - 1));
        }
 
        // 对中心坐标数组进行排序
        Coordinate[] retCoordinate = new Coordinate[coordinateList.size()];
        Coordinate[] sortedCoordinates = DistanceCalculator.sortByDistance(coordinateList, airportLat, airportLon, 0, retCoordinate);
 
        // 开始拼接图斑点位--按中心坐标数组排序顺序
        // 长度-3第一个起点只有一个点位
        Coordinate[] retc = new Coordinate[sortedCoordinates.length * 4 - 3];
        // 赋值起点
        retc[0] = sortedCoordinates[0];
        int i = 0;
        for (Coordinate num : sortedCoordinates) {
            List<Coordinate> coordinatePoints = points.get(num);
            if (null != coordinatePoints) {
                for (int j = 0; j < coordinatePoints.size(); j++) {
                    // 开始拼接
                    retc[i * coordinatePoints.size() + j - 3] = coordinatePoints.get(j);
                }
            }
 
            i++;
        }
 
        return retc;
 
 
    }
 
}