package com.dji.sample.geo.utils; import com.dji.sample.geo.entity.Feature; import com.dji.sample.geo.entity.GeoJson; import com.dji.sample.wayline.model.param.FlyAreaParam; import com.dji.sample.wayline.model.param.PointPOJO; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.List; /** * 空间计算工具类 */ public class GeoUtils { /** * 默认地球半径 */ private static double EARTH_RADIUS = 6371000;//赤道半径(单位m) /** * 转化为弧度(rad) */ private static double rad(double d) { return d * Math.PI / 180.0; } /** * 计算中心经纬度与目标经纬度的距离(米) * * @param centerLon 中心精度 * @param centerLat 中心纬度 * @param targetLon 需要计算的精度 * @param targetLat 需要计算的纬度 * @return 米 */ public static double distance(double centerLon, double centerLat, double targetLon, double targetLat) { double jl_jd = 102834.74258026089786013677476285;// 每经度单位米; double jl_wd = 111712.69150641055729984301412873;// 每纬度单位米; double b = Math.abs((centerLat - targetLat) * jl_jd); double a = Math.abs((centerLon - targetLon) * jl_wd); return Math.sqrt((a * a + b * b)); } /** * 根据多个点计算中心点 * @param list * @return */ public static PointPOJO getCenterPoint(List list) { int total = list.size(); double X = 0, Y = 0, Z = 0; for (int i = 0; i < list.size(); i++) { PointPOJO point = list.get(i); double lat, lon, x, y, z; lon = point.getLon() * Math.PI / 180; lat = point.getLat() * Math.PI / 180; x = Math.cos(lat) * Math.cos(lon); y = Math.cos(lat) * Math.sin(lon); z = Math.sin(lat); X += x; Y += y; Z += z; } X = X / total; Y = Y / total; Z = Z / total; double Lon = Math.atan2(Y, X); double Hyp = Math.sqrt(X * X + Y * Y); double Lat = Math.atan2(Z, Hyp); PointPOJO centerPoint = new PointPOJO(); centerPoint.setLon( Lon * 180 / Math.PI); centerPoint.setLat(Lat * 180 / Math.PI); return centerPoint; } /** * 获取点区域并按从近到远排序 * @param flyAreaParam * @return */ public static List caculatePointList(FlyAreaParam flyAreaParam){ List> areaList = flyAreaParam.getAreaList(); PointPOJO dockPoint = flyAreaParam.getDockPoint(); Double radius = flyAreaParam.getRadius(); List centerPointList = new ArrayList<>(); //根据区域获取中心点 areaList.forEach(area -> { PointPOJO centerPoint = GeoUtils.getCenterPoint(area); centerPointList.add(centerPoint); }); //巡逻目标集合 List targetList = new ArrayList<>(); //获取所有中心点之后, centerPointList.forEach(centerPoint -> { //判断各个中心点和机场点的距离 double distance = GeoUtils.distance(dockPoint.getLon(), dockPoint.getLat(), centerPoint.getLon(), centerPoint.getLat()); //当距离小于半径时,添加到目标集合中 if (distance <= radius) { centerPoint.setDistance(distance); targetList.add(centerPoint); } }); //按距离从近到远排序 targetList.sort(Comparator.comparing(PointPOJO::getDistance)); return targetList; } /** * 根据geojson获取点区域并按从近到远排序 * @param geoJson * @return */ public static List caculatePointList(GeoJson geoJson,PointPOJO dockPoint,Double radius){ List features = geoJson.getFeatures(); List> areaList = new ArrayList<>(); features.forEach(feature -> { List list = new ArrayList<>(); List> coordinates = feature.getGeometry().getCoordinates().get(0); coordinates.forEach(coordinate ->{ PointPOJO pointPOJO = new PointPOJO(); pointPOJO.setLon(coordinate.get(0)); pointPOJO.setLat(coordinate.get(1)); list.add(pointPOJO); }); areaList.add(list); }); FlyAreaParam flyAreaParam = new FlyAreaParam(); flyAreaParam.setDockPoint(dockPoint); flyAreaParam.setRadius(radius); flyAreaParam.setAreaList(areaList); List pointPOJOS = caculatePointList(flyAreaParam); return pointPOJOS; } /** * 读取json文件 * @param path * @return */ public static GeoJson readJsonFile(String path){ try { File file = new File(path); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return objectMapper.readValue(file, GeoJson.class); } catch (IOException e) { e.printStackTrace(); return null; } } }