From 016de324c0145632532fcd5450b96a2d1291dac6 Mon Sep 17 00:00:00 2001
From: GuLiMmo <2820890765@qq.com>
Date: Fri, 08 Mar 2024 17:41:24 +0800
Subject: [PATCH] update:  右键添加航点完成、kmz文件编辑等

---
 src/utils/cesium/use-kmz-tsa.ts |   96 ++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 85 insertions(+), 11 deletions(-)

diff --git a/src/utils/cesium/use-kmz-tsa.ts b/src/utils/cesium/use-kmz-tsa.ts
index 2f49acf..3017b97 100644
--- a/src/utils/cesium/use-kmz-tsa.ts
+++ b/src/utils/cesium/use-kmz-tsa.ts
@@ -1,11 +1,15 @@
 import { ref, getCurrentInstance } from 'vue'
 import { analyzeKmzFile, getKmlParams, generateKmlFormat } from '/@/utils/cesium/kmz'
-import { getLngLatAltitude } from './mapUtils'
 import JSZIP from 'jszip'
 import { saveAs } from 'file-saver'
+import { point } from '@turf/turf'
 
 const JsZip = new JSZIP()
 
+const fileAuthor = reactive({
+  name: '',
+  author: '',
+})
 const kmlStr = ref('')
 const kmzFile = ref<any>(null)
 
@@ -71,7 +75,7 @@
 
   // const write = (settingParmas: any = {}, name: string) => {
   // }
-  const writePoint = (settingParmas: any = {}) => {
+  const writePoint = (settingParmas: any = {}, place: string, pointNumber: number = 0) => {
     Object.keys(settingParmas).forEach((key: string) => {
       placemark[key] = settingParmas[key]
     })
@@ -81,11 +85,46 @@
       findRegx: '([\\s\\S]*?)',
       mode: 'g',
     }) || ['']
-    const lastPoint = points[points.length - 1]
-    const index = kmlStr.value.indexOf(lastPoint)
-    const beforeStr = kmlStr.value.slice(0, index)
-    const afterStr = kmlStr.value.slice(index + lastPoint.length)
-    kmlStr.value = beforeStr + lastPoint + Placemark + afterStr
+    if (place === 'add') {
+      const lastPoint = points[points.length - 1]
+      const index = kmlStr.value.indexOf(lastPoint)
+      const beforeStr = kmlStr.value.slice(0, index)
+      const afterStr = kmlStr.value.slice(index + lastPoint.length)
+      kmlStr.value = beforeStr + lastPoint + Placemark + afterStr
+    }
+    if (place === 'prev') {
+      const currentPoint: string = points[pointNumber]
+      const index = kmlStr.value.indexOf(currentPoint)
+      const beforeStr = kmlStr.value.slice(0, index)
+      const afterStr = kmlStr.value.slice(index + currentPoint.length)
+      kmlStr.value = beforeStr + Placemark + currentPoint + afterStr
+      // 更新点位序号
+      const pointIndexList = getKmlParams(kmlStr.value, true, {
+        name: 'index',
+        findRegx: '([\\s\\S]*?)',
+        mode: 'g',
+      })
+      pointIndexList?.forEach((pintIndex: string, index: number) => {
+        kmlStr.value.replace(pintIndex, `<wpml:index>${index}</wpml:index>`)
+      })
+    }
+    if (place === 'next') {
+      const currentPoint: string = points[pointNumber + 1]
+      const index = kmlStr.value.indexOf(currentPoint)
+      const beforeStr = kmlStr.value.slice(0, index)
+      const currentPointLength = currentPoint?.length || 0
+      const afterStr = kmlStr.value.slice(index + currentPointLength)
+      kmlStr.value = beforeStr + Placemark + currentPoint + afterStr
+      // 更新点位序号
+      const pointIndexList = getKmlParams(kmlStr.value, true, {
+        name: 'index',
+        findRegx: '([\\s\\S]*?)',
+        mode: 'g',
+      })
+      pointIndexList?.forEach((pintIndex: string, index: number) => {
+        kmlStr.value.replace(pintIndex, `<wpml:index>${index}</wpml:index>`)
+      })
+    }
   }
 
   const del = () => {}
@@ -104,9 +143,44 @@
             findRegx: '([\\s\\S]*?)',
             mode: 'g',
           })
-          regxVal.forEach((heightStr: string) => {
-            const rstr = generateKmlFormat({ height: settingParmas[key] })
-            kmlStr.value = kmlStr.value.replace(heightStr, rstr)
+          const ellipsoidHeightRegx = getKmlParams(kmlStr.value, true, {
+            name: 'ellipsoidHeight',
+            findRegx: '([\\s\\S]*?)',
+            mode: 'g',
+          }) || ['']
+          // 文件中的海拔高度
+          const takeOffRefPointAGLHeightRegxStr = getKmlParams(kmlStr.value, true, {
+            name: 'takeOffRefPointAGLHeight',
+            findRegx: '([\\s\\S]*?)'
+          }) || ['', '0']
+          const posterHeight = Number(takeOffRefPointAGLHeightRegxStr[1])
+          // 获取每个点是否使用全局高度
+          const useGlobalHeightRegStr = getKmlParams(kmlStr.value, true, {
+            name: 'useGlobalHeight',
+            findRegx: '([\\s\\S]*?)',
+            mode: 'g',
+          }) || ['', '0']
+          regxVal.forEach((heightStr: string, index: number) => {
+            // 使用正则取值
+            const getUseGlobalHeightValStr = getKmlParams(useGlobalHeightRegStr[index], true, {
+              name: 'useGlobalHeight',
+              findRegx: '([\\s\\S]*?)',
+            }) || ['', '0']
+            const isGlobalHeight = Boolean(Number(getUseGlobalHeightValStr[1]))
+            if (isGlobalHeight) {
+              // 替换height
+              const rstr = generateKmlFormat({ height: settingParmas[key] })
+              const rEllipsoidHeightStr = generateKmlFormat({ ellipsoidHeight: settingParmas[key] - posterHeight })
+              kmlStr.value = kmlStr.value.replace(heightStr, rstr)
+              kmlStr.value = kmlStr.value.replace(ellipsoidHeightRegx[index], rEllipsoidHeightStr)
+              // 替换
+            } else {
+              const heightRegxVal: any = getKmlParams(heightStr, true, {
+                name: 'height',
+                findRegx: '([\\s\\S]*?)',
+              })
+              console.log(heightRegxVal)
+            }
           })
         }
         const replaceStr = generateKmlFormat({ [key]: settingParmas[key] })
@@ -136,5 +210,5 @@
   }
 }
 
-export { kmlStr }
+export { kmlStr, fileAuthor }
 export default useKmzTsa

--
Gitblit v1.9.3