2026-04-13 10:30:03 +08:00
|
|
|
import type { Point } from '@/types/base/Point';
|
|
|
|
|
import type { PrimitiveOptions } from '@/types/cesium/PrimitiveOptions';
|
|
|
|
|
import { CesiumUtilsSingleton } from '@/utils/cesium/CesiumUtils';
|
|
|
|
|
import {
|
|
|
|
|
Cartesian3,
|
|
|
|
|
HorizontalOrigin,
|
|
|
|
|
NearFarScalar,
|
|
|
|
|
VerticalOrigin,
|
|
|
|
|
} from 'cesium';
|
2026-04-11 10:09:40 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 公共批量处理点钩子函数
|
2026-04-13 22:28:56 +08:00
|
|
|
* @returns 添加点的方法
|
2026-04-11 10:09:40 +08:00
|
|
|
*/
|
|
|
|
|
export const usePointsHandle = () => {
|
2026-04-13 10:30:03 +08:00
|
|
|
/**
|
2026-04-13 22:28:56 +08:00
|
|
|
* 批量添加点
|
|
|
|
|
* @param points - 点数据数组
|
2026-04-13 10:30:03 +08:00
|
|
|
* @param getDisasterIcon - 获取灾害图标的函数
|
|
|
|
|
* @param prefix - 前缀
|
2026-04-14 17:05:27 +08:00
|
|
|
* @param isDefault - 是否为默认图元,默认false
|
2026-04-13 10:30:03 +08:00
|
|
|
* @returns 点的ID列表
|
|
|
|
|
*/
|
|
|
|
|
function addPoints(
|
|
|
|
|
points: Point[],
|
|
|
|
|
getDisasterIcon: (disasterType?: string) => string,
|
2026-04-14 17:05:27 +08:00
|
|
|
prefix: string,
|
|
|
|
|
isDefault: boolean = false
|
2026-04-27 21:36:37 +08:00
|
|
|
): { ids: string[]; names: string[] } {
|
2026-04-13 10:30:03 +08:00
|
|
|
// 设置加载配置
|
|
|
|
|
const options: PrimitiveOptions[] = [];
|
2026-04-11 10:09:40 +08:00
|
|
|
|
2026-04-27 21:36:37 +08:00
|
|
|
// 存放id、name
|
2026-04-13 10:30:03 +08:00
|
|
|
const ids: string[] = [];
|
2026-04-27 21:36:37 +08:00
|
|
|
const names: string[] = [];
|
2026-04-11 10:09:40 +08:00
|
|
|
|
2026-04-13 10:30:03 +08:00
|
|
|
points.forEach((point) => {
|
|
|
|
|
try {
|
|
|
|
|
if (point.lon === undefined || point.lat === undefined) {
|
|
|
|
|
throw new Error(`点位数据缺少经纬度:${point.id}`);
|
|
|
|
|
}
|
|
|
|
|
// 将经纬度转换为笛卡尔坐标,太高一点高度,方便点击
|
|
|
|
|
const position = Cartesian3.fromDegrees(point.lon, point.lat, 10);
|
2026-04-11 10:09:40 +08:00
|
|
|
|
2026-04-13 10:30:03 +08:00
|
|
|
const id = `${prefix}${point.id}`;
|
2026-04-27 21:36:37 +08:00
|
|
|
const name = point.name!;
|
2026-04-13 10:30:03 +08:00
|
|
|
ids.push(id);
|
2026-04-27 21:36:37 +08:00
|
|
|
names.push(name);
|
|
|
|
|
|
2026-04-13 10:30:03 +08:00
|
|
|
options.push({
|
|
|
|
|
id: id,
|
|
|
|
|
type: 'billboard',
|
|
|
|
|
positions: [position],
|
|
|
|
|
image: getDisasterIcon(point.disasterType),
|
|
|
|
|
scale: 0.8,
|
|
|
|
|
width: 40,
|
2026-04-14 17:05:27 +08:00
|
|
|
isDefault: isDefault,
|
2026-04-13 10:30:03 +08:00
|
|
|
scaleByDistance: new NearFarScalar(500, 1, 5e5, 0.3),
|
|
|
|
|
customProperties: {
|
|
|
|
|
verticalOrigin: VerticalOrigin.BOTTOM,
|
|
|
|
|
horizontalOrigin: HorizontalOrigin.CENTER,
|
|
|
|
|
height: 40,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(`处理点位失败:${error}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// 批量创建图层
|
|
|
|
|
CesiumUtilsSingleton.addPrimitivesBatch(options);
|
2026-04-11 10:09:40 +08:00
|
|
|
|
2026-04-27 21:36:37 +08:00
|
|
|
return { ids, names };
|
2026-04-13 10:30:03 +08:00
|
|
|
}
|
2026-04-11 10:09:40 +08:00
|
|
|
|
2026-04-13 10:30:03 +08:00
|
|
|
return { addPoints };
|
|
|
|
|
};
|