From aec00ecc093be803860c8675cbe1c4c776a7cb4e Mon Sep 17 00:00:00 2001
From: GuLiMmo <2820890765@qq.com>
Date: Tue, 19 Mar 2024 17:49:21 +0800
Subject: [PATCH] update: 新建航线、事件编辑、kmz文件问题修改

---
 src/utils/cesium/use-map-draw.ts |  172 +++++++++++++++++++++------------------------------------
 1 files changed, 64 insertions(+), 108 deletions(-)

diff --git a/src/utils/cesium/use-map-draw.ts b/src/utils/cesium/use-map-draw.ts
index c088045..e5a8005 100644
--- a/src/utils/cesium/use-map-draw.ts
+++ b/src/utils/cesium/use-map-draw.ts
@@ -4,7 +4,8 @@
 import ImageTrailMaterial from '/@/utils/cesium/ImageTrailMaterial'
 import { ref } from 'vue'
 import { cesiumOperation } from '/@/hooks/use-cesium-tsa'
-const { addPolyline, getEntityById } = cesiumOperation()
+import { XMLToJSON } from './kmz'
+const { addPolyline, getEntityById, removeAllDataSource, removeAllPoint } = cesiumOperation()
 
 const getResource = (name: string) => {
   return new URL(`/src/assets/icons/${name}`, import.meta.url).href
@@ -22,11 +23,17 @@
 }
 interface tragetPoint {
   position: Cesium.Cartesian3
-  eventList: eventParmas[]
+  isUseGlobalHeight: boolean
+  eventList: eventParmas[] | null
 }
 
+interface action {
+  key: string
+  name: string
+  icon?: string
+}
 // 对应事件
-const eventList = [
+const actionList: eventParmas[] = [
   {
     key: 'takePhoto',
     name: '单拍',
@@ -56,11 +63,6 @@
     key: 'zoom',
     name: '变焦',
     icon: getResource('waylinetool/fd.png'),
-  },
-  {
-    key: 'customDirName',
-    name: '创建新文件夹',
-    icon: getResource('waylinetool/create-file.png'),
   },
   {
     key: 'gimbalRotate',
@@ -109,6 +111,8 @@
 // kml中的全部实体
 const kmlEntities = ref<Cesium.Entity[]>([])
 
+const selectPointIndex = ref<number | null>(null)
+
 const useMapDraw = (
   dataSource: Cesium.DataSource | any,
   entitiesList: Cesium.Entity[] | any,
@@ -116,15 +120,11 @@
   global: any,
 ) => {
   const ellipsoid = global.$viewer.scene.globe.ellipsoid
-
-  //   kml文件中点位string
-  let kmlPoints: string[] | any = []
   //  记录点位信息
   let cartesianArr: Cesium.Cartesian3[] = []
 
-  //   绘制路线
+  // 绘制路线
   const drawWayline = async (entities?: Cesium.Entity[], kmlStr?: string) => {
-    console.log(kmlStr)
     if (!entities && !kmlStr) {
       dataSource.show = false
     }
@@ -136,19 +136,25 @@
     // 获取所有点位
     let kmlRes = ''
     if (kmlStr) {
+      removeAllDataSource()
+      removeAllPoint()
       kmlRes = kmlStr
     } else {
       const fileRes = await analyzeKmzFile(fileUrl)
-      kmlRes = await fileRes.content
+      kmlRes = await fileRes.fileInfoObj['wpmz/template.kml']
     }
+    const kmlJson = XMLToJSON(kmlRes)
     // 所有航点
-    kmlPoints = getKmlParams(kmlRes, false, {
-      name: 'Placemark',
-      findRegx: '([\\s\\S]*?)',
-      mode: 'g',
-    })
+    const kmlPoints = kmlJson.Document.Folder.Placemark
     // 起飞点
     let btmStartPoint = null
+    // 获取文件中的起飞点
+    const startPointPosition = kmlJson.Document.missionConfig.takeOffRefPoint['#text'].split(',')
+    btmStartPoint = Cesium.Cartesian3.fromDegrees(
+      Number(startPointPosition[1]),
+      Number(startPointPosition[0]),
+      Number(startPointPosition[2]),
+    )
     // 判断是否存在实体集
     if (entities) {
       cartesianArr = []
@@ -164,23 +170,21 @@
       const latitude = Cesium.Math.toDegrees(c2Postion.latitude)
       // 获取当前航点中的值
       const point = kmlPoints[i]
-      const getHaeHeight: any = getKmlParams(point, true, {
-        name: 'ellipsoidHeight',
-        findRegx: '([\\s\\S]*?)',
-      })
-      const getPointHeight: any = getKmlParams(point, true, {
-        name: 'height',
-        findRegx: '([\\s\\S]*?)',
-      })
-      const AslHeight = Number(getPointHeight[1])
-      const HeaHeight = Number(getHaeHeight[1])
+      const getHaeHeight = point.ellipsoidHeight['#text']
+      const getPointHeight = point.height['#text']
+      // 获取是否使用全局高度
+      const isUseGlobalHeight = point?.useGlobalHeight ? point.useGlobalHeight['#text'] : 0
+      const AslHeight = Number(getPointHeight)
+      const HeaHeight = Number(getHaeHeight)
       entity.position = Cesium.Cartesian3.fromDegrees(longitude, latitude, AslHeight)
-      if (i === 0) {
-        btmStartPoint = Cesium.Cartesian3.fromDegrees(longitude, latitude, 0)
-      }
       // 创建广告牌信息
-      const title = Number(i + 1) === 1 ? 'S' : i
-      const billboard = createTriangleMarker(title, '#61d396')
+      const title = Number(i + 1) === 1 ? 'S' : i + 1
+      let billboard = null
+      if (isUseGlobalHeight) {
+        billboard = createTriangleMarker(title, '#61d396')
+      } else {
+        billboard = createTriangleMarker(title, '#409eff')
+      }
       // 修改id
       entity._id = 'tragetPoint' + i
       // 修改广告牌样式
@@ -196,6 +200,7 @@
         verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
         disableDepthTestDistance: Number.POSITIVE_INFINITY,
         pixelOffset: new Cesium.Cartesian2(0, -40),
+        show: false,
       })
       // 修改点的信息
       entity.point = new Cesium.PointGraphics({
@@ -228,6 +233,7 @@
       // 当前航线点位事件信息等
       waylinePointsEvent.value.push({
         position: entity.position._value,
+        isUseGlobalHeight,
         eventList: [],
       })
     }
@@ -269,11 +275,9 @@
   //   获取航线详情
   const getWaylineDetails = (kmlStr: string) => {
     // 获取当前航线全局速度
-    const getGlobalSpeed: any = getKmlParams(kmlStr, true, {
-      name: 'autoFlightSpeed',
-      findRegx: '([\\s\\S]*?)',
-    })
-    const speed = Number(getGlobalSpeed[1])
+    const kmlJson = XMLToJSON(kmlStr).Document
+    const getGlobalSpeed = kmlJson.Folder.autoFlightSpeed['#text']
+    const speed = Number(getGlobalSpeed)
     const lineEntity = getEntityById('entityLine')
     // 获取距离
     const polylineLength: any = getPolylineLength(lineEntity).toFixed(1) || 0
@@ -296,78 +300,30 @@
       kmlRes = kmlStr
     } else {
       const fileRes = await analyzeKmzFile(fileUrl)
-      kmlRes = await fileRes.content
+      kmlRes = await fileRes.fileInfoObj['wpmz/template.kml']
     }
-    // 所有航点
-    const points = getKmlParams(kmlRes, false, {
-      name: 'Placemark',
-      findRegx: '([\\s\\S]*?)',
-      mode: 'g',
-    })
-    points?.forEach((point: string, index: number) => {
-      // 当前点位事件
-      const pointActions = getKmlParams(point, true, {
-        name: 'actionGroup',
-        findRegx: '([\\s\\S]*?)',
-        mode: 'g',
-      })
-      const eventArr: eventParmas[] = []
-      if (pointActions) {
-        const actions =
-          getKmlParams(pointActions[0], true, {
-            name: 'action',
-            findRegx: '([\\s\\S]*?)',
-            mode: 'g',
-          }) || []
-        actions?.forEach((action) => {
-          eventList.forEach((event: any) => {
-            if (!event.distinguish) {
-              action.includes(event.key) && eventArr.push(event)
-            } else {
-              const pitchAngle =
-                getKmlParams(action, true, {
-                  name: 'gimbalPitchRotateAngle',
-                  findRegx: '([\\s\\S]*?)',
-                }) || []
-              const yawAngle =
-                getKmlParams(action, true, {
-                  name: 'gimbalYawRotateAngle',
-                  findRegx: '([\\s\\S]*?)',
-                }) || []
-              if (!!Number(pitchAngle[1]) && event.distinguish === 'pitch') {
-                eventArr.push(event)
-              }
-              if (!!Number(yawAngle[1]) && event.distinguish === 'yaw') {
-                eventArr.push(event)
-              }
-            }
+    const kmlJson = XMLToJSON(kmlRes).Document
+    const points = kmlJson.Folder?.Placemark
+    let takePhotoNum = 0
+    points?.forEach((point: { actionGroup: any }, index: number) => {
+      if (Reflect.has(point, 'actionGroup')) {
+        const action = point.actionGroup.action
+        if (Array.isArray(action)) {
+          action.forEach((item) => {
+            const { actionActuatorFunc } = item
+            actionActuatorFunc?.['#text'] === 'takePhoto' && takePhotoNum++
+            const actionObj: eventParmas | any = actionList.find((event) => actionActuatorFunc['#text'] === event.key)
+            waylinePointsEvent.value[index].eventList?.push(actionObj)
           })
-          // 判断一共有多少个拍照模式
-          const actionFunc = getKmlParams(point, true, {
-            name: 'actionActuatorFunc',
-            findRegx: 'takePhoto',
-            mode: 'g',
-          })
-          if (actionFunc) {
-            ;(waylineDetails[3] as any).value++
-          }
-        })
-        waylinePointsEvent.value[index].eventList = eventArr
-      }
-      // 获取当前是否存在拍照模式
-      const pointShootMode = getKmlParams(point, true, {
-        name: 'shootType',
-        findRegx: '([\\s\\S]*?)',
-        mode: 'g',
-      })
-      if (pointShootMode) {
-        pointShootMode.forEach((type: string) => {
-          eventList.forEach((item: any) => {
-            type.includes(item.key) && eventArr.push(item)
-          })
-        })
+        } else {
+          const { actionActuatorFunc } = action
+          actionActuatorFunc['#text'] === 'takePhoto' && takePhotoNum++
+          const actionObj: eventParmas | any = actionList.find((item) => actionActuatorFunc['#text'] === item.key)
+          waylinePointsEvent.value[index].eventList?.push(actionObj)
+        }
       }
     })
+    waylineDetails[3].value = takePhotoNum
   }
   const clearWaylineData = () => {
     kmlEntities.value = []
@@ -384,5 +340,5 @@
   }
 }
 
-export { waylinePointsEvent, waylineDetails, kmlEntities }
+export { waylinePointsEvent, waylineDetails, kmlEntities, selectPointIndex }
 export default useMapDraw

--
Gitblit v1.9.3