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
@@ -4,6 +4,9 @@ import { canEditJunction, type JunctionSession } from './workspace';
|
||||
import { Button } from '../ui/button';
|
||||
import { ParameterPanel } from '../components/ParameterPanel';
|
||||
import type { EditHandle } from '../edit/types';
|
||||
import type { Road } from '../types/state';
|
||||
import type { Coordinate } from '../edit/meters';
|
||||
import { junctionLabelForHandle, junctionRoadName } from './labels';
|
||||
|
||||
interface Props {
|
||||
descriptor: JunctionDescriptor;
|
||||
@@ -18,9 +21,13 @@ interface Props {
|
||||
onEnableEdit?: () => void;
|
||||
handles?: EditHandle[];
|
||||
onDraft?: (handle: EditHandle, value: number) => void;
|
||||
onDraftGroup?: (handles: EditHandle[], value: number) => void;
|
||||
onSettle?: (handle: EditHandle, value: number) => void;
|
||||
onSettleGroup?: (handles: EditHandle[], value: number) => void;
|
||||
onAbandon?: () => void;
|
||||
onReset?: (handle: EditHandle) => void;
|
||||
roads?: Road[];
|
||||
junctionCoordinate?: Coordinate;
|
||||
}
|
||||
|
||||
export function JunctionWorkspace({
|
||||
@@ -36,19 +43,22 @@ export function JunctionWorkspace({
|
||||
facilitySummary = { crosswalks: 0, stopLines: 0, signals: 0 },
|
||||
handles = [],
|
||||
onDraft,
|
||||
onDraftGroup,
|
||||
onSettle,
|
||||
onSettleGroup,
|
||||
onAbandon,
|
||||
onReset,
|
||||
roads = [],
|
||||
junctionCoordinate = descriptor.coordinate,
|
||||
}: Props) {
|
||||
const editable = useMemo(() => canEditJunction(descriptor), [descriptor]);
|
||||
return (
|
||||
<section className="junction-workspace" aria-label="路口编辑工作区">
|
||||
<header className="junction-workspace__header">
|
||||
<div>
|
||||
<span className="junction-workspace__eyebrow">JunctionTools</span>
|
||||
<h2>
|
||||
{descriptor.ref.type === 'node' ? '路口节点' : '复杂路口上下文'} {descriptor.ref.id}
|
||||
</h2>
|
||||
<div className="junction-workspace__title-line">
|
||||
<span className="junction-workspace__eyebrow">JunctionTools</span>
|
||||
</div>
|
||||
<p>
|
||||
{editable
|
||||
? editMode
|
||||
@@ -65,9 +75,9 @@ export function JunctionWorkspace({
|
||||
<div className="junction-workspace__body">
|
||||
<dl>
|
||||
<dt>组成节点</dt>
|
||||
<dd>{descriptor.nodeIds.join(', ') || '—'}</dd>
|
||||
<dd>{descriptor.nodeIds.length ? `${descriptor.nodeIds.length} 个路口节点` : '—'}</dd>
|
||||
<dt>进口道路</dt>
|
||||
<dd>{descriptor.approachRoadIds.join(', ') || '—'}</dd>
|
||||
<dd>{roads.length ? roads.map((road) => junctionRoadName(road, roads)).join('、') : '—'}</dd>
|
||||
<dt>诊断</dt>
|
||||
<dd>{descriptor.diagnosticIds.length ? `${descriptor.diagnosticIds.length} 条` : '无'}</dd>
|
||||
<dt>状态</dt>
|
||||
@@ -98,10 +108,14 @@ export function JunctionWorkspace({
|
||||
<ParameterPanel
|
||||
handles={handles}
|
||||
hasSelection
|
||||
grouped
|
||||
onDraft={onDraft ?? (() => undefined)}
|
||||
onDraftGroup={onDraftGroup}
|
||||
onSettle={onSettle ?? (() => undefined)}
|
||||
onSettleGroup={onSettleGroup}
|
||||
onAbandon={onAbandon ?? (() => undefined)}
|
||||
onReset={onReset ?? (() => undefined)}
|
||||
labelForHandle={(handle) => junctionLabelForHandle(handle, roads, junctionCoordinate)}
|
||||
/>
|
||||
<div className="junction-workspace__actions">
|
||||
<Button onClick={onApply} disabled={!session.dirty}>
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { EditHandle } from '../edit/types';
|
||||
import type { Road } from '../types/state';
|
||||
import { junctionLabelForHandle } from './labels';
|
||||
|
||||
const roads = [
|
||||
{
|
||||
id: 'road-north',
|
||||
segmentId: 'segment:way/-3254/0',
|
||||
centerline: [
|
||||
[120, 30],
|
||||
[120, 30.001],
|
||||
],
|
||||
sourceNodeIds: ['junction'],
|
||||
osmWayIds: [],
|
||||
tags: { name: '创新大道' },
|
||||
highway: 'primary',
|
||||
direction: 'forward',
|
||||
widthMeters: 7,
|
||||
laneCount: 2,
|
||||
sidewalkLeft: false,
|
||||
sidewalkRight: false,
|
||||
},
|
||||
{
|
||||
id: 'road-east',
|
||||
segmentId: 'segment:way/-3255/0',
|
||||
centerline: [
|
||||
[120, 30],
|
||||
[120.001, 30],
|
||||
],
|
||||
sourceNodeIds: ['junction'],
|
||||
osmWayIds: [],
|
||||
tags: {},
|
||||
highway: 'residential',
|
||||
direction: 'forward',
|
||||
widthMeters: 6,
|
||||
laneCount: 2,
|
||||
sidewalkLeft: false,
|
||||
sidewalkRight: false,
|
||||
},
|
||||
] as const;
|
||||
|
||||
function handle(anchor: EditHandle['anchor'], kind: EditHandle['kind']): EditHandle {
|
||||
return {
|
||||
handleId: 'h',
|
||||
kind,
|
||||
anchor,
|
||||
position: [120, 30],
|
||||
axisAzimuth: 0,
|
||||
value: { current: 3, min: 1, max: 8, unit: 'meter' },
|
||||
affects: [],
|
||||
editable: true,
|
||||
};
|
||||
}
|
||||
|
||||
describe('junction handle labels', () => {
|
||||
it('uses road name and geographic approach direction', () => {
|
||||
expect(
|
||||
junctionLabelForHandle(
|
||||
handle(
|
||||
{ type: 'junction-approach', nodeId: 'junction', segmentId: roads[0].segmentId },
|
||||
'junction-approach-width',
|
||||
),
|
||||
roads as unknown as Road[],
|
||||
[120, 30],
|
||||
),
|
||||
).toBe('创新大道 · 北进口 · 进口宽度');
|
||||
});
|
||||
|
||||
it('uses a readable fallback when a road has no name', () => {
|
||||
expect(
|
||||
junctionLabelForHandle(
|
||||
handle({ type: 'junction-approach', nodeId: 'junction', segmentId: roads[1].segmentId }, 'junction-cutback'),
|
||||
roads as unknown as Road[],
|
||||
[120, 30],
|
||||
),
|
||||
).toBe('道路2 · 东进口 · 进口 cutback');
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { localJunctionPreview, localJunctionState } from './local-state';
|
||||
import type { JunctionDescriptor } from './hotspots';
|
||||
import type { WorkbenchState } from '../types/state';
|
||||
|
||||
const descriptor: JunctionDescriptor = {
|
||||
ref: { type: 'node', id: 'junction-1' },
|
||||
status: 'editable',
|
||||
coordinate: [120, 30],
|
||||
radiusMeters: 10,
|
||||
nodeIds: ['junction-1'],
|
||||
approachRoadIds: ['road-a'],
|
||||
diagnosticIds: [],
|
||||
source: 'junction-plan',
|
||||
};
|
||||
|
||||
const state = {
|
||||
areaId: 'test',
|
||||
compiled: {
|
||||
model: {
|
||||
roads: [
|
||||
{
|
||||
id: 'road-a',
|
||||
segmentId: 'segment-a',
|
||||
sourceNodeIds: ['junction-1', 'end-a'],
|
||||
centerline: [],
|
||||
osmWayIds: [],
|
||||
tags: {},
|
||||
highway: 'residential',
|
||||
direction: 'forward',
|
||||
widthMeters: 6,
|
||||
laneCount: 2,
|
||||
sidewalkLeft: false,
|
||||
sidewalkRight: false,
|
||||
},
|
||||
{
|
||||
id: 'road-b',
|
||||
segmentId: 'segment-b',
|
||||
sourceNodeIds: ['other', 'end-b'],
|
||||
centerline: [],
|
||||
osmWayIds: [],
|
||||
tags: {},
|
||||
highway: 'residential',
|
||||
direction: 'forward',
|
||||
widthMeters: 6,
|
||||
laneCount: 2,
|
||||
sidewalkLeft: false,
|
||||
sidewalkRight: false,
|
||||
},
|
||||
],
|
||||
endpoints: [
|
||||
{ id: 'endpoint-a', roadId: 'road-a', side: 'end', nodeId: 'junction-1', coordinate: [120, 30] },
|
||||
{ id: 'endpoint-b', roadId: 'road-b', side: 'end', nodeId: 'other', coordinate: [121, 31] },
|
||||
],
|
||||
connections: [{ id: 'connection-a', fromEndpointId: 'endpoint-a', toEndpointId: 'endpoint-a', enabled: true }],
|
||||
},
|
||||
movements: [],
|
||||
diagnostics: [],
|
||||
},
|
||||
layers: {
|
||||
nativeRoadSurface: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
{ type: 'Feature', properties: { road_id: 'road-a' }, geometry: null },
|
||||
{ type: 'Feature', properties: { road_id: 'road-b' }, geometry: null },
|
||||
],
|
||||
},
|
||||
crosswalks: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
{ type: 'Feature', properties: { crossing_node_id: 'junction-1' }, geometry: null },
|
||||
{ type: 'Feature', properties: { crossing_node_id: 'other' }, geometry: null },
|
||||
],
|
||||
},
|
||||
},
|
||||
overrides: { schema: 'test', overrides: [] },
|
||||
trafficSignals: { provenance: 'test', assemblies: { type: 'FeatureCollection', features: [] } },
|
||||
trafficRuntime: { signals: [] },
|
||||
comparison: {},
|
||||
} as unknown as WorkbenchState;
|
||||
|
||||
describe('local junction state', () => {
|
||||
it('keeps only the selected junction roads and related features', () => {
|
||||
const local = localJunctionState(state, descriptor);
|
||||
expect(local.compiled.model.roads.map((road) => road.id)).toEqual(['road-a']);
|
||||
expect(local.layers.nativeRoadSurface?.features).toHaveLength(1);
|
||||
expect(local.layers.crosswalks?.features).toHaveLength(1);
|
||||
expect(local.compiled.model.endpoints.map((endpoint) => endpoint.id)).toEqual(['endpoint-a']);
|
||||
});
|
||||
|
||||
it('scopes preview collections to the selected junction', () => {
|
||||
const preview = localJunctionPreview(
|
||||
{
|
||||
roadSurface: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
{ type: 'Feature', properties: { road_id: 'road-a' }, geometry: null },
|
||||
{ type: 'Feature', properties: { road_id: 'road-b' }, geometry: null },
|
||||
],
|
||||
},
|
||||
crosswalks: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
{ type: 'Feature', properties: { crossing_node_id: 'junction-1' }, geometry: null },
|
||||
{ type: 'Feature', properties: { crossing_node_id: 'other' }, geometry: null },
|
||||
],
|
||||
},
|
||||
},
|
||||
descriptor,
|
||||
state.compiled.model.roads,
|
||||
);
|
||||
expect(preview?.roadSurface?.features).toHaveLength(1);
|
||||
expect(preview?.crosswalks?.features).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
import type { GeoFeature, GeoJson, Road, WorkbenchState } from '../types/state';
|
||||
import type { JunctionDescriptor } from './hotspots';
|
||||
import type { PreviewLayerName } from '../edit/types';
|
||||
|
||||
const empty: GeoJson = { type: 'FeatureCollection', features: [] };
|
||||
|
||||
function text(value: unknown): string | null {
|
||||
return typeof value === 'string' || typeof value === 'number' ? String(value) : null;
|
||||
}
|
||||
|
||||
function related(feature: GeoFeature, nodeIds: Set<string>, roadIds: Set<string>, segmentIds: Set<string>): boolean {
|
||||
const props = feature.properties || {};
|
||||
const nodeKeys = ['node_id', 'osm_node_id', 'crossing_node_id', 'junction_node_id'];
|
||||
if (
|
||||
nodeKeys.some((key) => {
|
||||
const value = text(props[key]);
|
||||
return value !== null && nodeIds.has(value);
|
||||
})
|
||||
)
|
||||
return true;
|
||||
const roadKeys = ['road_id', 'source_road_id', 'from_road_id', 'to_road_id', 'roadId'];
|
||||
if (
|
||||
roadKeys.some((key) => {
|
||||
const value = text(props[key]);
|
||||
return value !== null && roadIds.has(value);
|
||||
})
|
||||
)
|
||||
return true;
|
||||
const segment = text(props.segment_id);
|
||||
if (segment && segmentIds.has(segment)) return true;
|
||||
const directional = text(props.directional_road_ids);
|
||||
return Boolean(directional && [...roadIds].some((id) => directional.split(',').includes(id)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep server preview collections scoped to the independent junction canvas.
|
||||
* Preview responses are solved from the junction manifest, but some compiler
|
||||
* layers still contain the surrounding area; showing those collections would
|
||||
* make a keyboard edit appear to jump back to the global map.
|
||||
*/
|
||||
export function localJunctionPreview(
|
||||
preview: Partial<Record<PreviewLayerName, GeoJson | null>> | null | undefined,
|
||||
descriptor: JunctionDescriptor,
|
||||
roads: Road[],
|
||||
): Partial<Record<PreviewLayerName, GeoJson | null>> | null {
|
||||
if (!preview) return preview ?? null;
|
||||
const nodeIds = new Set(descriptor.nodeIds.map(String));
|
||||
const selectedRoads = roads.filter((road) => descriptor.approachRoadIds.includes(road.id));
|
||||
const roadIds = new Set(selectedRoads.map((road) => road.id));
|
||||
const segmentIds = new Set(selectedRoads.map((road) => road.segmentId));
|
||||
const scoped = { ...preview };
|
||||
for (const name of Object.keys(preview) as PreviewLayerName[]) {
|
||||
const value = preview[name];
|
||||
if (value === null || value === undefined) continue;
|
||||
scoped[name] = {
|
||||
type: 'FeatureCollection',
|
||||
features: value.features.filter((feature) => related(feature, nodeIds, roadIds, segmentIds)),
|
||||
};
|
||||
}
|
||||
return scoped;
|
||||
}
|
||||
|
||||
/** Build the intentionally small state consumed by the independent junction canvas. */
|
||||
export function localJunctionState(state: WorkbenchState, descriptor: JunctionDescriptor): WorkbenchState {
|
||||
const nodeIds = new Set(descriptor.nodeIds.map(String));
|
||||
const roads = state.compiled.model.roads.filter((road) => descriptor.approachRoadIds.includes(road.id));
|
||||
const roadIds = new Set(roads.map((road) => road.id));
|
||||
const segmentIds = new Set(roads.map((road) => road.segmentId));
|
||||
const keep = (value: GeoJson | null | undefined): GeoJson => ({
|
||||
type: 'FeatureCollection',
|
||||
features: (value?.features || []).filter((feature) => related(feature, nodeIds, roadIds, segmentIds)),
|
||||
});
|
||||
const layerNames = [
|
||||
'nativeRoadSurface',
|
||||
'nativeIntersectionSurface',
|
||||
'nativeSidewalkSurface',
|
||||
'laneCenterlines',
|
||||
'laneSeparators',
|
||||
'centerLines',
|
||||
'directionArrows',
|
||||
'turnArrows',
|
||||
'crosswalks',
|
||||
'vehicleStopLines',
|
||||
'connectors',
|
||||
'edgeLines',
|
||||
];
|
||||
const layers = { ...state.layers } as Record<string, GeoJson | null>;
|
||||
for (const name of layerNames) layers[name] = keep(state.layers[name]);
|
||||
layers.nativeRoadSurface = keep(state.layers.nativeRoadSurface);
|
||||
layers.nativeIntersectionSurface = keep(state.layers.nativeIntersectionSurface);
|
||||
const signals = state.trafficSignals.assemblies.features.filter((feature) =>
|
||||
related(feature, nodeIds, roadIds, segmentIds),
|
||||
);
|
||||
return {
|
||||
...state,
|
||||
compiled: {
|
||||
...state.compiled,
|
||||
model: {
|
||||
...state.compiled.model,
|
||||
roads,
|
||||
endpoints: state.compiled.model.endpoints.filter(
|
||||
(endpoint) => nodeIds.has(String(endpoint.nodeId)) || roadIds.has(endpoint.roadId),
|
||||
),
|
||||
connections: state.compiled.model.connections.filter((connection) => {
|
||||
const endpointIds = new Set(
|
||||
state.compiled.model.endpoints
|
||||
.filter((endpoint) => roadIds.has(endpoint.roadId) || nodeIds.has(String(endpoint.nodeId)))
|
||||
.map((endpoint) => endpoint.id),
|
||||
);
|
||||
return endpointIds.has(connection.fromEndpointId) || endpointIds.has(connection.toEndpointId);
|
||||
}),
|
||||
},
|
||||
movements: state.compiled.movements?.filter(
|
||||
(movement) => roadIds.has(movement.fromRoadId) || roadIds.has(movement.toRoadId),
|
||||
),
|
||||
diagnostics: state.compiled.diagnostics.filter((item) =>
|
||||
related(
|
||||
{ type: 'Feature', properties: item as unknown as Record<string, unknown>, geometry: item.geometry || null },
|
||||
nodeIds,
|
||||
roadIds,
|
||||
segmentIds,
|
||||
),
|
||||
),
|
||||
},
|
||||
layers,
|
||||
trafficSignals: { ...state.trafficSignals, assemblies: { ...state.trafficSignals.assemblies, features: signals } },
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user