zhongrj
2025-03-28 4ff3eee77b41466d08117970970d01be6e48ffe1
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { ref,reactive } from 'vue'
import * as Cesium from 'cesium'
import { analyzeKmzFile, XMLToJSON } from '@/utils/cesium/kmz.js'
import cesiumOperation from '@/utils/cesium-tsa'
import ImageTrailMaterial from '@/utils/cesium/ImageTrailMaterial'
import { getLnglatAltitude, getPositionsHeight, getLnglatDist, cartesian3Convert, getCenterPoint } from '@/utils/cesium/mapUtil'
 
// import { appointroutePlanning } from '@/api/RoutePlanning'
import { PatchesgetInfoById } from '@/api/patternSpot'
 
import { ElMessage } from 'element-plus'
 
import { useMyStore } from '@/store'
 
const { getEntityById, removeById, addLeftClickEvent, removeLeftClickEvent, flyTo } = cesiumOperation()
 
export function initPointWayLine () {
  const store = useMyStore()
 
  const state = reactive({
    // pinNum: 0,
    // polylineNum: 0,
    // PolygonNum: 0,
    // currentType: '',
    pointList: [],
    obtainWaypointData: [],
    droneCoordinates: null,
    longitude: 0, // 机场经纬度
    latitude: 0,
    clampToGroundshow: (store.state.common.mapSetting.visual == '2D' ? true : false)
  })
  async function pointWayline (route:any,longitude:any, latitude:any) {
    if (!route || !longitude) return
    state.longitude = longitude
    state.latitude = latitude
    const workspaceId = localStorage.getItem('bs_workspace_id') || store.state.index.selectedWorkSpaceId
    // 请求获取ktz地址
    // const { data: waylineUrl } = await appointroutePlanning(workspaceId, wayline?.id || route.id)
    // if (waylineUrl.code !== 0) return ElMessage.error(waylineUrl.message)
    // let textUrl = 'https://wrj.shuixiongit.com/minio/cloud-bucket//wayline/20250115/wayline_1736931502859.kmz'
    // const { fileInfoObj } = await analyzeKmzFile(`${waylineUrl.data}?_t=${new Date().getTime()}`)
    const { fileInfoObj } = await analyzeKmzFile(`${route}?_t=${new Date().getTime()}`)
    const xmlStr = await fileInfoObj['wpmz/template.kml']
    const xmlJson = XMLToJSON(xmlStr)?.['Document']
    state.pointList = xmlJson.Folder.Placemark
    // if (!getEntityById(route.id)) {
    //   drawWayline(xmlJson, route)
    // }
    drawWayline(xmlJson, route)
    // if (
    //   !route.patches_id ||
    //   route.patches_id == 'null' ||
    //   route.patches_id == ''
    // )
    // return
    // this.obtainSpotInfo(xmlJson)
    // this.spottedSurfaceGet(route)
  }
  // 绘制航线到地图上
  function drawWayline (xmlJson:any, route:any) {
    console.log('航线绘制到地图上',xmlJson)
    const points = xmlJson?.['Folder']?.['Placemark']
    //每个点数据
    let waylinePosition = reactive([])
    state.obtainWaypointData = []
    let SpecialPointsd = ref(false)
    const imageStart = new URL('@/assets/images/Startingpointicon.png', import.meta.url).href
    const imageEnd = new URL('@/assets/images/EndPointicon.png', import.meta.url).href
    const imgRightBlue = new URL('@/assets/images/arrow-right-blue.png', import.meta.url).href
    // 获取机场信息
    getLnglatAltitude(state.longitude, state.latitude, window.cesiumViewer).then((res) => {
      state.droneCoordinates = Cesium.Cartesian3.fromDegrees(
        Number(state.longitude),
        Number(state.latitude),
        Number(res.height),
      )
    })
    getPositionsHeight(points, window.cesiumViewer).then((res) => {
      waylinePosition = res.map((item) => {
        return Cesium.Cartesian3.fromDegrees(
          Number(item.longitude),
          Number(item.latitude),
          item.FinalHeight,
        )
      })
 
      res.forEach((item, index) => {
        let setting = {}
 
        if (index === 0) {
          let firstPoint = cartesian3Convert(
            waylinePosition[index],
            window.cesiumViewer,
          )
          let dornePoint = cartesian3Convert(
            state.droneCoordinates,
            window.cesiumViewer,
          )
          
          let getLongOk = getLnglatDist(
            firstPoint.longitude,
            firstPoint.latitude,
            dornePoint.longitude,
            dornePoint.latitude,
          )
          if (getLongOk < 20) {
            setting = {
              id: 'route_point_' + index,
              position: waylinePosition[index],
              billboard: {
                image: imageStart,
                pixelOffset: new Cesium.Cartesian2(0, -13),
                outlineWidth: 0,
                width: 30,
                height: 30,
                scale: 1.0,
              },
            }
          } else {
            setting = {
              id: 'route_point_' + index,
              position: waylinePosition[index],
              billboard: {
                image: createBillboard(`${index + 1}`, '#FFBA00FF'),
                pixelOffset: new Cesium.Cartesian2(0, -13),
                outlineWidth: 0,
              },
            }
            SpecialPointsd.value = true
          }
        } else if (index === points.length - 1) {
          setting = {
            id: 'route_point_' + index,
            position: waylinePosition[index],
            billboard: {
              image: imageEnd,
              pixelOffset: new Cesium.Cartesian2(0, -13),
              outlineWidth: 0,
              width: 30,
              height: 30,
              scale: 1.0,
            },
          }
        } else {
          setting = {
            id: 'route_point_' + index,
            position: waylinePosition[index],
            billboard: {
              image: createBillboard(
                `${SpecialPointsd.value ? index + 1 : index}`,
                '#FFBA00FF',
              ),
              pixelOffset: new Cesium.Cartesian2(0, -13),
              outlineWidth: 0,
            },
          }
        }
 
        window.cesiumViewer.entities.add(setting)
        // this.addClickevents('route_point_' + index, waylinePosition[index])
        // if (item?.['actionGroup']) {
        //   this.obtainWaypointInfo(item?.['actionGroup']?.['action'], index)
        // }
      })
 
      waylinePosition.unshift(state.droneCoordinates)
      //没有起点坐标时,在机场和第一个点增加一个中间转折点作为起点
      if (SpecialPointsd.value) {
        let cartesian = addTurnPoint(
          waylinePosition[0],
          waylinePosition[1],
        )
        waylinePosition.splice(1, 0, cartesian)
        if (getEntityById('route_detailSpecialPointsd')) {
          removeById('route_detailSpecialPointsd')
        }
        window.cesiumViewer.entities.add({
          position: cartesian,
          id: 'route_detailSpecialPointsd',
          billboard: {
            image: imageStart,
            width: 30,
            height: 30,
            pixelOffset: new Cesium.Cartesian2(0, -20),
          },
        })
      }
      window.cesiumViewer.entities.add({
        id: 'route_wayline_line',
        polyline: {
          positions: waylinePosition,
          width: 5,
          material: new ImageTrailMaterial({
            color: Cesium.Color.WHITE,
            speed: 10,
            image: imgRightBlue,
            repeat: { x: waylinePosition.length, y: 0 },
          }),
          zIndex: 1,
          clampToGround: state.clampToGroundshow,
        },
      })
      store.commit('SET_WAYLINE_POINTS', {
        type: 'point',
        data: waylinePosition,
      })
      flyToRouterShow(waylinePosition)
    })
  }
  // 指向该区域
  function flyToRouterShow (waylinePosition:any) {
    let cartesianlengthArr = waylinePosition.map((cartesian:any) => {
      return [
        cartesian3Convert(cartesian, window.cesiumViewer).longitude,
        cartesian3Convert(cartesian, window.cesiumViewer).latitude,
      ]
    })
    const centerpoint = getCenterPoint(cartesianlengthArr)
    let maxlength = 0
    cartesianlengthArr.forEach((item:any, index:any) => {
      let banseTwoPoints = getLnglatDist(
        item[0],
        item[1],
        centerpoint.lng,
        centerpoint.lat,
      )
      if (banseTwoPoints > maxlength) {
        maxlength = banseTwoPoints
      }
    })
    // clampToGroundshow
    let flyNum = state.clampToGroundshow ? 7 : 12
    flyTo(
      { longitude: centerpoint.lng, latitude: centerpoint.lat },
      3,
      maxlength * flyNum,
    )
  }
  // 创建广告牌
  function createBillboard (title:any, color:any) {
    const billboard = document.createElement('canvas')
    const ctx = billboard.getContext('2d')
    ctx.beginPath()
    ctx.ellipse(150, 90, 11, 11, 0, 0, Math.PI * 2)
    ctx.fillStyle = color
    ctx.fill()
    ctx.font = 'bold 15px serif'
    ctx.textAlign = 'center'
    ctx.fillStyle = '#ffffff'
    ctx.fillText(title, 150, 95)
    ctx.closePath()
    return billboard
  }
  // 在机场和第一个点直接增加一个中间转折点
  function addTurnPoint (firstPoint:any, SecondPoint:any) {
    const dronePosition = cartesian3Convert(firstPoint, window.cesiumViewer)
    const firstPosition = cartesian3Convert(SecondPoint, window.cesiumViewer)
    return new Cesium.Cartesian3.fromDegrees(
      dronePosition?.longitude,
      dronePosition?.latitude,
      firstPosition?.height,
    )
  }
  // 所有点事件
  // function obtainWaypointInfo (obtainlist, index) {
  //   if (Array.isArray(obtainlist)) {
  //     let eventData = []
  //     obtainlist.forEach((item) => {
  //       let eventName = item?.['actionActuatorFunc']?.['#text']
  //       eventData.push(this.findValueByLabel(eventName))
  //     })
  //     this.obtainWaypointData.push({
  //       label: eventData,
  //       subIndex: index,
  //     })
  //   } else {
  //     let eventName = obtainlist?.['actionActuatorFunc']?.['#text']
  //     this.obtainWaypointData.push({
  //       label: [this.findValueByLabel(eventName)],
  //       subIndex: index,
  //     })
  //   }
  // }
  // 增加鼠标监听事件,点击展示事件
  // function addClickevents (tragetPointint_id, currentPositionxyz) {
  //   addLeftClickEvent(tragetPointint_id, startPointEvent)
  //   const _this = this
  //   //鼠标点击展示详情
  //   function startPointEvent (click, pick, viewer) {
  //     // 当前点击项获取数据
  //     let id = pick?.id?._id.split('_')[2]
  //     let getNowData = _this.obtainWaypointData.find(
  //       (obj) => obj.subIndex == id,
  //     )
  //     if (getNowData) {
  //       _this.nowObtainWaypointData = getNowData.label
  //     }
  //     // 展示详情
  //     _this.taskBoxis = true
  //     viewer.container.appendChild(_this.$refs.taskBox)
  //     viewer.scene.postRender.addEventListener(() => {
  //       let windowCoord = Cesium.SceneTransforms.wgs84ToWindowCoordinates(window.cesiumViewer.scene,currentPositionxyz,)
  //       if (!_this.$refs.taskBox) return
  //       const x = windowCoord.x + 25
  //       const y = windowCoord.y - _this.$refs.taskBox.offsetHeight / 2 - 15
  //       _this.$refs.taskBox.style.transform = `translate3d(${Math.round(
  //         x,
  //       )}px,${Math.round(y)}px,${Math.round(40)}px )`
  //     })
  //   }
  // }
  return {
    pointWayline
  }
}