Files
xian_vue_new/src/utils/cesium/CesiumUtils.ts
T

454 lines
15 KiB
TypeScript
Raw Normal View History

2026-04-07 20:01:06 +08:00
import type { CesiumInitOptions } from '@/types/cesium/CesiumInitOptions'
import type { EntityOptions } from '@/types/cesium/EntityOptions'
import type { PrimitiveOptions } from '@/types/cesium/PrimitiveOptions'
import type { LayerConfig } from '@/types/cesium/LayerConfig'
2026-04-08 18:17:54 +08:00
import type { CustomizeGeoJsonDataSource, GeoJsonOptions } from '@/types/cesium/GeoJsonOptions'
2026-04-08 18:59:00 +08:00
import { Viewer, Entity, DataSource, ImageryLayer, Primitive, BillboardCollection, Cartesian3 } from 'cesium'
import { CesiumViewerManager } from './CesiumViewerManager'
import { EntityManager } from './EntityManager'
import { PrimitiveManager } from './PrimitiveManager'
import { LayerManager } from './LayerManager'
import { GeoJsonManager, type ClearType } from './GeoJsonManager'
import { CameraController } from './CameraController'
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
// 导出 ClearType 类型
export type { ClearType }
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:59:00 +08:00
* Cesium 工具类(重构版 - 委托模式)
2026-04-07 20:01:06 +08:00
*/
export class CesiumUtils {
2026-04-08 18:59:00 +08:00
// 管理器实例
#viewerManager: CesiumViewerManager
#entityManager: EntityManager | null = null
#primitiveManager: PrimitiveManager | null = null
#layerManager: LayerManager | null = null
#geoJsonManager: GeoJsonManager | null = null
#cameraController: CameraController | null = null
2026-04-07 20:01:06 +08:00
constructor() {
2026-04-08 18:59:00 +08:00
this.#viewerManager = new CesiumViewerManager()
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 初始化 Cesium Viewer
2026-04-08 18:59:00 +08:00
* @param options - Viewer 初始化选项
* @param tdMapToken - 天地图 Token 数组(可选)
* @param type - 底图类型:0=影像图,1=矢量图(默认 0)
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
initCesiumViewer(options: CesiumInitOptions, tdMapToken?: string[], type: number = 0): void {
2026-04-08 18:59:00 +08:00
this.#viewerManager.initCesiumViewer(options, tdMapToken, type)
const viewer = this.#viewerManager.getViewer()
if (viewer) {
this.#entityManager = new EntityManager(viewer)
this.#primitiveManager = new PrimitiveManager(viewer)
this.#layerManager = new LayerManager(viewer)
this.#geoJsonManager = new GeoJsonManager(viewer)
this.#cameraController = new CameraController(viewer)
2026-04-07 20:01:06 +08:00
}
2026-04-08 18:17:54 +08:00
}
/**
* 销毁 Cesium Viewer
*/
destroyCesiumViewer(): void {
2026-04-08 18:59:00 +08:00
this.#viewerManager.destroyCesiumViewer(() => this.clearAllResources('all'))
2026-04-07 20:01:06 +08:00
}
2026-04-08 18:17:54 +08:00
// ===================== 实体管理 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:59:00 +08:00
* 添加 Cesium 实体
* @param entityOptions - 实体配置选项
* @returns 创建的 Entity 实例
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
addCesiumEntity(entityOptions: EntityOptions): Entity {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#entityManager, 'EntityManager')
return this.#entityManager!.addCesiumEntity(entityOptions)
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 查询实体
2026-04-08 18:59:00 +08:00
* @param entityId - 实体 ID
* @returns Entity 实例,不存在则返回 null
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
getCesiumEntityById(entityId: string): Entity | null {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#entityManager, 'EntityManager')
return this.#entityManager!.getCesiumEntityById(entityId)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 删除实体
2026-04-08 18:59:00 +08:00
* @param entityId - 实体 ID
* @returns 是否删除成功
2026-04-08 18:17:54 +08:00
*/
removeCesiumEntity(entityId: string): boolean {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#entityManager, 'EntityManager')
return this.#entityManager!.removeCesiumEntity(entityId)
2026-04-07 20:01:06 +08:00
}
/**
* 批量删除实体
2026-04-08 18:59:00 +08:00
* @param entityIds - 实体 ID 数组
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
batchRemoveCesiumEntities(entityIds: string[]): void {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#entityManager, 'EntityManager')
this.#entityManager!.batchRemoveCesiumEntities(entityIds)
}
/**
* 清除实体
* @param clearType - 清除类型:'default'=默认实体,'custom'=自定义实体,'all'=所有实体(默认 'custom'
*/
clearAllEntities(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#entityManager, 'EntityManager')
this.#entityManager!.clearAllEntities(clearType)
2026-04-07 20:01:06 +08:00
}
/**
* 获取所有实体ID
* @param clearType - 类型:'default'=默认实体,'custom'=自定义实体,'all'=所有实体(默认 'all'
* @returns 实体 ID 集合
*/
getEntityIds(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#entityManager, 'EntityManager')
return this.#entityManager!.getEntityIds(clearType)
}
2026-04-08 18:17:54 +08:00
// ===================== Primitive 管理 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 批量添加 Primitive
2026-04-08 18:59:00 +08:00
* @param primitives - Primitive 配置选项数组
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
addPrimitivesBatch(primitives: PrimitiveOptions[]): void {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#primitiveManager, 'PrimitiveManager')
this.#primitiveManager!.addPrimitivesBatch(primitives)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 查询 Primitive
2026-04-08 18:59:00 +08:00
* @param id - Primitive ID
* @returns Primitive 或 BillboardCollection 实例,不存在则返回 undefined
2026-04-08 18:17:54 +08:00
*/
getPrimitiveById(id: string): Primitive | BillboardCollection | undefined {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#primitiveManager, 'PrimitiveManager')
return this.#primitiveManager!.getPrimitiveById(id)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 删除 Primitive
2026-04-08 18:59:00 +08:00
* @param id - Primitive ID
* @returns 是否删除成功
2026-04-08 18:17:54 +08:00
*/
removePrimitiveById(id: string): boolean {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#primitiveManager, 'PrimitiveManager')
return this.#primitiveManager!.removePrimitiveById(id)
}
2026-04-08 18:17:54 +08:00
2026-04-08 18:59:00 +08:00
/**
* 清除 Primitive
* @param clearType - 清除类型:'default'=默认 Primitive'custom'=自定义 Primitive'all'=所有 Primitive(默认 'custom'
*/
clearAllPrimitives(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager')
this.#primitiveManager!.clearAllPrimitives(clearType)
2026-04-07 20:01:06 +08:00
}
/**
* 获取所有Primitive ID
* @param clearType - 类型:'default'=默认 Primitive'custom'=自定义 Primitive'all'=所有 Primitive(默认 'all'
* @returns Primitive ID 集合
*/
getPrimitiveIds(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#primitiveManager, 'PrimitiveManager')
return this.#primitiveManager!.getPrimitiveIds(clearType)
}
2026-04-08 18:17:54 +08:00
// ===================== 图层管理 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 创建图层
2026-04-08 18:59:00 +08:00
* @param layerConfig - 图层配置
* @returns 创建的 ImageryLayer 实例,失败则返回 null
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
createLayer(layerConfig: LayerConfig): ImageryLayer | null {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#layerManager, 'LayerManager')
return this.#layerManager!.createLayer(layerConfig)
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 查询图层
2026-04-08 18:59:00 +08:00
* @param key - 图层 key
* @returns ImageryLayer 实例,不存在则返回 undefined
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
getLayerByKey(key: string): ImageryLayer | undefined {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#layerManager, 'LayerManager')
return this.#layerManager!.getLayerByKey(key)
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 删除图层
2026-04-08 18:59:00 +08:00
* @param key - 图层 key
* @returns 是否删除成功
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
removeLayerByKey(key: string): boolean {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#layerManager, 'LayerManager')
return this.#layerManager!.removeLayerByKey(key)
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:17:54 +08:00
* 批量删除图层
2026-04-08 18:59:00 +08:00
* @param layerIds - 图层 ID 数组
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
batchRemoveLayers(layerIds: string[]): void {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#layerManager, 'LayerManager')
this.#layerManager!.batchRemoveLayers(layerIds)
}
/**
* 清除图层
* @param clearType - 清除类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'custom'
*/
clearAllLayers(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#layerManager, 'LayerManager')
this.#layerManager!.clearAllLayers(clearType)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
/**
* 获取所有图层 Key
* @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all'
* @returns 图层 Key 集合
*/
getLayerKeys(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#layerManager, 'LayerManager')
return this.#layerManager!.getLayerKeys(clearType)
}
2026-04-08 18:17:54 +08:00
// ===================== GeoJSON 图层管理 =====================
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 添加 GeoJSON 图层
2026-04-08 18:59:00 +08:00
* @param layerId - 图层唯一标识
* @param geojsonData - GeoJSON 数据(路径、URL 或对象)
* @param isDefault - 是否为默认图层(默认 false)
* @param options - 配置选项(样式、标签等)
* @returns Promise<DataSource> 数据源实例
2026-04-08 18:17:54 +08:00
*/
async addGeoJsonLayer(
layerId: string,
geojsonData: CustomizeGeoJsonDataSource,
isDefault: boolean = false,
options?: GeoJsonOptions
): Promise<DataSource> {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.addGeoJsonLayer(layerId, geojsonData, isDefault, options)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 根据ID查询图层
* @param layerId - 图层 ID
* @returns DataSource 实例,不存在则返回 undefined
*/
2026-04-08 18:17:54 +08:00
getGeoJsonLayerById(layerId: string): DataSource | undefined {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.getGeoJsonLayerById(layerId)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 删除图层
* @param layerId - 图层 ID
* @returns 是否删除成功
*/
2026-04-08 18:17:54 +08:00
removeGeoJsonLayer(layerId: string): boolean {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.removeGeoJsonLayer(layerId)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
* 批量添加GeoJSON图层
2026-04-08 18:59:00 +08:00
* @param layerIds - 图层 ID 数组
* @param geojsonDatas - GeoJSON 数据数组
2026-04-09 10:51:58 +08:00
* @param options - 配置选项数组(包含 isDefault)
2026-04-08 18:59:00 +08:00
*/
async batchAddGeoJsonLayers(
2026-04-08 18:59:00 +08:00
layerIds: string[],
geojsonDatas: CustomizeGeoJsonDataSource[],
options?: GeoJsonOptions[]
): Promise<void> {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
2026-04-09 10:51:58 +08:00
await this.#geoJsonManager!.batchAddGeoJsonLayers(layerIds, geojsonDatas, options)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 批量删除
* @param layerIds - 图层 ID 数组
*/
2026-04-08 18:17:54 +08:00
batchRemoveGeoJsonLayers(layerIds: string[]): void {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
this.#geoJsonManager!.batchRemoveGeoJsonLayers(layerIds)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 清空图层
* @param clearType - 清除类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'custom'
*/
clearAllGeoJsonLayers(clearType: ClearType = 'custom'): void {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
this.#geoJsonManager!.clearAllGeoJsonLayers(clearType)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 显示图层
* @param layerId - 图层 ID
* @returns 是否操作成功
*/
2026-04-08 18:17:54 +08:00
showGeoJsonLayer(layerId: string): boolean {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.showGeoJsonLayer(layerId)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:59:00 +08:00
/**
* 隐藏图层
* @param layerId - 图层 ID
* @returns 是否操作成功
*/
2026-04-08 18:17:54 +08:00
hideGeoJsonLayer(layerId: string): boolean {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.hideGeoJsonLayer(layerId)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
2026-04-08 18:59:00 +08:00
* 切换显隐
* @param layerId - 图层 ID
* @returns 切换后的显示状态,图层不存在则返回 null
2026-04-08 18:17:54 +08:00
*/
2026-04-08 18:59:00 +08:00
toggleGeoJsonLayer(layerId: string): boolean | null {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.toggleGeoJsonLayer(layerId)
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:59:00 +08:00
* 批量显示
* @param layerIds - 图层 ID 数组
* @returns 成功显示的图层数量
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:59:00 +08:00
batchShowGeoJsonLayers(layerIds: string[]): number {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.batchShowGeoJsonLayers(layerIds)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
/**
2026-04-08 18:59:00 +08:00
* 批量隐藏
* @param layerIds - 图层 ID 数组
* @returns 成功隐藏的图层数量
2026-04-08 18:17:54 +08:00
*/
2026-04-08 18:59:00 +08:00
batchHideGeoJsonLayers(layerIds: string[]): number {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.batchHideGeoJsonLayers(layerIds)
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:59:00 +08:00
* 获取显示状态
* @param layerId - 图层 ID
* @returns 显示状态,图层不存在则返回 null
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:59:00 +08:00
getGeoJsonLayerVisibility(layerId: string): boolean | null {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.getGeoJsonLayerVisibility(layerId)
2026-04-07 20:01:06 +08:00
}
/**
* 获取所有 GeoJSON 图层 ID
* @param clearType - 类型:'default'=默认图层,'custom'=自定义图层,'all'=所有图层(默认 'all'
* @returns GeoJSON 图层 ID 集合
*/
getGeoJsonLayerIds(clearType: ClearType = 'all'): Set<string> {
this.#checkManager(this.#geoJsonManager, 'GeoJsonManager')
return this.#geoJsonManager!.getGeoJsonLayerIds(clearType)
}
2026-04-08 18:17:54 +08:00
// ===================== 视角控制 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 飞行到目标位置
2026-04-08 18:59:00 +08:00
* @param target - 目标位置 [经度, 纬度, 高度] 或 Cartesian3
* @param duration - 飞行持续时间(秒,默认 2)
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
flyToTarget(target: [number, number, number] | Cartesian3, duration = 2): void {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#cameraController, 'CameraController')
this.#cameraController!.flyToTarget(target, duration)
2026-04-07 20:01:06 +08:00
}
/**
2026-04-08 18:59:00 +08:00
* 跳转到目标位置
* @param target - 目标位置 [经度, 纬度, 高度] 或 Cartesian3
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
viewToTarget(target: [number, number, number] | Cartesian3): void {
2026-04-08 18:59:00 +08:00
this.#checkManager(this.#cameraController, 'CameraController')
this.#cameraController!.viewToTarget(target)
2026-04-08 18:17:54 +08:00
}
2026-04-07 20:01:06 +08:00
2026-04-08 18:17:54 +08:00
// ===================== 清除与资源管理 =====================
2026-04-07 20:01:06 +08:00
/**
2026-04-08 18:17:54 +08:00
* 清除所有资源
2026-04-08 18:59:00 +08:00
* @param clearType - 清除类型:'default'=默认资源,'custom'=自定义资源,'all'=所有资源(默认 'custom'
2026-04-07 20:01:06 +08:00
*/
2026-04-08 18:17:54 +08:00
clearAllResources(clearType: ClearType = 'custom'): void {
this.clearAllEntities(clearType)
this.clearAllPrimitives(clearType)
this.clearAllLayers(clearType)
this.clearAllGeoJsonLayers(clearType)
}
// ===================== getter 和 setter函数 =====================
2026-04-08 18:59:00 +08:00
/**
* 获取 Viewer 实例
* @returns Viewer 实例,如果未初始化则返回 null
*/
2026-04-08 18:17:54 +08:00
getViewer(): Viewer | null {
2026-04-08 18:59:00 +08:00
return this.#viewerManager.getViewer()
2026-04-08 18:17:54 +08:00
}
// ===================== 辅助函数 =====================
2026-04-08 18:59:00 +08:00
/**
* 转换位置坐标
* @param pos - 位置坐标
* @returns Cartesian3 坐标
*/
2026-04-07 20:01:06 +08:00
convertPosition(pos: Cartesian3 | [number, number, number]): Cartesian3 {
return Array.isArray(pos) ? Cartesian3.fromDegrees(pos[0], pos[1], pos[2] || 0) : pos
}
2026-04-08 18:59:00 +08:00
/**
* 批量转换位置坐标
* @param positions - 位置坐标数组
* @returns Cartesian3 坐标数组
*/
2026-04-07 20:01:06 +08:00
convertPositionArray(positions: (Cartesian3 | [number, number, number])[]): Cartesian3[] {
return positions.map((pos) => this.convertPosition(pos))
}
2026-04-08 18:17:54 +08:00
// ===================== 私有方法 =====================
/**
2026-04-08 18:59:00 +08:00
* 检查管理器是否已初始化
* @param manager - 管理器实例
* @param managerName - 管理器名称(用于错误提示)
2026-04-08 18:17:54 +08:00
*/
2026-04-08 18:59:00 +08:00
#checkManager(manager: any, managerName: string): void {
if (!manager) {
throw new Error(`${managerName} 未初始化,请先调用 initCesiumViewer()`)
2026-04-08 18:17:54 +08:00
}
}
2026-04-07 20:01:06 +08:00
}
2026-04-08 18:17:54 +08:00
2026-04-08 18:59:00 +08:00
/**
* CesiumUtils 单例实例
*/
export const CesiumUtilsSingleton = new CesiumUtils()