forked from drone/command-center-dashboard

罗广辉
2025-04-02 e1699c4851f6ca397cd0ad1ff63f32c737654836
src/utils/cesium/use-kmz-tsa.js
@@ -1,7 +1,7 @@
import _ from 'lodash';
import JSZIP from 'jszip';
import { JSONToXML } from './kmz';
import { getDateFromTimestamp } from '@/utils/date';
import _ from 'lodash'
import JSZIP from 'jszip'
import { JSONToXML } from './kmz'
import { getDateFromTimestamp } from '@/utils/date'
export default function kmzFile() {
  // 云台信息
@@ -12,20 +12,20 @@
    67: '1',
    80: '0',
    81: '1',
  };
   }
  // 创建图斑航线
  const create = async (waylineBasicInfo, lnglats, spotList) => {
    try {
      const date = new Date();
      const username = window.localStorage.getItem('bs_username');
      const updateTime = date.getTime();
      const fileName = '图斑航线规划_' + date.getTime();
         const date = new Date()
         const username = window.localStorage.getItem('bs_username')
         const updateTime = date.getTime()
         const fileName = '图斑航线规划_' + date.getTime()
      // 无人机机型、云台型号、航线高度
      const { droneEnumValue, payloadEnumValue, height } = waylineBasicInfo;
      const defaultHeight = height || 100;
         const { droneEnumValue, payloadEnumValue, height } = waylineBasicInfo
         const defaultHeight = height || 100
      // 起始点位 (参考点位-具体以机场为准)
      const startPoint = lnglats[0];
      const takeOffPoint = [startPoint[1], startPoint[0], 0].join(',');
         const startPoint = lnglats[0]
         const takeOffPoint = [startPoint[1], startPoint[0], 0].join(',')
      // template.xml对象模版
      const fileTemplate = {
        author: { '#text': username },
@@ -113,27 +113,25 @@
          //   imageFormat: { '#text': 'wide,zoom' },
          // },
        },
      };
         }
      // 航点创建
      const polygonNo = {
        no: '',
        index: 0,
      };
         }
      lnglats.forEach((lnglat, index) => {
        const obj = spotList.find((spot) =>
          spot.dkfw.find((item) => _.isEqual(item, lnglat)),
        );
        let fileSuffix = '';
            const obj = spotList.find(spot => spot.dkfw.find(item => _.isEqual(item, lnglat)))
            let fileSuffix = ''
        if (obj) {
          if (polygonNo.no === obj.dkbh) {
            polygonNo.index++;
                  polygonNo.index++
          } else {
            polygonNo.no = obj.dkbh;
            polygonNo.index = 1;
                  polygonNo.no = obj.dkbh
                  polygonNo.index = 1
          }
          fileSuffix = `图斑航线拍摄(${obj.dkbh}-${polygonNo.index})`;
               fileSuffix = `图斑航线拍摄(${obj.dkbh}-${polygonNo.index})`
        } else {
          fileSuffix = '图斑航线拍摄(起点)';
               fileSuffix = '图斑航线拍摄(起点)'
        }
        const placemark = {
          Point: {
@@ -191,40 +189,36 @@
              },
            },
          },
        };
        fileTemplate.Folder.Placemark.push(placemark);
      });
      const fileWaylines = waylinesContext(fileTemplate);
            }
            fileTemplate.Folder.Placemark.push(placemark)
         })
         const fileWaylines = waylinesContext(fileTemplate)
      return {
        fileName,
        template: fileTemplate,
        waylines: fileWaylines,
      };
    } catch (error) {
      console.log(error);
      return false;
    }
  };
      } catch (error) {
         console.log(error)
         return false
      }
   }
  // 创建正常航线
  const createNoramlWaylines = (waylineBasicInfo, lnglats) => {
    try {
      const date = new Date();
      const username = window.localStorage.getItem('bs_username');
      const updateTime = date.getTime();
      const { year, month, day, hours, minutes } =
        getDateFromTimestamp(updateTime);
      const fileName = '新建航线_' + year + month + day + hours + minutes;
         const date = new Date()
         const username = window.localStorage.getItem('bs_username')
         const updateTime = date.getTime()
         const { year, month, day, hours, minutes } = getDateFromTimestamp(updateTime)
         const fileName = '新建航线_' + year + month + day + hours + minutes
      // 无人机机型、云台型号、航线高度
      const { droneEnumValue, payloadEnumValue, height, waylineName } =
        waylineBasicInfo;
      const defaultHeight = height || 100;
         const { droneEnumValue, payloadEnumValue, height, waylineName } = waylineBasicInfo
         const defaultHeight = height || 100
      // 起始点位 (参考点位-具体以机场为准)
      const startPoint = lnglats[0];
      const takeOffPoint = [startPoint.latitude, startPoint.longitude, 0].join(
        ',',
      );
         const startPoint = lnglats[0]
         const takeOffPoint = [startPoint.latitude, startPoint.longitude, 0].join(',')
      // template.xml对象模版
      const fileTemplate = {
        author: { '#text': username },
@@ -317,7 +311,7 @@
            imageFormat: { '#text': 'wide,zoom' },
          },
        },
      };
         }
      lnglats.forEach((lnglat, index) => {
        const placemark = {
          Point: {
@@ -357,7 +351,7 @@
          useGlobalHeadingParam: { '#text': 1 },
          useGlobalTurnParam: { '#text': 1 },
          useStraightLine: { '#text': 1 },
        };
            }
        if (lnglat?.actions) {
          const actionGroup = {
            actionGroupId: { '#text': index },
@@ -374,13 +368,13 @@
            //   actionActuatorFuncParam: {},
            // }],
            action: [],
          };
               }
          lnglat?.actions.forEach((action, index) => {
            const singleAction = {
              actionId: { '#text': index },
              actionActuatorFunc: { '#text': '' },
              actionActuatorFuncParam: {},
            };
                  }
            // actionGroup.action['actionActuatorFunc'] = { '#text': action.key }
            // action.params.forEach(p => {
            //   if (p.value || p.value !== '') {
@@ -389,69 +383,66 @@
            // })
            singleAction['actionActuatorFunc'] = {
              '#text': action.key,
            };
            action.params.forEach((p) => {
                  }
                  action.params.forEach(p => {
              if (p.value || p.value !== '') {
                singleAction['actionActuatorFuncParam'][p.key] = {
                  '#text': p.value,
                };
              }
            });
                     }
                  })
            if (lnglat?.actions.length > 1) {
              actionGroup.action.push(singleAction);
                     actionGroup.action.push(singleAction)
            } else {
              actionGroup.action = singleAction;
                     actionGroup.action = singleAction
            }
          });
          placemark.actionGroup = actionGroup;
               })
               placemark.actionGroup = actionGroup
        }
        fileTemplate.Folder.Placemark.push(placemark);
      });
      const fileWaylines = waylinesContext(fileTemplate);
            fileTemplate.Folder.Placemark.push(placemark)
         })
         const fileWaylines = waylinesContext(fileTemplate)
      return {
        fileName: waylineName || fileName,
        template: fileTemplate,
        waylines: fileWaylines,
      };
    } catch (error) {
      console.log(error);
      return error;
    }
  };
      } catch (error) {
         console.log(error)
         return error
      }
   }
  // 创建面状航线
  const createPlanarWayline = (waylineBasicInfo, lnglats) => {};
   const createPlanarWayline = (waylineBasicInfo, lnglats) => {}
  // 创建点状航线
  const createPointWayLines = (
    waylineBasicInfo,
    points,
    settingInfo,
    dockPosition,
  ) => {
   const createPointWayLines = (waylineBasicInfo, points, settingInfo, dockPosition) => {
    try {
      const { droneEnumValue, payloadEnumValue, waylineName } =
        waylineBasicInfo;
         const { droneEnumValue, payloadEnumValue, waylineName } = waylineBasicInfo
      const { imageFormat, heightMode, globalHeight, autoFlightSpeed,gimbalPitchMode,
        waypointTurnMode,waypointHeadingMode,finishAction,globalTransitionalSpeed  } =
        settingInfo;
         const {
            imageFormat,
            heightMode,
            globalHeight,
            autoFlightSpeed,
            gimbalPitchMode,
            waypointTurnMode,
            waypointHeadingMode,
            finishAction,
            globalTransitionalSpeed,
         } = settingInfo
      const date = new Date();
      const username = window.localStorage.getItem('bs_username');
      const updateTime = date.getTime();
      const { year, month, day, hours, minutes } =
        getDateFromTimestamp(updateTime);
      const fileName = '新建航线_' + year + month + day + hours + minutes;
         const date = new Date()
         const username = window.localStorage.getItem('bs_username')
         const updateTime = date.getTime()
         const { year, month, day, hours, minutes } = getDateFromTimestamp(updateTime)
         const fileName = '新建航线_' + year + month + day + hours + minutes
      // 无人机机型、云台型号、航线高度
      const defaultHeight = 100;
         const defaultHeight = 100
      // 起始点位 (参考点位-具体以机场为准)
      const takeOffPoint = [
        dockPosition.latitude,
        dockPosition.longitude,
        0,
      ].join(',');
         const takeOffPoint = [dockPosition.latitude, dockPosition.longitude, 0].join(',')
      // template.xml对象模版
      const fileTemplate = {
@@ -545,10 +536,10 @@
            imageFormat: imageFormat,
          },
        },
      };
         }
      points.length &&
        points.forEach((detail, ind) => {
          const { Point, index, height, ellipsoidHeight, actionGroup,waypointSpeed } = detail;
               const { Point, index, height, ellipsoidHeight, actionGroup, waypointSpeed } = detail
          const placemark = {
            Point,
@@ -562,7 +553,9 @@
            waypointSpeed: { '#text': waypointSpeed ||  10 },
            // 沿航线方向 - 可使用当前默认值
            waypointHeadingParam: {
              waypointHeadingMode: { '#text': waypointHeadingMode || 'followWayline' },
                     waypointHeadingMode: {
                        '#text': waypointHeadingMode || 'followWayline',
                     },
              waypointHeadingAngle: { '#text': 0 },
              waypointPoiPoint: {
                '#text': '0.000000,0.000000,0.000000',
@@ -583,39 +576,39 @@
            useGlobalHeadingParam: { '#text': 1 },
            useGlobalTurnParam: { '#text': 1 },
            useStraightLine: { '#text': 1 },
          };
               }
          if (actionGroup) {
            placemark.actionGroup = actionGroup;
                  placemark.actionGroup = actionGroup
          }
          fileTemplate.Folder.Placemark.push(placemark);
        });
               fileTemplate.Folder.Placemark.push(placemark)
            })
      const fileWaylines = waylinesContext(fileTemplate);
         const fileWaylines = waylinesContext(fileTemplate)
      return {
        fileName: waylineName || fileName,
        template: fileTemplate,
        waylines: fileWaylines,
      };
    } catch (error) {
      console.log(error);
      return error;
    }
  };
      } catch (error) {
         console.log(error)
         return error
      }
   }
  const save = async (fileInfo) => {
    const JsZip = new JSZIP();
    const { fileName, template, waylines } = fileInfo;
    const templateXml = JSONToXML(template, '', true);
    const waylinesWpml = JSONToXML(waylines, '', true);
    JsZip.file('wpmz/template.kml', templateXml);
    JsZip.file('wpmz/waylines.wpml', waylinesWpml);
    const fileBlob = await JsZip.generateAsync({ type: 'blob' });
   const save = async fileInfo => {
      const JsZip = new JSZIP()
      const { fileName, template, waylines } = fileInfo
      const templateXml = JSONToXML(template, '', true)
      const waylinesWpml = JSONToXML(waylines, '', true)
      JsZip.file('wpmz/template.kml', templateXml)
      JsZip.file('wpmz/waylines.wpml', waylinesWpml)
      const fileBlob = await JsZip.generateAsync({ type: 'blob' })
    // saveAs(kmzFile, `${fileName}.kmz`)
    return fileBlob;
  };
      return fileBlob
   }
  const waylinesContext = (templateJson) => {
   const waylinesContext = templateJson => {
    const waylinesObj = {
      // 标准模版
      missionConfig: {
@@ -642,38 +635,35 @@
        */
        Placemark: null,
      },
    };
    Object.keys(waylinesObj).forEach((key) => {
      if (
        Object.prototype.toString.call(waylinesObj[key]) === '[object Object]'
      ) {
        Object.keys(waylinesObj[key]).forEach((item) => {
          waylinesObj[key][item] = templateJson[key][item];
      }
      Object.keys(waylinesObj).forEach(key => {
         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;
                  waylinesObj[key][item] = templateJson[key].waylineCoordinateSysParam.heightMode
          }
          if (item === 'waylineId') {
            waylinesObj[key][item] = templateJson[key].templateId;
                  waylinesObj[key][item] = templateJson[key].templateId
          }
          if (item === 'Placemark') {
            const placemarks = _.cloneDeep(templateJson[key][item]);
            placemarks.forEach((placemark) => {
                  const placemarks = _.cloneDeep(templateJson[key][item])
                  placemarks.forEach(placemark => {
              placemark.executeHeight = {
                '#text': placemark.ellipsoidHeight?.['#text'] || '',
              };
              delete placemark.ellipsoidHeight;
              delete placemark.height;
            });
            waylinesObj[key][item] = placemarks;
          }
        });
                     delete placemark.ellipsoidHeight
                     delete placemark.height
                  })
                  waylinesObj[key][item] = placemarks
               }
            })
      } else {
        waylinesObj[key] = templateJson[key];
            waylinesObj[key] = templateJson[key]
      }
    });
    return waylinesObj;
  };
      })
      return waylinesObj
   }
  return {
    create,
@@ -681,5 +671,5 @@
    createPlanarWayline,
    createPointWayLines,
    save,
  };
   }
}