feat(workbench): stabilize junction tools workspace
This commit is contained in:
1 parent
e6b822416d
commit
f10a5da325
22 files changed
+1078
-53
No files matched your search
@@ -0,0 +1,62 @@
|
||||
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}`;
|
||||
}
|
||||
return base;
|
||||
}
|
||||
Reference in new issue
Block a user