You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GCSGUI/src/utils/map/geometry.ts

135 lines
3.5 KiB
TypeScript

/*
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-20 08:55:59
* @LastEditors: cbwu
* @LastEditTime: 2024-03-26 09:00:46
* @Description:几何类型类
*/
import {
Entity,
Cartesian3,
Color,
PrimitiveCollection,
PointPrimitiveCollection,
PolylineGeometry,
PointGraphics,
Property,
PolylineGraphics,
PolygonGraphics,
} from 'cesium'
type EntityOptions = {
id?: string
name?: string
show?: boolean
pixelSize?: number
color?: Color
fillColor?: Color
fill?: boolean
width?: number
outlineWidth?: number
}
// 点
class PointEntity extends Entity {
static id: number = 0
options: EntityOptions = {
id: 'Point' + String(PointEntity.id),
name: 'Point' + String(PointEntity.id + 1),
show: true,
pixelSize: 10,
color: Color.GREEN,
outlineWidth: 0,
}
constructor(position: Cartesian3, options?: EntityOptions) {
super({
// id: options?.id || String(PointEntity.id),
position: position,
})
this.options = { ...this.options, ...options }
this.name = this.options.name
this.point = new PointGraphics({ ...this.options })
PointEntity.id++
}
}
// 线
class PolylineEntity extends Entity {
static id: number = 0
options: EntityOptions = {
id: 'Polyline' + String(PolylineEntity.id),
name: 'Polyline' + String(PolylineEntity.id + 1),
show: true,
width: 2,
color: Color.GREEN,
}
constructor(ptArr: Cartesian3[] | Property, options?: EntityOptions) {
super({
// id: options?.id || String(PolylineEntity.id),
})
this.options = { ...this.options, ...options }
this.name = this.options.name
this.polyline = new PolylineGraphics({
positions: ptArr,
show: this.options.show,
width: this.options.width,
material: this.options.color,
})
PolylineEntity.id++
}
}
// 多边形
class PolygonEntity extends Entity {
static id: number = 0
options: EntityOptions = {
id: 'Polygon' + String(PolygonEntity.id),
name: 'Polygon' + String(PolygonEntity.id + 1),
show: true,
width: 2,
color: Color.RED,
fillColor: Color.RED.withAlpha(0.5),
fill: true,
}
constructor(ptArr: Cartesian3[] | Property, options?: EntityOptions) {
super({
// id: options?.id || String(PolygonEntity.id),
})
this.options = { ...this.options, ...options }
this.name = this.options.name
this.polygon = new PolygonGraphics({
show: this.options.show,
hierarchy: ptArr,
material: this.options.fillColor, //填充颜色
fill: this.options.fill, //是否填充
outlineWidth: this.options.width, //线宽
outlineColor: this.options.color, //线颜色
})
PolygonEntity.id++
}
}
// 线
class Polyline extends PrimitiveCollection {
constructor(ptArr: Cartesian3[]) {
super()
this.add(this.createPoints(ptArr))
this.add(this.createPolyline(ptArr))
// this.id
}
private createPoints(posArr: Cartesian3[]) {
const points = new PointPrimitiveCollection()
for (const pos in posArr) {
points.add({
position: pos,
color: Color.RED,
pixelSize: 10,
})
}
return points
}
private createPolyline(posArr: Cartesian3[]) {
return new PolylineGeometry({
positions: posArr,
width: 2,
colors: new Array(posArr.length).fill(Color.fromCssColorString('green')),
})
}
}
export { PointEntity, Polyline, PolylineEntity, PolygonEntity }