import type { EditHandle } from '../edit/types'; import { azimuthBetween, haversineMeters, type Coordinate } from '../edit/meters'; import type { Road } from '../types/state'; import { labelForHandle } from '../edit/parameter'; const DIRECTIONS = ['北', '东北', '东', '东南', '南', '西南', '西', '西北']; function directionOf(azimuth: number): string { return DIRECTIONS[Math.round((((azimuth % 360) + 360) % 360) / 45) % 8]; } function roadForKey(roads: Road[], key: string): Road | undefined { return roads.find((road) => road.id === key || road.segmentId === key); } function approachDirection(road: Road, junction: Coordinate): string | null { if (road.centerline.length < 2) return null; const first = road.centerline[0]; const last = road.centerline[road.centerline.length - 1]; const fromStart = haversineMeters(junction, first) <= haversineMeters(junction, last); const outside = fromStart ? road.centerline[1] : road.centerline[road.centerline.length - 2]; return directionOf(azimuthBetween(junction, outside)); } export function junctionRoadName(road: Road | undefined, roads: Road[] = []): string { const name = road?.tags?.name?.trim(); if (name) return name; const index = road ? roads.findIndex((item) => item.id === road.id) : -1; return `道路${index >= 0 ? index + 1 : 1}`; } function approachName(road: Road | undefined, roads: Road[], junction: Coordinate): string { if (!road) return '进口'; const direction = approachDirection(road, junction); return `${junctionRoadName(road, roads)}${direction ? ` · ${direction}进口` : ' · 进口'}`; } /** User-facing labels for JunctionTools. Internal ids never appear in this text. */ export function junctionLabelForHandle(handle: EditHandle, roads: Road[], junction: Coordinate): string { const base = labelForHandle(handle).replace(/^.* · /, ''); if ( (handle.kind === 'junction-approach-width' || handle.kind === 'junction-cutback') && handle.anchor.type === 'junction-approach' ) { const anchor = handle.anchor; const road = roads.find((item) => item.segmentId === anchor.segmentId); return `${approachName(road, roads, junction)} · ${base}`; } if (handle.kind === 'junction-corner-radius' && handle.anchor.type === 'junction-corner') { const incoming = roadForKey(roads, handle.anchor.incomingRoadId); const outgoing = roadForKey(roads, handle.anchor.outgoingRoadId); return `${approachName(incoming, roads, junction)} → ${approachName(outgoing, roads, junction)} · ${base}`; } if ( (handle.kind === 'junction-crosswalk-inset' || handle.kind === 'junction-stop-line-offset') && handle.anchor.type === 'junction-crossing' ) { const road = roadForKey(roads, handle.anchor.roadId) ?? roadForKey(roads, handle.anchor.segmentId); return `${approachName(road, roads, junction)} · ${base}`; } const clusterAnchor = handle.anchor; if ( (handle.kind === 'cluster-carriageway-width' || handle.kind === 'cluster-carriageway-cutback') && clusterAnchor.type === 'cluster-carriageway' ) { const road = roads.find((item) => clusterAnchor.sourceRoadIds.includes(item.id)); return `${approachName(road, roads, junction)} · ${base}`; } return base; }