128 lines
4.2 KiB
TypeScript
128 lines
4.2 KiB
TypeScript
import * as Cesium from 'cesium';
|
|
|
|
import type { HCesiumManager } from './HCesiumManager';
|
|
import type { I站点 } from './HCesiumManager.types';
|
|
|
|
export class HCesiumStationManager {
|
|
private viewerManager: HCesiumManager;
|
|
// 用于存储当前由此管理器管理的地面站实体的 Map
|
|
private currentStationEntities: Map<string, Cesium.Entity> = new Map();
|
|
|
|
constructor(viewerManager: HCesiumManager) {
|
|
this.viewerManager = viewerManager;
|
|
}
|
|
|
|
/**
|
|
* 向视图中添加或更新地面站实体。
|
|
* 注意:当前实现仅添加,如果已存在则警告并返回现有实体。
|
|
*/
|
|
addOrUpdateStation(options: I站点): undefined {
|
|
const existingEntity = this.currentStationEntities.get(options.id);
|
|
if (existingEntity) {
|
|
console.warn(`ID 为 "${options.id}" 的地面站实体已由管理器追踪,跳过添加。`);
|
|
return;
|
|
}
|
|
|
|
// 解构赋值获取站点信息
|
|
const { height = 0, id, latitude, longitude, name, pixelSize = 10 } = options;
|
|
const position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height);
|
|
|
|
const stationEntity = this._createStationEntity(id, name, position, pixelSize);
|
|
|
|
const addedEntity = this.viewerManager.addEntity(stationEntity);
|
|
if (addedEntity) {
|
|
this.currentStationEntities.set(id, addedEntity); // 添加成功后,将其存入 Map
|
|
} else {
|
|
console.error(`通过 ViewerManager 添加 ID 为 "${id}" 的地面站实体失败。`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建地面站点的 Cesium 实体对象。
|
|
* 这是一个内部辅助方法,封装了实体创建的细节。
|
|
*/
|
|
private _createStationEntity(
|
|
id: string,
|
|
name: string,
|
|
position: Cesium.Cartesian3,
|
|
pixelSize: number,
|
|
): Cesium.Entity {
|
|
return new Cesium.Entity({
|
|
id, // 使用传入的 id 作为实体的唯一标识符
|
|
name,
|
|
position,
|
|
point: {
|
|
pixelSize,
|
|
color: Cesium.Color.fromRandom({ alpha: 1 }), // 确保 alpha 为 1
|
|
outlineColor: Cesium.Color.WHITE,
|
|
outlineWidth: 2,
|
|
},
|
|
label: {
|
|
text: name,
|
|
font: '14pt sans-serif',
|
|
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
|
outlineWidth: 2,
|
|
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
|
|
pixelOffset: new Cesium.Cartesian2(0, -12), // 标签略微偏移到点的上方
|
|
},
|
|
});
|
|
}
|
|
/**
|
|
* 从视图中移除指定的地面站实体 (通过 ID)。
|
|
*/
|
|
removeStation(entityId: string): boolean {
|
|
const entityToRemove = this.currentStationEntities.get(entityId);
|
|
if (!entityToRemove) {
|
|
// console.warn(`未在 GroundStationManager 中找到 ID 为 "${entityId}" 的实体,无法移除。`);
|
|
return false; // 不在管理范围内
|
|
}
|
|
|
|
const removed = this.viewerManager.removeEntity(entityToRemove); // 优先使用实体对象移除
|
|
if (removed) {
|
|
// 移除成功后,从 Map 中删除
|
|
this.currentStationEntities.delete(entityId);
|
|
return true;
|
|
} else {
|
|
console.warn(`尝试通过 ViewerManager 移除 ID 为 "${entityId}" 的地面站实体失败。`);
|
|
// 即使 ViewerManager 移除失败,也尝试从内部 Map 中移除,以保持一致性?
|
|
// 或者保留在 Map 中以供调试?暂时选择移除。
|
|
this.currentStationEntities.delete(entityId);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 清除所有由此管理器管理的地面站实体。
|
|
*/
|
|
clearAllStations(): void {
|
|
// 迭代 Map 的 ID 进行移除,避免在迭代 Map 值时修改 Map
|
|
const idsToRemove = [...this.currentStationEntities.keys()];
|
|
let removalFailed = false;
|
|
for (const id of idsToRemove) {
|
|
if (!this.removeStation(id)) {
|
|
removalFailed = true; // 记录是否有移除失败的情况
|
|
}
|
|
}
|
|
if (removalFailed) {
|
|
console.warn('清除部分地面站实体时遇到问题。');
|
|
}
|
|
// 确保最终清空 Map
|
|
this.currentStationEntities.clear();
|
|
}
|
|
|
|
/**
|
|
* 获取当前管理的地面站实体 Map。
|
|
* @returns 返回包含当前地面站实体的 Map。
|
|
*/
|
|
getCurrentStationEntities(): Map<string, Cesium.Entity> {
|
|
return this.currentStationEntities;
|
|
}
|
|
|
|
/**
|
|
* 销毁管理器,清理资源。
|
|
*/
|
|
destroy(): void {
|
|
this.clearAllStations();
|
|
}
|
|
}
|