feiyu02
2025-10-21 eb3dd00b0b7fcda477229d518d250f9c842b790b
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.flightfeather.uav.biz.report
 
import com.flightfeather.uav.biz.sourcetrace.model.PollutedClue
import com.flightfeather.uav.common.utils.MapUtil
import com.flightfeather.uav.domain.entity.SceneInfo
import com.flightfeather.uav.domain.entity.avg
import com.flightfeather.uav.socket.eunm.AggregatedFactorType
import com.flightfeather.uav.socket.eunm.FactorType
 
/**
 * 走航典型隐患区域
 * @date 2025/8/25 14:18
 * @author feiyu
 */
class MissionRiskArea {
 
    // 按区域分类的污染线索
    class ClueByArea {
        var sceneInfo: SceneInfo? = null
        // 所属街镇
        var township:String? = null
        // 格式化地址
        var address: String? = null
        // 交叉路段信息
        var roadinter:String? = null
        var clueByFactorList: MutableList<ClueByFactor>? = null
    }
    // 按因子分类的污染线索
    class ClueByFactor {
        var factor: String? = null
        var clues: MutableList<PollutedClue>? = null
    }
 
    // 分类线索
    class ClassifyClue {
        // 按所在街镇分类
        var township:String? = null
        // 按因子类型分类
        var factorTag: String? = null
        // 实际包含的因子类型
        var factors: List<FactorType>? = null
        // 按所在道路分类
        var street: String? = null
        // 线索
        var clue: PollutedClue? = null
    }
 
    /**
     * 生成走航典型隐患区域,根据关键场景列表进行分组
     * @param keyScenes 关键场景列表
     * @param pollutedClues 污染线索列表
     * @return 按区域和因子分组的污染线索
     */
    fun generateClueByKeyRiskScene(keyScenes: List<SceneInfo?>, pollutedClues: List<PollutedClue?>): List<ClueByArea> {
        val result = mutableListOf<ClueByArea>()
        
        pollutedClues.forEach { pollutedClue ->
            if (pollutedClue == null) return@forEach
            val dataList = pollutedClue.pollutedData?.dataList ?: emptyList()
            if (dataList.isEmpty()) return@forEach
            
            // 计算单个PollutedClue的均值经纬度
            val avgData = dataList.avg()
            val wgs84Lng = avgData.longitude?.toDouble() ?: return@forEach
            val wgs84Lat = avgData.latitude?.toDouble() ?: return@forEach
            
            // 坐标转换
            val gcj02Point = MapUtil.wgs84ToGcj02(wgs84Lng to wgs84Lat)
            
            // 查找最近场景
            var minDistance = Double.MAX_VALUE
            var closestScene: SceneInfo? = null
            keyScenes.forEach { scene ->
                scene?.let { s ->
                    val sceneLng = s.longitude?.toDouble() ?: return@let
                    val sceneLat = s.latitude?.toDouble() ?: return@let
                    val distance = MapUtil.getDistance(gcj02Point.first, gcj02Point.second, sceneLng, sceneLat)
                    if (distance < minDistance) {
                        minDistance = distance
                        closestScene = s
                    }
                }
            }
            
            // 按场景和因子分组线索
            closestScene?.let { scene ->
                var clueByArea = result.find { it.sceneInfo?.guid == scene.guid }
                if (clueByArea == null) {
                    clueByArea = ClueByArea().apply {
                        this.sceneInfo = scene
                        this.clueByFactorList = mutableListOf()
                    }
                    result.add(clueByArea)
                }
 
                val firstFactorType = pollutedClue.pollutedData?.statisticMap?.keys?.first()
                val afType =  AggregatedFactorType.getAFType(firstFactorType)
                val factorName = afType?.des ?: firstFactorType?.des
                var clueByFactor = clueByArea.clueByFactorList?.find { it.factor == factorName }
                if (clueByFactor == null) {
                    clueByFactor = ClueByFactor().apply {
                        this.factor = factorName
                        this.clues = mutableListOf()
                    }
                    clueByArea.clueByFactorList?.add(clueByFactor)
                }
                
                clueByFactor.clues?.add(pollutedClue)
            }
        }
        
        return result
    }
 
    /**
     * 生成走航典型隐患区域,根据污染线索溯源地址进行分组
     * @param pollutedClues 污染线索列表
     * @return 按区域和因子分组的污染线索
     */
    fun generateClueByRiskArea(pollutedClues: List<PollutedClue?>): List<ClassifyClue> {
        val clues =  pollutedClues.map { pollutedClue ->
            ClassifyClue().apply {
                this.township = pollutedClue?.pollutedArea?.township
 
                val factorTypes = pollutedClue?.pollutedData?.statisticMap?.keys ?: emptyList()
                val firstFactorType = factorTypes.firstOrNull()
                // 聚合因子类型
                val afType =  AggregatedFactorType.getAFType(firstFactorType)
                this.factorTag = afType?.des ?: firstFactorType?.des
                this.factors = factorTypes.toList()
                // 按所在道路分类
                this.street = pollutedClue?.pollutedArea?.street
 
                this.clue = pollutedClue
            }
        }
        // 统计每条道路下的线索数量
        val streetClueCount = clues.groupingBy { it.street }.eachCount()
        // 筛选出道路下线索数量超过1条的
        val validStreets = streetClueCount.filter { it.value > 1 }.keys
        // 筛选出有效道路下的线索
        val validClues = clues.filter { it.street in validStreets }
        return validClues
 
 
 
//        pollutedClues.forEach { pollutedClue ->
//            if (pollutedClue == null) return@forEach
//            val dataList = pollutedClue.pollutedData?.dataList ?: emptyList()
//            if (dataList.isEmpty()) return@forEach
//
//            // 按污染溯源地址街镇和因子分组线索
//            pollutedClue.pollutedArea?.township?.let { township ->
//                var clueByArea = result.find { it.township == township }
//                if (clueByArea == null) {
//                    clueByArea = ClueByArea().apply {
//                        this.township = township
//                        this.address = pollutedClue.pollutedArea?.address
//                        this.roadinter = pollutedClue.pollutedArea?.roadinter
//                        this.clueByFactorList = mutableListOf()
//                    }
//                    result.add(clueByArea)
//                }
//
//                val firstFactorType = pollutedClue.pollutedData?.statisticMap?.keys?.first()
//                // 聚合因子类型
////                val afType =  AggregatedFactorType.getAFType(firstFactorType)
////                val factorName = afType?.des ?: firstFactorType?.des
//                // 使用直接的因子类型
//                val factorName = firstFactorType?.des ?: ""
//                var clueByFactor = clueByArea.clueByFactorList?.find { it.factor == factorName }
//                if (clueByFactor == null) {
//                    clueByFactor = ClueByFactor().apply {
//                        this.factor = factorName
//                        this.clues = mutableListOf()
//                    }
//                    clueByArea.clueByFactorList?.add(clueByFactor)
//                }
//
//                clueByFactor.clues?.add(pollutedClue)
//            }
//        }
 
//        return result
    }
}