import { useStatusStore } from '@/stores/useStatusStore'; import { useLoadingResourceStore } from '@/stores/useLoadingResourceStore'; import type { PointResource, PointResourceCategory, ResourceConfig } from '@/types/common/useAroundAnalysisType'; /** * 判断资源分类是否可见 */ export const isCategoryVisible = ( category: PointResourceCategory, originalType?: string ): boolean => { const statusStore = useStatusStore(); const poi = statusStore.poiLayers; const map = statusStore.mapLayers; const infra = statusStore.infrastructureLayers; const visibilityMap: Record boolean> = { school: () => poi.showSchool.show, hospital: () => poi.showHospital.show, danger: () => poi.showDangerSource.show, shelter: () => poi.showRefugeeShelter.show, fire: () => poi.showFireStation.show, store: () => poi.showReservePoint.show, subway: () => poi.showSubwayStation.show, 'risk-point': () => map.riskPointShow.show, bridge: () => infra.showBridge.show, reservoir: () => infra.showReservoir.show, 'hidden-danger': () => { const hiddenMap: Record boolean> = { landslide: () => poi.showLandslideHiddenPoint.show, debris_flow: () => poi.showDebrisFlowHiddenPoint.show, water_logging: () => poi.showWaterLoggingHiddenPoint.show, flash_flood: () => poi.showFlashFloodHiddenPoint.show, collapse: () => poi.showCollapseHiddenPoint.show, }; return hiddenMap[originalType || '']?.() ?? false; }, }; return visibilityMap[category]?.() ?? false; }; /** * 转换 Store 数据为资源格式 */ export const convertStoreDataToResources = ( infoList: Record[], category: PointResource['category'], forcedType?: string ): PointResource[] => { if (!Array.isArray(infoList)) return []; return infoList.map(item => { const id = item.id || item._id || item.uuid || 'unknown_id'; const safeId = typeof id === 'string' || typeof id === 'number' ? id : 'unknown_id'; const name = item.name && String(item.name).trim() !== '' ? String(item.name) : String(safeId); return { ...item, id: safeId, value: name, category, originalType: (forcedType || item.type || item.disasterType)?.toString().toLowerCase(), }; }); }; /** * 加载所有点数据 */ export const loadAllPointData = (configs: ResourceConfig[]): PointResource[] => { const loadingResourceStore = useLoadingResourceStore(); const resources = configs.flatMap(config => convertStoreDataToResources( loadingResourceStore.getLoadingResource(config.key).info, config.category, config.forcedType ) ); const uniqueMap = new Map(); resources.forEach(item => uniqueMap.set(item.id, item)); return Array.from(uniqueMap.values()); }; /** * 计算两点间距离(Haversine 公式) */ export const calculateDistance = ( centerLon: number, centerLat: number, pointLon: unknown, pointLat: unknown ): number => { const pLon = Number(pointLon); const pLat = Number(pointLat); if (isNaN(pLon) || isNaN(pLat)) return Infinity; const dLat = (pLat - centerLat) * Math.PI / 180; const dLon = (pLon - centerLon) * Math.PI / 180; const a = Math.sin(dLat / 2) ** 2 + Math.cos(centerLat * Math.PI / 180) * Math.cos(pLat * Math.PI / 180) * Math.sin(dLon / 2) ** 2; return 6371000 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); };