From c5c27e7fdbee0d23264558f6abba46f3bdbbe881 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Mon, 31 Jul 2023 15:47:16 +0800
Subject: [PATCH] chore:解决经纬度丢失问题

---
 screen-api/src/main/java/com/moral/api/controller/UAVController.java |  139 ++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 127 insertions(+), 12 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/controller/UAVController.java b/screen-api/src/main/java/com/moral/api/controller/UAVController.java
index e7ad570..1e969e9 100644
--- a/screen-api/src/main/java/com/moral/api/controller/UAVController.java
+++ b/screen-api/src/main/java/com/moral/api/controller/UAVController.java
@@ -8,6 +8,7 @@
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.moral.api.entity.HistorySecondUav;
 import com.moral.api.mapper.HistorySecondUavMapper;
+import com.moral.api.pojo.dto.uav.UAVGetBD;
 import com.moral.api.pojo.dto.uav.UAVGteForDTO;
 import com.moral.api.pojo.dto.uav.UAVQueryTimeSlotDTO;
 import com.moral.api.pojo.dto.uav.UAVResultDTO;
@@ -88,17 +89,30 @@
         ArrayList<Double> flyLonList = new ArrayList<>();
 
         List<HistorySecondUav> historySecondUavs = historySecondUavMapper.reList(params);
+        ArrayList<UAVGetBD> rsBDList = new ArrayList<>();
         for (HistorySecondUav historySecondUav : historySecondUavs) {
+            UAVGetBD uavGetBD = new UAVGetBD();
             String value = historySecondUav.getValue();
             JSONObject jsonObject = JSONObject.parseObject(value);
+
             String flylon = jsonObject.get("flylon").toString();
             String flylat = jsonObject.get("flylat").toString();
-            flyLatList.add(Double.parseDouble(flylat));
-            flyLonList.add(Double.parseDouble(flylon));
+            //WGS84 ��� ������������
+            double[] doubles = transformWGS84ToBD09(Double.parseDouble(flylon), Double.parseDouble(flylat));
+            uavGetBD.setFlyLon(doubles[0]);
+            uavGetBD.setFlyLat(doubles[1]);
+            uavGetBD.setTime(DateUtils.dateToDateString(historySecondUav.getTime()));
+            uavGetBD.setValue(value);
+            rsBDList.add(uavGetBD);
+//            flyLatList.add(Double.parseDouble(flylat));
+//            flyLonList.add(Double.parseDouble(flylon));
+            flyLatList.add(doubles[1]);
+            flyLonList.add(doubles[0]);
         }
         if (ObjectUtils.isEmpty(historySecondUavs)){
             return new ResultMessage(ResponseCodeEnum.TARGET_IS_NULL,"null");
         }
+        //������������������������������
         Double maxLat = Collections.max(flyLatList);
         Double maxLon = Collections.max(flyLonList);
         Double minLat = Collections.min(flyLatList);
@@ -168,19 +182,30 @@
             Double y1 = Double.parseDouble(leftTops[1]);
             Double y2 = Double.parseDouble(rightBottoms[1]);
             //������������������
-            for (int i = 0; i < historySecondUavs.size(); i++) {
-                HistorySecondUav historySecondUav = historySecondUavs.get(i);
-                String value = historySecondUav.getValue();
-                Map map1 = JSON.parseObject(value, Map.class);
-                String flylat = map1.get("flylat").toString();
-                String flylon = map1.get("flylon").toString();
-                //���������������������������
-                boolean flag = isInPolygon(flylon,flylat,x1,x2,y1,y2);
+//            for (int i = 0; i < historySecondUavs.size(); i++) {
+//                HistorySecondUav historySecondUav = historySecondUavs.get(i);
+//                String value = historySecondUav.getValue();
+//                Map map1 = JSON.parseObject(value, Map.class);
+//                String flylat = map1.get("flylat").toString();
+//                String flylon = map1.get("flylon").toString();
+//                //���������������������������
+//                boolean flag = isInPolygon(flylon,flylat,x1,x2,y1,y2);
+//                if (flag){
+//                    String time = DateUtils.dateToDateString(historySecondUav.getTime());
+//                    //historySecondUavs.remove(i);
+//                    if(!stringList.contains(time)){
+//                        list1.add(historySecondUav.getValue());
+//                        stringList.add(time);
+//                    }
+//                }
+//            }
+            for (UAVGetBD uavGetBD : rsBDList) {
+                boolean flag = isInPolygon(uavGetBD.getFlyLon().toString(),uavGetBD.getFlyLat().toString(),x1,x2,y1,y2);
                 if (flag){
-                    String time = DateUtils.dateToDateString(historySecondUav.getTime());
+                    String time = uavGetBD.getTime();
                     //historySecondUavs.remove(i);
                     if(!stringList.contains(time)){
-                        list1.add(historySecondUav.getValue());
+                        list1.add(uavGetBD.getValue());
                         stringList.add(time);
                     }
                 }
@@ -457,7 +482,13 @@
         double rsLon = Lon * 180 / Math.PI;
         double rsLat = Lat * 180 / Math.PI;
 
+//        double[] doubles = transformWGS84ToGCJ02(rsLon, rsLat);
+//        String s = transformGCJ02ToBD09(doubles[0], doubles[1]);
+
+
         return decimalFormat.format(rsLon)+"_"+decimalFormat.format(rsLat);
+//        return rsLon+"_"+rsLat;
+//        return s;
     }
 
 
@@ -625,4 +656,88 @@
 
     }
 
+    private static final double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
+    private static final double PI = 3.1415926535897932384626;
+    private static final double a = 6378245.0;
+    private static final double ee = 0.00669342162296594323;
+    /**
+     * WGS84 ������ ��� GCJ02
+     *
+     * @param lng ������
+     * @param lat ������
+     * @return GCJ02 ���������[���������������]
+     */
+    public static double[] transformWGS84ToGCJ02(double lng, double lat) {
+        if (outOfChina(lng, lat)) {
+            return new double[]{lng, lat};
+        } else {
+            double dLat = transformLat(lng - 105.0, lat - 35.0);
+            double dLng = transformLng(lng - 105.0, lat - 35.0);
+            double redLat = lat / 180.0 * PI;
+            double magic = Math.sin(redLat);
+            magic = 1 - ee * magic * magic;
+            double sqrtMagic = Math.sqrt(magic);
+            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
+            dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(redLat) * PI);
+            double mgLat = lat + dLat;
+            double mgLng = lng + dLng;
+            return new double[]{mgLng, mgLat};
+        }
+    }
+
+    private static double transformLat(double lng, double lat) {
+        double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
+        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
+        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
+        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
+        return ret;
+    }
+
+    private static double transformLng(double lng, double lat) {
+        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
+        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
+        ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
+        ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
+        return ret;
+    }
+    /**
+     * ������������������������������
+     *
+     * @param lng ������
+     * @param lat ������
+     * @return ���������������������
+     */
+    public static boolean outOfChina(double lng, double lat) {
+        return (lng < 72.004 || lng > 137.8347) || (lat < 0.8293 || lat > 55.8271);
+    }
+
+    /**
+     * GCJ02 ���������������
+     *
+     * @param lng GCJ02 ������
+     * @param lat GCJ02 ������
+     * @return ���������������[���������������]
+     */
+    public static double[] transformGCJ02ToBD09(double lng, double lat) {
+        double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
+        double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
+        double bd_lng = z * Math.cos(theta) + 0.0065;
+        double bd_lat = z * Math.sin(theta) + 0.006;
+        return new double[]{bd_lng, bd_lat};
+    }
+
+
+    /**
+     * WGS84 ��� ������������BD09
+     *
+     * @param lng ������
+     * @param lat ������
+     * @return BD09 ���������[���������������]
+     */
+    public static double[] transformWGS84ToBD09(double lng, double lat) {
+        double[] lngLat = transformWGS84ToGCJ02(lng, lat);
+
+        return transformGCJ02ToBD09(lngLat[0], lngLat[1]);
+    }
+
 }

--
Gitblit v1.8.0