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
import * as Cesium from 'cesium';
import { appointroutePlanning } from '@/api/RoutePlanning';
import { analyzeKmzFile, XMLToJSON } from '@/utils/cesium/kmz.js';
import { getLnglatAltitude } from '@/utils/cesium/mapUtil';
import cesiumOperation from '@/utils/cesium-tsa';
const { removeAllPoint, removeById } = cesiumOperation();
 
export default {
  methods: {
    // 初始化航线
    async initPlanarWayline(route) {
      removeAllPoint();
      if (!route) return;
      const { data: waylineUrl } = await appointroutePlanning(
        this.wId,
        route.id,
      );
      if (waylineUrl.code !== 0)
        return this._showMessage.error(waylineUrl.message);
      const { fileInfoObj } = await analyzeKmzFile(
        `${waylineUrl.data}?_t=${new Date().getTime()}`,
      );
      const templateXML = await fileInfoObj['wpmz/template.kml'];
      const waylinesXML = await fileInfoObj['wpmz/waylines.wpml'];
      const templateXMLJSON = XMLToJSON(templateXML)?.['Document'];
      const waylinesXMLJSON = XMLToJSON(waylinesXML)?.['Document'];
      this.drawPlanarWayline(templateXMLJSON, waylinesXMLJSON);
    },
    async drawPlanarWayline(templateXMLJSON, waylinesXMLJSON) {
      let coordArr = null;
      const placemark = templateXMLJSON.Folder?.Placemark;
      // 取出点位
      const coordinates =
        placemark.Polygon?.outerBoundaryIs.LinearRing.coordinates?.[
          '#text'
        ]?.split('\n') || [];
      // 数组转换
      coordArr = coordinates.map((coordinate) =>
        coordinate
          .replace(/\s+/g, '')
          .split(',')
          .map((v) => Number(v)),
      );
      // 获取当前经纬度海拔高度
      const newCoordArr = [];
      // 面状点位
      for (let [index, coord] of coordArr.entries()) {
        const [lng, lat] = coord;
        const { height: hAltitude } = await getLnglatAltitude(
          Number(lng),
          Number(lat),
          global.$viewer,
        );
        newCoordArr.push([lng, lat, hAltitude]);
      }
      // 航线点位
      const waylinePoints = waylinesXMLJSON.Folder.Placemark;
      const waylinePointLnglats = waylinePoints.map((json) => {
        const executeHeight = Number(json.executeHeight['#text']);
        const coordinate = json.Point.coordinates['#text']
          .split(',')
          .map((lnglat) => Number(lnglat));
        coordinates.push();
        return Cesium.Cartesian3.fromDegrees(
          coordinate[0],
          coordinate[1],
          executeHeight,
        );
      });
      // 绘制面状航线--------------------
      waylinePointLnglats.unshift(this.droneCoordinates);
      if (!this.clampToGroundshow) {
        let cartesian = this.addTurnPoint(
          waylinePointLnglats[0],
          waylinePointLnglats[1],
        );
        waylinePointLnglats.splice(1, 0, cartesian);
      }
 
      global.$viewer.entities.add({
        id: 'task_result_wayline',
        polyline: {
          width: 3,
          positions: waylinePointLnglats,
          material: Cesium.Color.CHARTREUSE,
          zIndex: 1,
          clampToGround: this.clampToGroundshow,
        },
      });
      // 面状航线第一个点突出展示
      this.StartingIncreasePoint(waylinePointLnglats);
      // 面状判断当前点位数量
      const cloneCoordArr = [...newCoordArr];
      if (cloneCoordArr.length >= 3) {
        cloneCoordArr.push(newCoordArr[0]);
      }
 
      this.$store.commit('SET_WAYLINE_POINTS', {
        type: 'planar',
        data: waylinePointLnglats,
        planarOutline: cloneCoordArr,
        planarPolygon: newCoordArr,
        clampToGroundshow: this.clampToGroundshow,
      });
 
      // 绘制面状边框线--------------------
      global.$viewer.entities.add({
        id: 'task_planarOutline',
        polyline: {
          positions: Cesium.Cartesian3.fromDegreesArrayHeights(
            cloneCoordArr.flat(),
          ),
          width: 4,
          material: new Cesium.PolylineOutlineMaterialProperty({
            color: new Cesium.Color.fromBytes(74, 138, 233),
            outlineWidth: 2,
            outlineColor: Cesium.Color.WHITE,
          }),
          zIndex: -1,
          clampToGround: true,
        },
      });
      // 绘制面状地块--------------------
      global.$viewer.entities.add({
        id: 'task_planarWayline',
        polygon: {
          hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(
            newCoordArr.flat(),
          ),
          material: new Cesium.Color.fromBytes(75, 159, 221, 100),
          outline: true,
          outlineColor: new Cesium.Color.fromBytes(35, 85, 216, 255),
          zIndex: -1,
          clampToGround: true,
        },
      });
      // 飞向此处
      this.flyToRouterShow(waylinePointLnglats);
    },
    // 起点增加点展示
    StartingIncreasePoint(wayData) {
      let positions = this.clampToGroundshow ? wayData[1] : wayData[2];
      removeById('task_point_start');
      let setting = {
        id: 'task_point_start',
        position: positions,
        billboard: {
          image: this.PlanaBillboard('#2D8CF0'),
          pixelOffset: new Cesium.Cartesian2(146, 72),
          zIndex: 2,
          clampToGround: this.clampToGroundshow,
        },
      };
      global.$viewer.entities.add(setting);
    },
    //面状点设置
    PlanaBillboard(color) {
      const billboard = document.createElement('canvas');
      const ctx = billboard.getContext('2d');
      const radius = 4;
      ctx.beginPath();
      ctx.arc(radius, radius, radius, 0, 2 * Math.PI, false);
      ctx.fillStyle = color; // 设置填充颜色
      ctx.fill();
      return billboard;
    },
  },
};