feat(workbench): improve traffic signal editing

This commit is contained in:
que01 committed 2026-09-02 09:04:56 +08:00
1 parent 00d5f7650f
commit cf5bc0602b
3 files changed
+152 -23

No files matched your search

+1
View File
@@ -14,6 +14,7 @@ const SCHEMA = 'native-traffic-signals/v1';
function loadOrGenerate(file, osmText, stopLines, intersections) {
if (fs.existsSync(file)) {
const document = JSON.parse(fs.readFileSync(file, 'utf8'));
if (document?.provenance === 'empty') return generate(osmText, stopLines, intersections);
try {
return validateDocument(document, osmText);
} catch (error) {
+94 -13
View File
@@ -640,6 +640,38 @@ function App() {
if (typeof uid === 'string') setStatus(`已选中原生红绿灯 ${uid}`);
else if (properties.effective_style) setStatus('已选中标线,可在右侧暂存样式');
};
const moveSignal = (uid: string, coordinate: Coordinate) => {
setState((current) => {
if (!current) return current;
return {
...current,
trafficSignals: {
...current.trafficSignals,
assemblies: {
...current.trafficSignals.assemblies,
features: current.trafficSignals.assemblies.features.map((feature) =>
feature.properties?.signal_uid === uid
? { ...feature, geometry: { type: 'Point', coordinates: coordinate } }
: feature,
),
},
},
};
});
};
const finishSignalMove = (uid: string, coordinate: Coordinate) => {
const current = state;
if (!current) return;
const features = current.trafficSignals.assemblies.features.map((feature) =>
feature.properties?.signal_uid === uid
? { ...feature, geometry: { type: 'Point', coordinates: coordinate } }
: feature,
);
void api
.signals({ ...current.trafficSignals, assemblies: { ...current.trafficSignals.assemblies, features } })
.then((result) => setState((value) => (value ? { ...value, ...result } : value)))
.catch((error: Error) => setStatus(`红绿灯位置保存失败:${error.message}`));
};
return (
<>
<header>
@@ -771,6 +803,8 @@ function App() {
onSelectRoad={setSelected}
onFeature={handleFeature}
onBackground={() => setElement(null)}
onSignalDrag={moveSignal}
onSignalDragEnd={finishSignalMove}
handles={editHandles}
ranges={editRanges}
element={elementOutline}
@@ -1308,6 +1342,10 @@ function SignalEditor({
const [selected, setSelected] = useState('');
useEffect(() => setSelected(String(signal?.properties?.signal_uid || '')), [signal]);
const feature = state.trafficSignals.assemblies.features.find((item) => item.properties?.signal_uid === selected);
const coordinates =
feature?.geometry?.type === 'Point' && Array.isArray(feature.geometry.coordinates)
? (feature.geometry.coordinates as [number, number])
: null;
const saveField = (key: string, value: unknown) => {
if (!feature) return;
void update(
@@ -1316,22 +1354,21 @@ function SignalEditor({
),
);
};
const saveCoordinate = (index: 0 | 1, value: number) => {
if (!feature || !coordinates || !Number.isFinite(value)) return;
const nextCoordinates: [number, number] = [...coordinates];
nextCoordinates[index] = value;
void update(
state.trafficSignals.assemblies.features.map((item) =>
item === feature
? { ...item, geometry: { ...item.geometry, type: 'Point', coordinates: nextCoordinates } }
: item,
),
);
};
return (
<section className="element-form">
<h2>原生红绿灯</h2>
<Button
onClick={() =>
void api
.generateSignals()
.then((next) => {
void update(next.trafficSignals.assemblies.features);
status('已补充 OSM 信号灯');
})
.catch((error: Error) => status(error.message))
}
>
从 OSM 生成缺失信号灯
</Button>
<label>
检查信号灯
<select value={selected} onChange={(event) => setSelected(event.target.value)}>
@@ -1345,6 +1382,24 @@ function SignalEditor({
</label>
{feature ? (
<>
<label>
灯杆经度
<input
type="number"
step="0.000001"
value={coordinates?.[0] ?? 0}
onChange={(event) => saveCoordinate(0, Number(event.target.value))}
/>
</label>
<label>
灯杆纬度
<input
type="number"
step="0.000001"
value={coordinates?.[1] ?? 0}
onChange={(event) => saveCoordinate(1, Number(event.target.value))}
/>
</label>
<label>
横杆方向(度)
<input
@@ -1369,6 +1424,32 @@ function SignalEditor({
onChange={(event) => saveField('face_heading_deg', Number(event.target.value))}
/>
</label>
<label>
相位组
<select
value={Number(feature.properties?.phase_group || 0)}
onChange={(event) => saveField('phase_group', Number(event.target.value))}
>
<option value={0}>组 0</option>
<option value={1}>组 1</option>
</select>
</label>
<label>
高程偏移(米)
<input
type="number"
step="0.1"
min="-20"
max="100"
value={Number(feature.properties?.z_offset_m || 0)}
onChange={(event) => saveField('z_offset_m', Number(event.target.value))}
/>
</label>
<p className="form-hint">
控制进口:{String(feature.properties?.approach_id || '未绑定')}
<br />
来源道路:{String(feature.properties?.source_way_id || '未绑定')}
</p>
<label>
<input
type="checkbox"
+57 -10
View File
@@ -9,6 +9,7 @@ import Fill from 'ol/style/Fill';
import RegularShape from 'ol/style/RegularShape';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import { offsetCoordinate, type Coordinate } from '../edit/meters';
import type { Road, WorkbenchState } from '../types/state';
const geojson = new GeoJSON();
@@ -93,15 +94,29 @@ export function createLayers(
}),
signals: new VectorLayer({
source: source(),
style: new Style({
image: new RegularShape({
points: 4,
radius: 6,
angle: Math.PI / 4,
fill: new Fill({ color: '#263630' }),
stroke: new Stroke({ color: '#fff', width: 2 }),
}),
}),
style: (feature) =>
feature.get('signal_component') === 'mast'
? new Style({ stroke: new Stroke({ color: '#263630', width: 3 }) })
: feature.get('signal_component') === 'face'
? new Style({ stroke: new Stroke({ color: '#d49318', width: 2, lineDash: [5, 4] }) })
: feature.get('signal_component') === 'face-tip'
? new Style({
image: new RegularShape({
points: 3,
radius: 5,
rotation: (Number(feature.get('signal_heading') || 0) * Math.PI) / 180,
fill: new Fill({ color: '#d49318' }),
}),
})
: new Style({
image: new RegularShape({
points: 4,
radius: 6,
angle: Math.PI / 4,
fill: new Fill({ color: '#263630' }),
stroke: new Stroke({ color: '#fff', width: 2 }),
}),
}),
}),
connectors: new VectorLayer({
source: source(),
@@ -155,7 +170,39 @@ export function updateLayers(layers: ReturnType<typeof createLayers>, state: Wor
put('centerLines', state.layers.centerLines);
put('controls', state.layers.crosswalks);
get('controls').addFeatures(features(state.layers.vehicleStopLines));
put('signals', state.trafficSignals.assemblies);
const signalFeatures = state.trafficSignals.assemblies.features.flatMap((feature) => {
const point =
feature.geometry?.type === 'Point' && Array.isArray(feature.geometry.coordinates)
? (feature.geometry.coordinates as Coordinate)
: null;
if (!point || point.length < 2) return [feature];
const properties = feature.properties || {};
const mastHeading = Number(properties.mast_heading_deg);
const faceHeading = Number(properties.face_heading_deg);
const reach = Number(properties.mast_reach_m);
if (![mastHeading, faceHeading, reach].every(Number.isFinite)) return [feature];
const mastEnd = offsetCoordinate(point, mastHeading, reach);
const faceEnd = offsetCoordinate(mastEnd, faceHeading, Math.min(1.2, reach * 0.2));
return [
feature,
{
type: 'Feature' as const,
geometry: { type: 'LineString', coordinates: [point, mastEnd] },
properties: { ...properties, signal_component: 'mast' },
},
{
type: 'Feature' as const,
geometry: { type: 'LineString', coordinates: [mastEnd, faceEnd] },
properties: { ...properties, signal_component: 'face' },
},
{
type: 'Feature' as const,
geometry: { type: 'Point', coordinates: faceEnd },
properties: { ...properties, signal_component: 'face-tip', signal_heading: faceHeading },
},
];
});
put('signals', { type: 'FeatureCollection', features: signalFeatures });
put('connectors', state.layers.connectors);
put('diagnostics', {
type: 'FeatureCollection',