rain
2024-08-21 2db1aa88e8ab53096a936163d686b90d8e056a99
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
package com.dji.sample.droneairport.utils;
 
import com.dji.sample.droneairport.model.dto.Airport;
import com.dji.sample.droneairport.model.dto.AirportTasks;
import com.dji.sample.patches.model.entity.LotInfo;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.WKTReader;
 
import java.util.ArrayList;
import java.util.List;
 
public class TaskAllocator {
 
    private static final GeometryFactory geometryFactory = new GeometryFactory();
 
    // 计算机场到多边形的最短距离
    public static double calculateDistanceToPolygon(double airportLat, double airportLon, Geometry polygon) {
        Coordinate airportCoord = new Coordinate(airportLon, airportLat);
        return polygon.distance(geometryFactory.createPoint(airportCoord));
    }
 
    // 为每个图斑面分配任务
    public static List<AirportTasks> assignTasks(List<Airport> airports, List<LotInfo> lotInfos) throws Exception {
        // 创建集合用于存储每个机场的任务
        List<AirportTasks> airportTasksList = new ArrayList<>();
        for (Airport airport : airports) {
            airportTasksList.add(new AirportTasks(airport, new ArrayList<>()));
        }
 
        WKTReader reader = new WKTReader(geometryFactory);
 
        // 存储每个图斑面分配的机场信息
        List<String> polygonAssignments = new ArrayList<>();
 
        for (LotInfo lotInfo : lotInfos) {
            String polygonWKT = lotInfo.getDkfw();
            if (polygonWKT == null || polygonWKT.trim().isEmpty()) {
                polygonAssignments.add("Polygon data is missing for LotInfo ID: " + lotInfo.getId());
                continue;
            }
 
            Geometry polygon = reader.read(polygonWKT);
            double minDistance = Double.MAX_VALUE;
            int closestAirportIndex = -1;
 
            for (int i = 0; i < airports.size(); i++) {
                Airport airport = airports.get(i);
                double distanceToAirport = calculateDistanceToPolygon(airport.getLat(), airport.getLon(), polygon);
 
                if (distanceToAirport < minDistance) {
                    minDistance = distanceToAirport;
                    closestAirportIndex = i;
                }
            }
 
            if (closestAirportIndex != -1) {
                AirportTasks airportTasks = airportTasksList.get(closestAirportIndex);
                airportTasks.getTasks().add(lotInfo);
                Airport closestAirport = airports.get(closestAirportIndex);
                polygonAssignments.add("Polygon: " + polygonWKT + " assigned to Airport at (" + closestAirport.getLat() + ", " + closestAirport.getLon() + ")");
            } else {
                polygonAssignments.add("Polygon: " + polygonWKT + " has no available airport");
            }
        }
        return airportTasksList;
    }
    public static void main(String[] args) {
        // 创建测试数据
        List<Airport> airports = new ArrayList<>();
        airports.add(new Airport("4a574db8-4ad3-48f7-9f16-3edbcd8056e1","",28.62452712442823, 115.85666327144976)); // 机场1
        airports.add(new Airport("8520933f-31cd-4681-a262-57fe4d79b8b6","",28.630851, 115.856088)); // 机场2
        airports.add(new Airport("607e0666-d748-4504-a459-09774286dc8b","",28.623514, 115.865406)); // 机场3
        airports.add(new Airport("e3dea0f5-37f2-4d79-ae58-490af3228069","",25.8917271933111, 116.02094292569765)); // 机场4
 
        List<LotInfo> lotInfos = new ArrayList<>();
        lotInfos.add(new LotInfo(1, "", "bsm1", "dklx1", "xzqdm1", "xmc1", "dkbh1", "dkmc1", 10.0, "sfbhzdk1", 28.63450370457647, 115.84499139166738, "bz1", "kzxx1", "MULTIPOLYGON(((115.84499139166738 28.63450370457647,115.84416828981072 28.634958404355842,115.84485338292909 28.63573150645547,115.84571474781323 28.63530320347431,115.84499139166738 28.63450370457647)))", "sjlx1", 1, "taskName1", "taskId1", 1, 1, 0, "userName1", System.currentTimeMillis(), System.currentTimeMillis()));
        lotInfos.add(new LotInfo(2, "", "bsm2", "dklx2", "xzqdm2", "xmc2", "dkbh2", "dkmc2", 20.0, "sfbhzdk2", 28.63698795307923, 115.85921771312906, "bz2", "kzxx2", "MULTIPOLYGON(((115.85921771312906 28.63698795307923,115.85833969201798 28.635860088562403,115.85719041235207 28.63655251171517,115.8580684334633 28.637666099465918,115.85921771312906 28.63698795307923)))", "sjlx2", 0, "taskName2", "taskId2", 0, 1, 0, "userName2", System.currentTimeMillis(), System.currentTimeMillis()));
        // 添加更多 LotInfo 实例
 
        try {
            // 调用 assignTasks 方法
            List<AirportTasks> result = TaskAllocator.assignTasks(airports, lotInfos);
 
            // 打印结果
            for (AirportTasks airportTasks : result) {
                Airport airport = airportTasks.getAirport();
                System.out.println("任务分配给机场 (" + airport.getLat() + ", " + airport.getLon() + "):");
                for (LotInfo lotInfo : airportTasks.getTasks()) {
                    System.out.println("LotInfo ID: " + lotInfo.getId() + ", dkfw: " + lotInfo.getDkfw());
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}