Files
next-tjwater-frontend/src/features/workbench/map/feature-adapter.ts
T

112 lines
3.7 KiB
TypeScript

import type { MapGeoJSONFeature } from "maplibre-gl";
import type { DetailFeature } from "../types";
import { formatValue } from "../utils/format-value";
import {
SOURCE_LAYERS,
WATER_NETWORK_SOURCE_IDS,
isSourceGisSourceId,
type GeoServerWaterNetworkSourceId,
type WaterNetworkSourceId
} from "./sources";
export function getFeatureId(feature: MapGeoJSONFeature) {
const raw = feature.properties?.device_id ?? feature.properties?.id ?? feature.id;
return raw === undefined || raw === null ? "" : String(raw);
}
export function getWaterNetworkSourceId(feature: MapGeoJSONFeature): WaterNetworkSourceId | null {
if ((WATER_NETWORK_SOURCE_IDS as readonly string[]).includes(feature.source)) {
return feature.source as WaterNetworkSourceId;
}
for (const [sourceId, sourceLayer] of Object.entries(SOURCE_LAYERS) as [GeoServerWaterNetworkSourceId, string][]) {
if (feature.sourceLayer === sourceLayer) {
return sourceId;
}
}
return null;
}
export function toDetailFeature(feature: MapGeoJSONFeature): DetailFeature {
const sourceId = getWaterNetworkSourceId(feature) ?? "junctions";
const id = getFeatureId(feature);
const labels = getFeatureLabels(sourceId, id, feature.properties ?? {});
return {
id,
layer: sourceId,
title: labels.title,
subtitle: labels.subtitle,
properties: feature.properties ?? {}
};
}
function getFeatureLabels(sourceId: WaterNetworkSourceId, id: string, properties: Record<string, unknown>) {
if (sourceId === "water_quality_monitoring_points") {
return {
title: `水质监测点 ${formatValue(properties.monitoring_point_no)}`,
subtitle: `节点 ${formatValue(properties.node_id ?? id)} · 灵敏度分析选点`
};
}
if (isSourceGisSourceId(sourceId)) {
const sourceName = formatValue(properties.source_layer_name);
const sourceIdLabel = formatValue(properties.source_object_id ?? properties.source_fid ?? id);
return {
title: `${sourceName} ${sourceIdLabel}`,
subtitle: sourceId === "fx_20200423_pipes" || sourceId === "fx_20200811_dn100_pipes"
? `管径 ${formatValue(properties.diameter)} · 长度 ${formatValue(properties.length)} m`
: `${formatValue(properties.dataset_title)} · 源要素 ${formatValue(properties.source_fid)}`
};
}
if (sourceId === "pipes") {
return {
title: `管线 ${id || "未命名"}`,
subtitle: `DN${formatValue(properties.diameter)} · ${formatValue(properties.length)} m`
};
}
if (sourceId === "valves") {
return {
title: `阀门 ${id || "未命名"}`,
subtitle: `DN${formatValue(properties.diameter)} · ${formatValue(properties.valve_type)}`
};
}
if (sourceId === "reservoirs") {
return {
title: `水库 ${id || "未命名"}`,
subtitle: `水头 ${formatValue(properties.head)} · 模式 ${formatValue(properties.pattern_id)}`
};
}
if (sourceId === "scada") {
const associatedElementId = properties.node_id ?? properties.link_id;
return {
title: `SCADA ${id || "未命名"}`,
subtitle: `${formatValue(properties.device_type)} · 关联 ${formatValue(associatedElementId)}`
};
}
if (sourceId === "pumps") {
return {
title: `水泵 ${id || "未命名"}`,
subtitle: `${formatValue(properties.start_node_id)} → ${formatValue(properties.end_node_id)}`
};
}
if (sourceId === "tanks") {
return {
title: `水箱 ${id || "未命名"}`,
subtitle: `初始水位 ${formatValue(properties.initial_level)} · 高程 ${formatValue(properties.elevation)} m`
};
}
return {
title: `节点 ${id || "未命名"}`,
subtitle: `基础需水量 ${formatValue(properties.base_demand)} · 高程 ${formatValue(properties.elevation)} m`
};
}