GuLiMmo
2024-03-18 3e13dd0242140c7189b65e4171efaa9190d1763d
src/utils/cesium/use-kmz-tsa.ts
@@ -1,13 +1,8 @@
import { ref } from 'vue'
import {
  analyzeKmzFile,
  XMLToJSON,
  JSONToXML
} from '/@/utils/cesium/kmz'
import { analyzeKmzFile, XMLToJSON, JSONToXML } from '/@/utils/cesium/kmz'
import _ from 'lodash'
import JSZIP from 'jszip'
import { saveAs } from 'file-saver'
const JsZip = new JSZIP()
const fileAuthor = reactive({
  name: '',
@@ -150,30 +145,132 @@
    template.value = tplXmlJson.Document
    return kmzRes
  }
  const create = () => {
  const create = (waylineBasicInfo) => {
    const author = window.localStorage.getItem('username') || ''
    const createTime = new Date().getTime()
    // 模板
    const template = {
      author,
      createTime,
      updateTime: createTime,
      missionConfig: {
        flyToWaylineMode: 'safely',
        finishAction: 'goHome',
        exitOnRCLost: 'goContinue',
        executeRCLostAction: 'goBack',
        takeOffSecurityHeight: 20,
        takeOffRefPoint: '0.000000,0.000000,0.000000',
        takeOffRefPointAGLHeight: 0,
        globalTransitionalSpeed: 15,
        globalRTHHeight: 80,
        droneInfo: {
          droneEnumValue: 0,
          droneSubEnumValue: 0,
        },
        payloadInfo: {
          payloadEnumValue: 0,
          payloadSubEnumValue: 0,
          payloadPositionIndex: 0,
        },
      },
      Folder: {
        templateType: 'waypoint',
        useGlobalTransitionalSpeed: 0,
        templateId: 0,
        waylineCoordinateSysParam: {
          coordinateMode: 'WGS84',
          heightMode: 'EGM96',
          globalShootHeight: 50,
          positioningType: 'GPS',
          surfaceFollowModeEnable: 1,
          surfaceRelativeHeight: 100,
        },
        autoFlightSpeed: 7,
        gimbalPitchMode: 'usePointSetting',
        globalWaypointHeadingParam: {
          waypointHeadingMode: 'followWayline',
          waypointHeadingAngle: 0,
          waypointPoiPoint: '0.000000,0.000000,0.000000',
          waypointHeadingPathMode: 'followBadArc',
        },
        globalWaypointTurnMode: 'toPointAndStopWithDiscontinuityCurvature',
        globalUseStraightLine: 0,
        Placemark: [],
      },
    }
  }
  const save = () => {
    console.log('存在问题')
    const JsZip = new JSZIP()
    const waylines = waylinesContext(template.value)
    const templateXml = JSONToXML(template.value, '', true)
    const waylinesWpml = JSONToXML(waylines, '', true)
    JsZip.file('wpmz/template.kml', templateXml)
    JsZip.file('wpmz/waylines.wpml', waylinesWpml)
    JsZip.generateAsync({ type: 'blob' }).then((blob) => {
      saveAs(blob, '新建航线.kmz')
    })
  }
  const generateXML = <T>(xmlJson: T) => {
    return JSONToXML(xmlJson)
  }
  const waylinesContext = (templateJson: any) => {
    const waylinesObj: { [key: string]: any } = {
      missionConfig: {
        flyToWaylineMode: null,
        finishAction: null,
        exitOnRCLost: null,
        executeRCLostAction: null,
        takeOffSecurityHeight: null,
        globalTransitionalSpeed: null,
        globalRTHHeight: null,
        droneInfo: null,
        payloadInfo: null,
      },
      Folder: {
        templateId: null,
        executeHeightMode: null,
        waylineId: null,
        autoFlightSpeed: null,
        Placemark: null,
      },
    }
    Object.keys(waylinesObj).forEach((key: string) => {
      if (Object.prototype.toString.call(waylinesObj[key]) === '[object Object]') {
        Object.keys(waylinesObj[key]).forEach((item) => {
          waylinesObj[key][item] = templateJson[key][item]
          if (item === 'executeHeightMode') {
            waylinesObj[key][item] = templateJson[key].waylineCoordinateSysParam.heightMode
          }
          if (item === 'waylineId') {
            waylinesObj[key][item] = templateJson[key].templateId
          }
        })
      } else {
        waylinesObj[key] = templateJson[key]
      }
    })
    return waylinesObj
  }
  // 将wpmz/template.kml解析传来json转换回去,重新复制以便于后续绘制
  watch(() => template.value, (newobj) => {
    const xmlString = JSONToXML(newobj)
    kmlStr.value = xmlString
  }, {
    deep: true
  })
  watch(
    () => template.value,
    (newobj) => {
      const xmlString = JSONToXML(newobj)
      kmlStr.value = xmlString
    },
    {
      deep: true,
    },
  )
  return {
    init,
    create,
    save,
    generateXML
    generateXML,
  }
}