fix(workbench): show junction traffic signals
This commit is contained in:
1 parent
f10a5da325
commit
1f439d98de
15 files changed
+529
-60
No files matched your search
@@ -4,15 +4,21 @@ import { canEditJunction, type JunctionSession } from './workspace';
|
||||
import { Button } from '../ui/button';
|
||||
import { ParameterPanel } from '../components/ParameterPanel';
|
||||
import type { EditHandle } from '../edit/types';
|
||||
import type { ElementSelection } from '../edit/element';
|
||||
import type { Road } from '../types/state';
|
||||
import type { WorkbenchState } from '../types/state';
|
||||
import type { Coordinate } from '../edit/meters';
|
||||
import { junctionLabelForHandle, junctionRoadName } from './labels';
|
||||
import { junctionFacilities } from './facilities';
|
||||
import { SignalEditor } from '../components/ElementEditors';
|
||||
|
||||
interface Props {
|
||||
descriptor: JunctionDescriptor;
|
||||
session: JunctionSession;
|
||||
dirty?: boolean;
|
||||
diagnostics?: Array<{ id: string; message: string; rule: string; severity?: string }>;
|
||||
facilitySummary?: { crosswalks: number; stopLines: number; signals: number };
|
||||
state?: WorkbenchState;
|
||||
onClose: () => void;
|
||||
onApply: () => void;
|
||||
onDiscard: () => void;
|
||||
@@ -28,11 +34,18 @@ interface Props {
|
||||
onReset?: (handle: EditHandle) => void;
|
||||
roads?: Road[];
|
||||
junctionCoordinate?: Coordinate;
|
||||
selectedFacility?: ElementSelection | null;
|
||||
onSelectFacility?: (facility: { id: string; kind: 'crosswalk' | 'stop-line' | 'signal' }) => void;
|
||||
signalUids?: readonly string[];
|
||||
selectedSignalUid?: string | null;
|
||||
onSignalStatus?: (message: string) => void;
|
||||
onUpdateSignal?: (features: import('../types/state').GeoFeature[]) => Promise<void>;
|
||||
}
|
||||
|
||||
export function JunctionWorkspace({
|
||||
descriptor,
|
||||
session,
|
||||
dirty = session.dirty,
|
||||
onClose,
|
||||
onApply,
|
||||
onDiscard,
|
||||
@@ -41,6 +54,7 @@ export function JunctionWorkspace({
|
||||
onEnableEdit,
|
||||
diagnostics = [],
|
||||
facilitySummary = { crosswalks: 0, stopLines: 0, signals: 0 },
|
||||
state,
|
||||
handles = [],
|
||||
onDraft,
|
||||
onDraftGroup,
|
||||
@@ -50,8 +64,18 @@ export function JunctionWorkspace({
|
||||
onReset,
|
||||
roads = [],
|
||||
junctionCoordinate = descriptor.coordinate,
|
||||
selectedFacility,
|
||||
onSelectFacility,
|
||||
signalUids = [],
|
||||
selectedSignalUid,
|
||||
onSignalStatus,
|
||||
onUpdateSignal,
|
||||
}: Props) {
|
||||
const editable = useMemo(() => canEditJunction(descriptor), [descriptor]);
|
||||
const facilities = useMemo(
|
||||
() => (state ? junctionFacilities(state, descriptor.nodeIds, roads) : []),
|
||||
[descriptor.nodeIds, roads, state],
|
||||
);
|
||||
return (
|
||||
<section className="junction-workspace" aria-label="路口编辑工作区">
|
||||
<header className="junction-workspace__header">
|
||||
@@ -83,7 +107,7 @@ export function JunctionWorkspace({
|
||||
<dt>状态</dt>
|
||||
<dd>
|
||||
{session.status}
|
||||
{session.dirty ? '(有未应用草稿)' : ''}
|
||||
{dirty ? '(有未应用草稿)' : ''}
|
||||
</dd>
|
||||
</dl>
|
||||
<section className="junction-workspace__validation" aria-label="路口验证">
|
||||
@@ -91,6 +115,44 @@ export function JunctionWorkspace({
|
||||
<p>
|
||||
斑马线 {facilitySummary.crosswalks} · 停止线 {facilitySummary.stopLines} · 信号灯 {facilitySummary.signals}
|
||||
</p>
|
||||
{facilities.length ? (
|
||||
<ul className="junction-workspace__facilities" aria-label="已有设施">
|
||||
{facilities.map((facility) => (
|
||||
<li key={facility.id}>
|
||||
<button
|
||||
type="button"
|
||||
className={
|
||||
(facility.kind === 'signal' && selectedSignalUid === facility.id.slice('signal:'.length)) ||
|
||||
(selectedFacility &&
|
||||
selectedFacility.kind === facility.kind &&
|
||||
String(selectedFacility.crossingNodeId) === facility.id.split(':').slice(1).join(':'))
|
||||
? 'is-selected'
|
||||
: undefined
|
||||
}
|
||||
aria-current={
|
||||
(facility.kind === 'signal' && selectedSignalUid === facility.id.slice('signal:'.length)) ||
|
||||
(selectedFacility &&
|
||||
selectedFacility.kind === facility.kind &&
|
||||
String(selectedFacility.crossingNodeId) === facility.id.split(':').slice(1).join(':'))
|
||||
? 'true'
|
||||
: undefined
|
||||
}
|
||||
onClick={() => onSelectFacility?.(facility)}
|
||||
>
|
||||
{facility.label}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
{selectedFacility && selectedFacility.kind !== 'lane-maneuver' ? (
|
||||
<p className="junction-workspace__facility-selection">
|
||||
当前选中:{selectedFacility.kind === 'crosswalk' ? '斑马线' : '停止线'}。可在下方参数中调整位置。
|
||||
</p>
|
||||
) : null}
|
||||
{selectedSignalUid ? (
|
||||
<p className="junction-workspace__facility-selection">当前选中:信号灯。可在下方参数中调整。</p>
|
||||
) : null}
|
||||
{diagnostics.length ? (
|
||||
<ul>
|
||||
{diagnostics.map((item) => (
|
||||
@@ -116,22 +178,51 @@ export function JunctionWorkspace({
|
||||
onAbandon={onAbandon ?? (() => undefined)}
|
||||
onReset={onReset ?? (() => undefined)}
|
||||
labelForHandle={(handle) => junctionLabelForHandle(handle, roads, junctionCoordinate)}
|
||||
highlightedHandleId={
|
||||
selectedFacility && selectedFacility.kind !== 'lane-maneuver'
|
||||
? handles.find(
|
||||
(handle) =>
|
||||
handle.anchor.type === 'junction-crossing' &&
|
||||
String(handle.anchor.crossingId) === String(selectedFacility.crossingNodeId) &&
|
||||
handle.kind ===
|
||||
(selectedFacility.kind === 'crosswalk'
|
||||
? 'junction-crosswalk-inset'
|
||||
: 'junction-stop-line-offset'),
|
||||
)?.handleId
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
{onUpdateSignal ? (
|
||||
<SignalEditor
|
||||
state={state!}
|
||||
signal={
|
||||
selectedSignalUid
|
||||
? state?.trafficSignals.assemblies.features.find(
|
||||
(item) => String(item.properties?.signal_uid) === selectedSignalUid,
|
||||
)
|
||||
: undefined
|
||||
}
|
||||
signalUids={signalUids}
|
||||
allowDelete={false}
|
||||
status={onSignalStatus ?? (() => undefined)}
|
||||
update={onUpdateSignal}
|
||||
/>
|
||||
) : null}
|
||||
<div className="junction-workspace__actions">
|
||||
<Button onClick={onApply} disabled={!session.dirty}>
|
||||
<Button onClick={onApply} disabled={!dirty}>
|
||||
应用到工作区
|
||||
</Button>
|
||||
<Button onClick={onSave} disabled={session.dirty}>
|
||||
<Button onClick={onSave} disabled={!dirty}>
|
||||
保存
|
||||
</Button>
|
||||
<Button onClick={onDiscard} disabled={!session.dirty}>
|
||||
<Button onClick={onDiscard} disabled={!dirty}>
|
||||
放弃草稿
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
) : editable ? (
|
||||
<p className="junction-workspace__browse-hint">
|
||||
开启编辑模式后,这里会显示进口宽度、cutback 和角部圆角控件。
|
||||
开启编辑模式后,这里会显示进口宽度、进口收口距离和角部圆角控件。
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { junctionFacilities } from './facilities';
|
||||
import type { WorkbenchState } from '../types/state';
|
||||
|
||||
const road = {
|
||||
id: 'road-a',
|
||||
segmentId: 'segment-a',
|
||||
centerline: [],
|
||||
sourceNodeIds: [],
|
||||
osmWayIds: [],
|
||||
tags: { name: '创新大道' },
|
||||
highway: 'primary',
|
||||
direction: 'forward',
|
||||
widthMeters: 8,
|
||||
laneCount: 2,
|
||||
sidewalkLeft: true,
|
||||
sidewalkRight: true,
|
||||
};
|
||||
const feature = (properties: Record<string, unknown>) => ({ type: 'Feature' as const, properties, geometry: null });
|
||||
|
||||
it('groups zebra stripes while keeping each facility type distinct', () => {
|
||||
const state = {
|
||||
layers: {
|
||||
crosswalks: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
feature({ crossing_node_id: 'node-1', road_id: 'road-a' }),
|
||||
feature({ crossing_node_id: 'node-1', road_id: 'road-a' }),
|
||||
],
|
||||
},
|
||||
vehicleStopLines: {
|
||||
type: 'FeatureCollection',
|
||||
features: [feature({ crossing_node_id: 'node-1', road_id: 'road-a' })],
|
||||
},
|
||||
},
|
||||
trafficSignals: {
|
||||
assemblies: {
|
||||
type: 'FeatureCollection',
|
||||
features: [feature({ control_id: 'node-1', signal_uid: 'signal-1', road_id: 'road-a' })],
|
||||
},
|
||||
},
|
||||
} as unknown as WorkbenchState;
|
||||
expect(junctionFacilities(state, ['node-1'], [road])).toEqual([
|
||||
{ id: 'crosswalk:node-1', kind: 'crosswalk', label: '斑马线 · 创新大道' },
|
||||
{ id: 'stop-line:node-1', kind: 'stop-line', label: '停止线 · 创新大道' },
|
||||
{ id: 'signal:signal-1', kind: 'signal', label: '信号灯 · 创新大道' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('filters every facility to the active junction nodes', () => {
|
||||
const state = {
|
||||
layers: {
|
||||
crosswalks: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
feature({ crossing_node_id: 'node-1', road_id: 'road-a' }),
|
||||
feature({ crossing_node_id: 'node-2', road_id: 'road-a' }),
|
||||
],
|
||||
},
|
||||
vehicleStopLines: {
|
||||
type: 'FeatureCollection',
|
||||
features: [feature({ crossing_node_id: 'node-2', road_id: 'road-a' })],
|
||||
},
|
||||
},
|
||||
trafficSignals: {
|
||||
assemblies: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
feature({ node_id: 'node-1', signal_uid: 7, road_id: 'road-a' }),
|
||||
feature({ node_id: 'node-2', signal_uid: '8', road_id: 'road-a' }),
|
||||
],
|
||||
},
|
||||
},
|
||||
} as unknown as WorkbenchState;
|
||||
expect(junctionFacilities(state, ['node-1'], [road])).toEqual([
|
||||
{ id: 'crosswalk:node-1', kind: 'crosswalk', label: '斑马线 · 创新大道' },
|
||||
{ id: 'signal:7', kind: 'signal', label: '信号灯 · 创新大道' },
|
||||
]);
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
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()];
|
||||
}
|
||||
@@ -74,6 +74,6 @@ describe('junction handle labels', () => {
|
||||
roads as unknown as Road[],
|
||||
[120, 30],
|
||||
),
|
||||
).toBe('道路2 · 东进口 · 进口 cutback');
|
||||
).toBe('道路2 · 东进口 · 进口收口距离');
|
||||
});
|
||||
});
|
||||
@@ -4,13 +4,33 @@ import type { PreviewLayerName } from '../edit/types';
|
||||
|
||||
const empty: GeoJson = { type: 'FeatureCollection', features: [] };
|
||||
|
||||
function approachRoadIdsForDescriptor(state: WorkbenchState, descriptor: JunctionDescriptor): Set<string> {
|
||||
const ids = new Set(descriptor.approachRoadIds);
|
||||
if (!ids.size) {
|
||||
const nodes = new Set(descriptor.nodeIds.map(String));
|
||||
for (const endpoint of state.compiled.model.endpoints || []) {
|
||||
if (nodes.has(String(endpoint.nodeId))) ids.add(endpoint.roadId);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
function approachRoadIdsFromRoads(roads: Road[], descriptor: JunctionDescriptor): Set<string> {
|
||||
const ids = new Set(descriptor.approachRoadIds);
|
||||
if (!ids.size) {
|
||||
const nodes = new Set(descriptor.nodeIds.map(String));
|
||||
for (const road of roads) if (road.sourceNodeIds.some((node) => nodes.has(String(node)))) ids.add(road.id);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
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'];
|
||||
const nodeKeys = ['node_id', 'osm_node_id', 'control_id', 'crossing_node_id', 'junction_node_id'];
|
||||
if (
|
||||
nodeKeys.some((key) => {
|
||||
const value = text(props[key]);
|
||||
@@ -45,7 +65,8 @@ export function localJunctionPreview(
|
||||
): 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 approachRoadIds = approachRoadIdsFromRoads(roads, descriptor);
|
||||
const selectedRoads = roads.filter((road) => approachRoadIds.has(road.id) || approachRoadIds.has(road.segmentId));
|
||||
const roadIds = new Set(selectedRoads.map((road) => road.id));
|
||||
const segmentIds = new Set(selectedRoads.map((road) => road.segmentId));
|
||||
const scoped = { ...preview };
|
||||
@@ -63,7 +84,10 @@ export function localJunctionPreview(
|
||||
/** 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 approachRoadIds = approachRoadIdsForDescriptor(state, descriptor);
|
||||
const roads = state.compiled.model.roads.filter(
|
||||
(road) => approachRoadIds.has(road.id) || approachRoadIds.has(road.segmentId),
|
||||
);
|
||||
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 => ({
|
||||
|
||||
Reference in new issue
Block a user