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;
|
|
|
}
|
|
}
|