/*
|
* @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)
|
}
|