63 lines
2.4 KiB
TypeScript
63 lines
2.4 KiB
TypeScript
import type { GeoFeature, Road, WorkbenchState } from '../types/state';
|
|
import { junctionRoadName } from './labels';
|
|
|
|
export interface JunctionFacility {
|
|
id: string;
|
|
kind: 'crosswalk' | 'stop-line' | 'signal';
|
|
label: string;
|
|
}
|
|
|
|
function text(value: unknown): string | null {
|
|
return typeof value === 'string' || typeof value === 'number' ? String(value) : null;
|
|
}
|
|
|
|
function roadLabel(feature: GeoFeature, roads: Road[]): string {
|
|
const id = text(feature.properties?.road_id);
|
|
const sourceWay = text(feature.properties?.source_way_id);
|
|
const approach = text(feature.properties?.approach_id)?.split(':')[0];
|
|
const road = roads.find(
|
|
(item) =>
|
|
item.id === id ||
|
|
(sourceWay !== null && item.osmWayIds.map(String).includes(sourceWay)) ||
|
|
(approach !== undefined && item.osmWayIds.map(String).includes(approach)),
|
|
);
|
|
return junctionRoadName(road, roads);
|
|
}
|
|
|
|
/** One user-facing item per existing facility, never one item per zebra stripe. */
|
|
export function junctionFacilities(
|
|
state: WorkbenchState,
|
|
nodeIds: readonly string[],
|
|
roads: Road[],
|
|
): JunctionFacility[] {
|
|
const nodes = new Set(nodeIds.map(String));
|
|
const result = new Map<string, JunctionFacility>();
|
|
for (const feature of state.layers.crosswalks?.features ?? []) {
|
|
const crossing = text(feature.properties?.crossing_node_id);
|
|
if (!crossing || !nodes.has(crossing)) continue;
|
|
result.set(`crosswalk:${crossing}`, {
|
|
id: `crosswalk:${crossing}`,
|
|
kind: 'crosswalk',
|
|
label: `斑马线 · ${roadLabel(feature, roads)}`,
|
|
});
|
|
}
|
|
for (const feature of state.layers.vehicleStopLines?.features ?? []) {
|
|
const crossing = text(feature.properties?.crossing_node_id);
|
|
if (!crossing || !nodes.has(crossing)) continue;
|
|
result.set(`stop-line:${crossing}`, {
|
|
id: `stop-line:${crossing}`,
|
|
kind: 'stop-line',
|
|
label: `停止线 · ${roadLabel(feature, roads)}`,
|
|
});
|
|
}
|
|
for (const feature of state.trafficSignals.assemblies.features) {
|
|
const nodeIdsForSignal = [text(feature.properties?.node_id), text(feature.properties?.control_id)].filter(
|
|
(value): value is string => value !== null,
|
|
);
|
|
if (!nodeIdsForSignal.some((node) => nodes.has(node))) continue;
|
|
const id = text(feature.properties?.signal_uid) ?? `signal:${nodeIdsForSignal[0]}`;
|
|
result.set(`signal:${id}`, { id: `signal:${id}`, kind: 'signal', label: `信号灯 · ${roadLabel(feature, roads)}` });
|
|
}
|
|
return [...result.values()];
|
|
}
|