From db447bb757b51f8d03e62d6ae4f183b4608723ef Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期五, 07 三月 2025 17:10:43 +0800
Subject: [PATCH] 卫星遥测系统相关接口新增及调整

---
 src/main/kotlin/com/flightfeather/uav/biz/satellite/SatelliteGridManage.kt |  156 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 144 insertions(+), 12 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/biz/satellite/SatelliteGridManage.kt b/src/main/kotlin/com/flightfeather/uav/biz/satellite/SatelliteGridManage.kt
index 783ceb6..bea89eb 100644
--- a/src/main/kotlin/com/flightfeather/uav/biz/satellite/SatelliteGridManage.kt
+++ b/src/main/kotlin/com/flightfeather/uav/biz/satellite/SatelliteGridManage.kt
@@ -1,7 +1,7 @@
 package com.flightfeather.uav.biz.satellite
 
 import com.flightfeather.uav.common.utils.MapUtil
-import com.flightfeather.uav.domain.entity.GridCell
+import com.flightfeather.uav.domain.entity.*
 import kotlin.math.PI
 import kotlin.math.sqrt
 
@@ -17,7 +17,9 @@
     /**
      * 鏍规嵁姝f柟褰㈢綉鏍间腑蹇冪偣鍧愭爣锛岃绠�4涓《鐐瑰潗鏍�
      * 缃戞牸涓績鐐瑰潗鏍囨寜鐓т粠宸﹀埌鍙炽�佷粠涓婂埌涓嬬殑椤哄簭鎺掑垪
+     * @date 2025.1.8
      * @param points 缃戞牸涓績鍧愭爣鐐规暟缁�
+     * @return 缃戞牸4涓《鐐圭粡绾害鍧愭爣
      */
     fun calGridVertex(points: List<Pair<Double, Double>>): List<GridVertex> {
         // 缃戞牸灏戜簬2涓紝鍒欐棤娉曠粯鍒�
@@ -66,10 +68,11 @@
     /**
      * 鎷嗗垎缃戞牸涓虹粏鍒嗙綉鏍硷紝鎵�鏈夌綉鏍煎簲璇ユ槸鐩稿悓杈归暱鐨勬鏂瑰舰
      * 鏍规嵁鐩镐技鐭╁舰鐨勫師鐞嗭紝鍙互鍒嗗埆鎸夋瘮渚嬪緱鍒版瘡涓粏鍒嗙綉鏍肩殑缁忕含搴�
+     * @date 2025.1.17
      * @param gridCellList 鍘熷缃戞牸鏁扮粍
      * @param scale 鎷嗗垎鐨勭郴鏁帮紝渚嬪 2锛岃〃绀哄皢鍘熸湁缃戞牸鎸夎竟闀跨殑 1/2 鎷嗗垎鎴� 2 * 2 鐨�4涓綉鏍�
      * @param groupId 缁嗗垎鍚庣殑缃戞牸鎵�灞炵殑缃戞牸缁刬d
-     * @return
+     * @return 缁嗗垎缃戞牸
      */
     fun splitGrid(gridCellList: List<GridCell?>, scale: Int, groupId:Int): List<GridCell?> {
         if (scale <= 0) throw IllegalArgumentException("缃戞牸鎷嗗垎鐨勬暟閲忎笉鑳藉皬浜�1")
@@ -87,15 +90,15 @@
         val p2 = p.point2Lon to p.point2Lat
         val p3 = p.point3Lon to p.point3Lat
         val p4 = p.point4Lon to p.point4Lat
-        // p1銆乸3鐨勭粡绾害鍗曚綅宸��
-        val dx1 = (p3.first - p1.first) / scale.toBigDecimal()
-        val dy1 = (p3.second - p1.second) / scale.toBigDecimal()
+        // p1銆乸4鐨勭粡绾害鍗曚綅宸��
+        val dx1 = (p4.first - p1.first) / scale.toBigDecimal()
+        val dy1 = (p4.second - p1.second) / scale.toBigDecimal()
         // p1銆乸2鐨勭粡绾害鍗曚綅宸��
         val dx2 = (p2.first - p1.first) / scale.toBigDecimal()
         val dy2 = (p2.second - p1.second) / scale.toBigDecimal()
-        // p3銆乸4鐨勭粡绾害鍗曚綅宸��
-        val dx3 = (p4.first - p3.first) / scale.toBigDecimal()
-        val dy3 = (p4.second - p3.second) / scale.toBigDecimal()
+        // p4銆乸3鐨勭粡绾害鍗曚綅宸��
+        val dx3 = (p3.first - p4.first) / scale.toBigDecimal()
+        val dy3 = (p3.second - p4.second) / scale.toBigDecimal()
         // 涓績鐐瑰拰p1鐨勭粡绾害鍗曚綅宸��
         val dxC = (p.longitude - p1.first) / scale.toBigDecimal()
         val dyC = (p.latitude - p1.second) / scale.toBigDecimal()
@@ -116,14 +119,14 @@
                 newGridCell1.point1Lon = g.point1Lon + dx1 * row.toBigDecimal()
                 newGridCell1.point1Lat = g.point1Lat + dy1 * row.toBigDecimal()
                 // 宸︿笅瑙掗《鐐规牴鎹墍鍦ㄨ鏁板湪鍘熷缃戞牸椤剁偣鍩虹涓婂鍔犲亸绉婚噺锛堟瘮宸︿笂瑙掗《鐐瑰涓�涓亸绉伙級
-                newGridCell1.point3Lon = g.point1Lon + dx1 * (row + 1).toBigDecimal()
-                newGridCell1.point3Lat = g.point1Lat + dy1 * (row + 1).toBigDecimal()
+                newGridCell1.point4Lon = g.point1Lon + dx1 * (row + 1).toBigDecimal()
+                newGridCell1.point4Lat = g.point1Lat + dy1 * (row + 1).toBigDecimal()
                 // 鍙充笂瑙掗《鐐瑰湪缁嗗垎缃戞牸宸︿笂瑙掔殑鍩虹涓婂鍔犵浉搴旂殑鍋忕Щ閲�
                 newGridCell1.point2Lon = newGridCell1.point1Lon + dx2
                 newGridCell1.point2Lat = newGridCell1.point1Lat + dy2
                 // 鍙充笅瑙掗《鐐瑰湪缁嗗垎缃戞牸宸︿笅瑙掔殑鍩虹涓婂鍔犵浉搴旂殑鍋忕Щ閲�
-                newGridCell1.point4Lon = newGridCell1.point3Lon + dx3
-                newGridCell1.point4Lat = newGridCell1.point3Lat + dy3
+                newGridCell1.point3Lon = newGridCell1.point4Lon + dx3
+                newGridCell1.point3Lat = newGridCell1.point4Lat + dy3
                 // 涓績鐐瑰湪缁嗗垎缃戞牸宸︿笂瑙掔殑鍩虹涓婂鍔犲浐瀹氬亸绉婚噺
                 newGridCell1.longitude = newGridCell1.point1Lon + dxC
                 newGridCell1.latitude = newGridCell1.point1Lat + dyC
@@ -160,4 +163,133 @@
         return newGridCellList
     }
 
+    /**
+     * 鎷嗗垎鏁版嵁锛屽皢鍘熷鍗槦缃戞牸閬ユ祴鏁版嵁鏄犲皠鍒板搴旂粏鍒嗙綉鏍间笂
+     * @date 2025.2.7
+     * @param subGridCellList 缁嗗垎缃戞牸
+     * @param subGridData 缁嗗垎缃戞牸瀵瑰簲鐨勬暟鎹储寮�
+     * @param originGridDataDetailList 缁嗗垎缃戞牸鎵�灞炵綉鏍肩殑鍘熷缃戞牸鏁版嵁
+     * @return 鏄犲皠鍚庣殑缁嗗垎缃戞牸閬ユ祴鏁版嵁
+     */
+    fun splitData(
+        subGridCellList: List<GridCell?>, subGridData: GridData, originGridDataDetailList: List<GridDataDetail?>
+    ): List<GridDataDetail> {
+        if (subGridCellList.isEmpty() || originGridDataDetailList.isEmpty()) return emptyList()
+
+        val result = mutableListOf<GridDataDetail>()
+
+        // 灏嗙粏鍒嗙綉鏍兼寜鐓х埗缃戞牸id鍜岃嚜韬綉鏍糹d杩涜鍗囧簭鎺掑垪
+        val _subGridCellList = subGridCellList.sortedWith(Comparator { o1, o2 ->
+            if (o1 == null && o2 == null) {
+                return@Comparator 0
+            } else if (o1 == null) {
+                return@Comparator -1
+            } else if (o2 == null) {
+                return@Comparator 1
+            } else {
+                if (o1.fatherCellIndex == o2.fatherCellIndex) {
+                    return@Comparator o1.cellIndex - o2.cellIndex
+                } else {
+                    return@Comparator o1.fatherCellIndex - o2.fatherCellIndex
+                }
+            }
+        })
+
+        // 灏嗗師濮嬬綉鏍兼暟鎹寜鐓х綉鏍糹d鍗囧簭鎺掑垪
+        val _originGridDataDetailIterator = originGridDataDetailList.sortedBy { it?.cellId }.iterator()
+        var fatherGridData = _originGridDataDetailIterator.next()
+
+        // 閬嶅巻缁嗗垎缃戞牸锛屼负姣忎釜缁嗗垎缃戞牸鐢熸垚涓�鏉$綉鏍兼暟鎹�
+        _subGridCellList.forEach {
+            while (fatherGridData?.cellId != it?.fatherCellIndex && _originGridDataDetailIterator.hasNext()) {
+                fatherGridData = _originGridDataDetailIterator.next()
+            }
+            val subGridDataDetail = GridDataDetail().apply {
+                dataId = subGridData.id
+                groupId = it?.groupId
+                cellId = it?.cellIndex
+                pm25 = fatherGridData?.pm25
+                rank = fatherGridData?.rank
+            }
+
+            result.add(subGridDataDetail)
+        }
+
+        return result
+    }
+
+    /**
+     * 璧拌埅鏁版嵁鍜屽崼鏄熺綉鏍艰瀺鍚�
+     * 鏁版嵁铻嶅悎閲囩敤鍧囧�兼柟寮忕粺璁★紙鏆傛椂锛�
+     * @date 2025.2.7
+     * @param realTimeDataList 寰呰瀺鍚堢殑璧拌埅鐩戞祴鏁版嵁
+     * @param gridData 铻嶅悎鍚庣殑鏁版嵁缁勭储寮�
+     * @param gridCellList 寰呰瀺鍚堢殑鍗槦缃戞牸
+     * @return 铻嶅悎鍚庣殑缃戞牸鐩戞祴鏁版嵁
+     */
+    fun dataFusion(
+        realTimeDataList: List<BaseRealTimeData>,
+        gridData: GridData?,
+        gridCellList: List<GridCell?>,
+    ): List<GridDataDetail> {
+        // 閬嶅巻璧拌埅鐩戞祴鏁版嵁锛岃绠楁瘡涓偣鎵�鍦ㄧ綉鏍�
+        val dataMap = mutableMapOf<GridCell, MutableList<BaseRealTimeData>>()
+        realTimeDataList.forEach {
+            if (it.longitude == null || it.latitude == null) return@forEach
+
+            searchGirdIn(it.longitude!!.toDouble() to it.latitude!!.toDouble(), gridCellList)?.let { cell ->
+                if (!dataMap.containsKey(cell)) {
+                    dataMap[cell] = mutableListOf()
+                }
+                dataMap[cell]?.add(it)
+            }
+        }
+
+        // 缁熻姣忎釜缃戞牸涓殑鍧囧��
+        // Fixme 2025.2.20 鏆傛椂榛樿浠ュ潎鍊兼柟寮忕粺璁★紝鍚庣画璋冩暣涓哄绉嶆柟寮忓苟鏀寔鐢ㄦ埛閫夋嫨
+        val gridDataDetailList = mutableListOf<GridDataDetail>()
+        dataMap.forEach { (k, v) ->
+            val avgData = v.avg()
+            val dataDetail = GridDataDetail().apply {
+                dataId = gridData?.id
+                groupId = k.groupId
+                cellId = k.cellIndex
+                pm25 = avgData.pm25
+                rank
+            }
+            gridDataDetailList.add(dataDetail)
+        }
+
+        gridDataDetailList.sortBy { it.pm25 }
+        gridDataDetailList.forEachIndexed { index, d ->
+            d.rank = index + 1
+        }
+        gridDataDetailList.sortBy { it.cellId }
+
+        return gridDataDetailList
+    }
+
+    /**
+     * 璁$畻鍧愭爣鐐瑰湪鍝釜鍗槦缃戞牸鍐�
+     * @date 2025.2.14
+     * @param point 鍧愭爣鐐�
+     * @param gridCellList 鍗槦缃戞牸
+     */
+    fun searchGirdIn(point: Pair<Double, Double>, gridCellList: List<GridCell?>): GridCell? {
+        for (i in gridCellList.indices) {
+            val gridCell = gridCellList[i] ?: continue
+            val polygon = listOf(
+                gridCell.point1Lon.toDouble() to gridCell.point1Lat.toDouble(),
+                gridCell.point2Lon.toDouble() to gridCell.point2Lat.toDouble(),
+                gridCell.point3Lon.toDouble() to gridCell.point3Lat.toDouble(),
+                gridCell.point4Lon.toDouble() to gridCell.point4Lat.toDouble(),
+            )
+            if (MapUtil.isPointInPolygon(point, polygon)) {
+                return gridCell
+            }
+        }
+
+        return null
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3