feiyu02
2024-07-04 022af485fbd77bc3d6b01f9f779248b3c189dad2
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
package com.flightfeather.uav.biz.dataprocess
 
import com.flightfeather.uav.common.location.TrackSegment
import com.flightfeather.uav.common.net.AMapService
import com.flightfeather.uav.domain.entity.BaseRealTimeData
import com.flightfeather.uav.domain.entity.Mission
import com.flightfeather.uav.domain.entity.SegmentInfo
import com.flightfeather.uav.domain.entity.avg
import com.flightfeather.uav.domain.repository.RealTimeDataRep
import com.flightfeather.uav.domain.repository.SegmentInfoRep
import org.springframework.stereotype.Component
import java.math.BigDecimal
 
/**
 * 走航轨迹按照路段进行分割
 * @date 2024/7/4
 * @author feiyu02
 */
@Component
class RoadSegment(
    private val realTimeDataRep: RealTimeDataRep,
    private val segmentInfoRep: SegmentInfoRep,
) {
 
    fun handle(mission: Mission) {
        // 获取数据分段
        val data = mutableListOf<BaseRealTimeData>()
        realTimeDataRep.fetchData(mission).forEach {
            // 去除无效经纬度
            if ((it.longitude != null && it.longitude != BigDecimal.ZERO)
                && (it.latitude != null && it.latitude != BigDecimal.ZERO)
            ) {
                data.add(it)
            }
        }
        val sData = TrackSegment.segmentWithRoad(data)
        // 根据每个分段的GPS坐标均值通过高德API转换为高德坐标
        val avgGPS = mutableListOf<Pair<Double, Double>>()
        sData.forEach {
            val d = it.avg()
            if (d.longitude != null && d.latitude != null) {
                avgGPS.add(d.longitude!!.toDouble() to d.latitude!!.toDouble())
            }
        }
        val gdGPS = AMapService.coordinateConvert(avgGPS)
        // 通过高德API查询坐标对应的路段
        val segmentInfoList = mutableListOf<SegmentInfo>()
        gdGPS.forEachIndexed { i, pair ->
            val address = AMapService.reGeo(pair)
            segmentInfoList.add(SegmentInfo().apply {
                missionCode = mission.missionCode
                deviceCode = mission.deviceCode
                startTime = sData[i][0].dataTime
                endTime = sData[i].last().dataTime
                provinceName = address.province
                cityCode = address.citycode
                districtCode = address.adcode
                districtName = address.district
                townCode = address.towncode
                towmName = address.township
                street = address.street
            })
        }
        // 结果入库
        segmentInfoRep.insert(segmentInfoList)
    }
}