feat(workbench): improve traffic signal editing
This commit is contained in:
1 parent
00d5f7650f
commit
cf5bc0602b
3 files changed
+144
-15
No files matched your search
@@ -14,6 +14,7 @@ const SCHEMA = 'native-traffic-signals/v1';
|
|||||||
function loadOrGenerate(file, osmText, stopLines, intersections) {
|
function loadOrGenerate(file, osmText, stopLines, intersections) {
|
||||||
if (fs.existsSync(file)) {
|
if (fs.existsSync(file)) {
|
||||||
const document = JSON.parse(fs.readFileSync(file, 'utf8'));
|
const document = JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||||
|
if (document?.provenance === 'empty') return generate(osmText, stopLines, intersections);
|
||||||
try {
|
try {
|
||||||
return validateDocument(document, osmText);
|
return validateDocument(document, osmText);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -640,6 +640,38 @@ function App() {
|
|||||||
if (typeof uid === 'string') setStatus(`已选中原生红绿灯 ${uid}`);
|
if (typeof uid === 'string') setStatus(`已选中原生红绿灯 ${uid}`);
|
||||||
else if (properties.effective_style) setStatus('已选中标线,可在右侧暂存样式');
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<header>
|
<header>
|
||||||
@@ -771,6 +803,8 @@ function App() {
|
|||||||
onSelectRoad={setSelected}
|
onSelectRoad={setSelected}
|
||||||
onFeature={handleFeature}
|
onFeature={handleFeature}
|
||||||
onBackground={() => setElement(null)}
|
onBackground={() => setElement(null)}
|
||||||
|
onSignalDrag={moveSignal}
|
||||||
|
onSignalDragEnd={finishSignalMove}
|
||||||
handles={editHandles}
|
handles={editHandles}
|
||||||
ranges={editRanges}
|
ranges={editRanges}
|
||||||
element={elementOutline}
|
element={elementOutline}
|
||||||
@@ -1308,6 +1342,10 @@ function SignalEditor({
|
|||||||
const [selected, setSelected] = useState('');
|
const [selected, setSelected] = useState('');
|
||||||
useEffect(() => setSelected(String(signal?.properties?.signal_uid || '')), [signal]);
|
useEffect(() => setSelected(String(signal?.properties?.signal_uid || '')), [signal]);
|
||||||
const feature = state.trafficSignals.assemblies.features.find((item) => item.properties?.signal_uid === selected);
|
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) => {
|
const saveField = (key: string, value: unknown) => {
|
||||||
if (!feature) return;
|
if (!feature) return;
|
||||||
void update(
|
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 (
|
return (
|
||||||
<section className="element-form">
|
<section className="element-form">
|
||||||
<h2>原生红绿灯</h2>
|
<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>
|
<label>
|
||||||
检查信号灯
|
检查信号灯
|
||||||
<select value={selected} onChange={(event) => setSelected(event.target.value)}>
|
<select value={selected} onChange={(event) => setSelected(event.target.value)}>
|
||||||
@@ -1345,6 +1382,24 @@ function SignalEditor({
|
|||||||
</label>
|
</label>
|
||||||
{feature ? (
|
{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>
|
<label>
|
||||||
横杆方向(度)
|
横杆方向(度)
|
||||||
<input
|
<input
|
||||||
@@ -1369,6 +1424,32 @@ function SignalEditor({
|
|||||||
onChange={(event) => saveField('face_heading_deg', Number(event.target.value))}
|
onChange={(event) => saveField('face_heading_deg', Number(event.target.value))}
|
||||||
/>
|
/>
|
||||||
</label>
|
</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>
|
<label>
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import Fill from 'ol/style/Fill';
|
|||||||
import RegularShape from 'ol/style/RegularShape';
|
import RegularShape from 'ol/style/RegularShape';
|
||||||
import Stroke from 'ol/style/Stroke';
|
import Stroke from 'ol/style/Stroke';
|
||||||
import Style from 'ol/style/Style';
|
import Style from 'ol/style/Style';
|
||||||
|
import { offsetCoordinate, type Coordinate } from '../edit/meters';
|
||||||
import type { Road, WorkbenchState } from '../types/state';
|
import type { Road, WorkbenchState } from '../types/state';
|
||||||
|
|
||||||
const geojson = new GeoJSON();
|
const geojson = new GeoJSON();
|
||||||
@@ -93,7 +94,21 @@ export function createLayers(
|
|||||||
}),
|
}),
|
||||||
signals: new VectorLayer({
|
signals: new VectorLayer({
|
||||||
source: source(),
|
source: source(),
|
||||||
style: new Style({
|
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({
|
image: new RegularShape({
|
||||||
points: 4,
|
points: 4,
|
||||||
radius: 6,
|
radius: 6,
|
||||||
@@ -155,7 +170,39 @@ export function updateLayers(layers: ReturnType<typeof createLayers>, state: Wor
|
|||||||
put('centerLines', state.layers.centerLines);
|
put('centerLines', state.layers.centerLines);
|
||||||
put('controls', state.layers.crosswalks);
|
put('controls', state.layers.crosswalks);
|
||||||
get('controls').addFeatures(features(state.layers.vehicleStopLines));
|
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('connectors', state.layers.connectors);
|
||||||
put('diagnostics', {
|
put('diagnostics', {
|
||||||
type: 'FeatureCollection',
|
type: 'FeatureCollection',
|
||||||
|
|||||||
Reference in new issue
Block a user