78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
import type { MapGeoJSONFeature } from "maplibre-gl";
|
|
import type { DetailFeature } from "../types";
|
|
import { formatValue } from "../utils/format-value";
|
|
import { SOURCE_LAYERS, type WaterNetworkSourceId } from "./sources";
|
|
|
|
export function getFeatureId(feature: MapGeoJSONFeature) {
|
|
const layer = getWaterNetworkSourceId(feature);
|
|
const raw = layer === "scada"
|
|
? feature.properties?.sensor_id ?? feature.id
|
|
: feature.id ?? feature.properties?.id;
|
|
|
|
return raw === undefined || raw === null ? "" : String(raw);
|
|
}
|
|
|
|
export function getWaterNetworkSourceId(feature: MapGeoJSONFeature): WaterNetworkSourceId | null {
|
|
const entry = Object.entries(SOURCE_LAYERS).find(([, sourceLayer]) => sourceLayer === feature.sourceLayer);
|
|
return (entry?.[0] as WaterNetworkSourceId | undefined) ?? null;
|
|
}
|
|
|
|
export function toDetailFeature(feature: MapGeoJSONFeature): DetailFeature {
|
|
const layer = getWaterNetworkSourceId(feature);
|
|
if (!layer) {
|
|
throw new Error(`Unsupported water network source layer: ${feature.sourceLayer ?? "unknown"}`);
|
|
}
|
|
|
|
const id = getFeatureId(feature);
|
|
const properties = feature.properties ?? {};
|
|
const presentation = getFeaturePresentation(layer, id, properties);
|
|
|
|
return {
|
|
id,
|
|
layer,
|
|
...presentation,
|
|
properties
|
|
};
|
|
}
|
|
|
|
function getFeaturePresentation(
|
|
layer: WaterNetworkSourceId,
|
|
id: string,
|
|
properties: Record<string, unknown>
|
|
) {
|
|
const displayId = id || "未命名";
|
|
|
|
switch (layer) {
|
|
case "conduits":
|
|
return {
|
|
title: `管渠 ${displayId}`,
|
|
subtitle: `DN${formatValue(properties.diameter)} · ${formatValue(properties.length)} m`
|
|
};
|
|
case "junctions":
|
|
return {
|
|
title: `检查井 ${displayId}`,
|
|
subtitle: `最大深度 ${formatValue(properties.max_depth)} m · 井底高程 ${formatValue(properties.invert_elevation)} m`
|
|
};
|
|
case "orifices":
|
|
return {
|
|
title: `孔口 ${displayId}`,
|
|
subtitle: `类型 ${formatValue(properties.orifice_type)} · 断面 ${formatValue(properties.shape)}`
|
|
};
|
|
case "pumps":
|
|
return {
|
|
title: `泵 ${displayId}`,
|
|
subtitle: `状态 ${formatValue(properties.status)} · 曲线 ${formatValue(properties.pump_curve)}`
|
|
};
|
|
case "outfalls":
|
|
return {
|
|
title: `排放口 ${displayId}`,
|
|
subtitle: `类型 ${formatValue(properties.outfall_type)} · 井底高程 ${formatValue(properties.invert_elevation)} m`
|
|
};
|
|
case "scada":
|
|
return {
|
|
title: String(properties.sensor_name || `综合监测点 ${displayId}`),
|
|
subtitle: `综合监测点 · SWMM 节点 ${formatValue(properties.swmm_node)}`
|
|
};
|
|
}
|
|
}
|