罗广辉
2025-06-19 f27ca082eb0a839449dd50c49007b58e5ed6946f
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
/*
 * @Author: GuLiMmo 2820890765@qq.com
 * @Date: 2024-03-22 08:50:18
 * @LastEditors: GuLiMmo 2820890765@qq.com
 * @LastEditTime: 2024-03-22 08:57:01
 * @FilePath: /drone-web/src/utils/turf.ts
 * @Description: turf
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
import * as turf from '@turf/turf'
import * as Cesium from 'cesium'
 
// 中心点
export function getRegionCenter (params: any[]) {
  const arr: any[] = []
 
  params.forEach((areaArray) => {
    const data = JSON.parse(areaArray)
    if (data.type === 'Polygon') {
      data.coordinates.forEach((item: turf.helpers.Position[]) => {
        arr.push(turf.centroid(turf.polygon([item])).geometry.coordinates)
      })
    } else {
      data.coordinates.forEach((item: turf.helpers.Position[][]) => {
        arr.push(turf.centroid(turf.polygon([item[0]])).geometry.coordinates)
      })
    }
  })
 
  if (arr.length === 1) {
    return arr[0]
  } else if (arr.length === 2) {
    return turf.centroid(turf.midpoint(turf.point(arr[0]), turf.point(arr[1]))).geometry.coordinates
  } else {
    arr.push(arr[0])
 
    return turf.centroid(turf.polygon([arr])).geometry.coordinates
  }
}
 
// 最值
export const getMapBounds = (data: { x: number; y: number }[], type = '') => {
  const pointArr: turf.helpers.Feature<turf.helpers.Geometry, turf.helpers.Properties>[] = []
 
  data.forEach((item: { x: number; y: number }) => {
    pointArr.push(turf.point([item.x, item.y]))
    return false
  })
 
  const features = turf.featureCollection(pointArr)
 
  const scope = turf.envelope(features).bbox
 
  // const poly = turf.polygon([[[0, 29], [3.5, 29], [2.5, 32], [0, 29]]])
 
  return scope
}
 
// 加载面
export function addDataToGlobe (polygonArr: string[], viewer: Cesium.Viewer) {
  const polygonInstances: any[] = []
 
  polygonArr.forEach((item: string, index: any) => {
    let position: any = item
      .slice(9, item.length - 2)
      .replace(/,/g, ';')
      .replace(/ /g, ',')
      .split(';')
    position = position.map((item: string) => item.split(',').map((i) => Number(i)))
    position = position.map((item: any) => Cesium.GeoJsonDataSource.crsNames['EPSG:4524'](item))
 
    const polygonArr = position.reduce((pre: any[], cur: any[]) => {
      pre.push(cur[0])
      pre.push(cur[1])
      return pre
    }, [])
 
    // polygonArr 格式 [112, 24, 115, 25, 116, 27]
 
    const polygon = new Cesium.PolygonGeometry({
      polygonHierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(polygonArr)),
      vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    })
 
    const geometry = Cesium.PolygonGeometry.createGeometry(polygon)
 
    polygonInstances.push(
      new Cesium.GeometryInstance({
        geometry: geometry as any,
        attributes: {
          color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.GREEN),
        },
      }),
    )
  })
 
  const polylinePrimitive = new Cesium.Primitive({
    geometryInstances: polygonInstances,
    appearance: new Cesium.PerInstanceColorAppearance(),
    asynchronous: false, // 确定基元是异步创建还是阻塞直到准备就绪
  })
 
  viewer.scene.primitives.add(polylinePrimitive)
}