feat(workbench): add traffic signal rotation handle
This commit is contained in:
1 parent
9b7c3d59bd
commit
caa3cb843f
4 files changed
+234
-23
No files matched your search
+10
-2
@@ -6,6 +6,7 @@ const { parseOsm } = require('./osm');
|
|||||||
|
|
||||||
const EARTH_RADIUS = 6371008.8;
|
const EARTH_RADIUS = 6371008.8;
|
||||||
const CURB_OFFSET_METERS = 5.2;
|
const CURB_OFFSET_METERS = 5.2;
|
||||||
|
const MAX_SIGNAL_POLE_DISTANCE_FROM_STOP_METERS = 40;
|
||||||
const MAST_REACH_METERS = 4.5;
|
const MAST_REACH_METERS = 4.5;
|
||||||
const SIGNAL_LAYOUT = Object.freeze({
|
const SIGNAL_LAYOUT = Object.freeze({
|
||||||
poleHeightMeters: 6.7,
|
poleHeightMeters: 6.7,
|
||||||
@@ -195,6 +196,13 @@ function validateTrafficSignalFeatures(collection) {
|
|||||||
input.face_heading_deg == null || input.face_heading_deg === ''
|
input.face_heading_deg == null || input.face_heading_deg === ''
|
||||||
? normalizeDegrees((legacyHeading == null ? 0 : legacyHeading) + 180)
|
? normalizeDegrees((legacyHeading == null ? 0 : legacyHeading) + 180)
|
||||||
: normalizeDegrees(number('face_heading_deg'));
|
: normalizeDegrees(number('face_heading_deg'));
|
||||||
|
const stopLon = number('stop_lon', { min: -180, max: 180 });
|
||||||
|
const stopLat = number('stop_lat', { min: -90, max: 90 });
|
||||||
|
const poleDistance = metersBetween(feature.geometry.coordinates, [stopLon, stopLat]);
|
||||||
|
if (poleDistance > MAX_SIGNAL_POLE_DISTANCE_FROM_STOP_METERS)
|
||||||
|
throw new Error(
|
||||||
|
`${label}: signal pole is ${Math.round(poleDistance)}m from its stop line (maximum ${MAX_SIGNAL_POLE_DISTANCE_FROM_STOP_METERS}m)`,
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
type: 'Feature',
|
type: 'Feature',
|
||||||
geometry: { type: 'Point', coordinates: feature.geometry.coordinates.slice(0, 2).map(Number) },
|
geometry: { type: 'Point', coordinates: feature.geometry.coordinates.slice(0, 2).map(Number) },
|
||||||
@@ -212,8 +220,8 @@ function validateTrafficSignalFeatures(collection) {
|
|||||||
face_heading_deg: faceHeading,
|
face_heading_deg: faceHeading,
|
||||||
phase_group: phaseGroup,
|
phase_group: phaseGroup,
|
||||||
mast_reach_m: number('mast_reach_m', { min: 0.1, max: 30 }),
|
mast_reach_m: number('mast_reach_m', { min: 0.1, max: 30 }),
|
||||||
stop_lon: number('stop_lon', { min: -180, max: 180 }),
|
stop_lon: stopLon,
|
||||||
stop_lat: number('stop_lat', { min: -90, max: 90 }),
|
stop_lat: stopLat,
|
||||||
enabled,
|
enabled,
|
||||||
z_offset_m: number('z_offset_m', { min: -20, max: 100 }),
|
z_offset_m: number('z_offset_m', { min: -20, max: 100 }),
|
||||||
},
|
},
|
||||||
|
|||||||
+103
-16
@@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||||
import { Download, RefreshCw, Save, Upload } from 'lucide-react';
|
import { Download, RefreshCw, Upload } from 'lucide-react';
|
||||||
import { api, isVersionConflict } from './lib/api';
|
import { api, isVersionConflict } from './lib/api';
|
||||||
import { MapCanvas } from './components/MapCanvas';
|
import { MapCanvas } from './components/MapCanvas';
|
||||||
import { ParameterPanel, type ParameterPanelHandle } from './components/ParameterPanel';
|
import { ParameterPanel, type ParameterPanelHandle } from './components/ParameterPanel';
|
||||||
@@ -19,6 +19,7 @@ import type { ElementOutline } from './edit/handle-layer';
|
|||||||
import {
|
import {
|
||||||
azimuthBetween,
|
azimuthBetween,
|
||||||
coordinateAtStation,
|
coordinateAtStation,
|
||||||
|
haversineMeters,
|
||||||
metersAlongPolyline,
|
metersAlongPolyline,
|
||||||
polylineLengthMeters,
|
polylineLengthMeters,
|
||||||
toLonLat,
|
toLonLat,
|
||||||
@@ -116,6 +117,7 @@ function App() {
|
|||||||
const signalRedoRef = useRef<GeoFeature[][]>([]);
|
const signalRedoRef = useRef<GeoFeature[][]>([]);
|
||||||
const signalSavedRef = useRef<GeoFeature[] | null>(null);
|
const signalSavedRef = useRef<GeoFeature[] | null>(null);
|
||||||
const signalMoveActiveRef = useRef(false);
|
const signalMoveActiveRef = useRef(false);
|
||||||
|
const signalRotateActiveRef = useRef(false);
|
||||||
const [layers, setLayers] = useState(defaultLayers);
|
const [layers, setLayers] = useState(defaultLayers);
|
||||||
const [scene, setScene] = useState(false);
|
const [scene, setScene] = useState(false);
|
||||||
const [status, setStatus] = useState('正在加载工作台...');
|
const [status, setStatus] = useState('正在加载工作台...');
|
||||||
@@ -739,6 +741,20 @@ function App() {
|
|||||||
signalRedoRef.current = [];
|
signalRedoRef.current = [];
|
||||||
signalMoveActiveRef.current = true;
|
signalMoveActiveRef.current = true;
|
||||||
}
|
}
|
||||||
|
const currentFeature = current.trafficSignals.assemblies.features.find(
|
||||||
|
(feature) => feature.properties?.signal_uid === uid,
|
||||||
|
);
|
||||||
|
const stop: Coordinate | null = currentFeature
|
||||||
|
? [Number(currentFeature.properties?.stop_lon), Number(currentFeature.properties?.stop_lat)]
|
||||||
|
: null;
|
||||||
|
let nextCoordinate = coordinate;
|
||||||
|
if (stop && stop.every(Number.isFinite)) {
|
||||||
|
const distance = haversineMeters(stop, coordinate);
|
||||||
|
if (distance > 40) {
|
||||||
|
const ratio = 40 / distance;
|
||||||
|
nextCoordinate = [stop[0] + (coordinate[0] - stop[0]) * ratio, stop[1] + (coordinate[1] - stop[1]) * ratio];
|
||||||
|
}
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
...current,
|
...current,
|
||||||
trafficSignals: {
|
trafficSignals: {
|
||||||
@@ -747,7 +763,7 @@ function App() {
|
|||||||
...current.trafficSignals.assemblies,
|
...current.trafficSignals.assemblies,
|
||||||
features: current.trafficSignals.assemblies.features.map((feature) =>
|
features: current.trafficSignals.assemblies.features.map((feature) =>
|
||||||
feature.properties?.signal_uid === uid
|
feature.properties?.signal_uid === uid
|
||||||
? { ...feature, geometry: { type: 'Point', coordinates: coordinate } }
|
? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } }
|
||||||
: feature,
|
: feature,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
@@ -759,15 +775,80 @@ function App() {
|
|||||||
const finishSignalMove = (uid: string, coordinate: Coordinate) => {
|
const finishSignalMove = (uid: string, coordinate: Coordinate) => {
|
||||||
const current = state;
|
const current = state;
|
||||||
if (!current) return;
|
if (!current) return;
|
||||||
|
const source = current.trafficSignals.assemblies.features.find((feature) => feature.properties?.signal_uid === uid);
|
||||||
|
const stop: Coordinate | null = source
|
||||||
|
? [Number(source.properties?.stop_lon), Number(source.properties?.stop_lat)]
|
||||||
|
: null;
|
||||||
|
let nextCoordinate = coordinate;
|
||||||
|
if (stop && stop.every(Number.isFinite)) {
|
||||||
|
const distance = haversineMeters(stop, coordinate);
|
||||||
|
if (distance > 40) {
|
||||||
|
const ratio = 40 / distance;
|
||||||
|
nextCoordinate = [stop[0] + (coordinate[0] - stop[0]) * ratio, stop[1] + (coordinate[1] - stop[1]) * ratio];
|
||||||
|
}
|
||||||
|
}
|
||||||
const features = current.trafficSignals.assemblies.features.map((feature) =>
|
const features = current.trafficSignals.assemblies.features.map((feature) =>
|
||||||
feature.properties?.signal_uid === uid
|
feature.properties?.signal_uid === uid
|
||||||
? { ...feature, geometry: { type: 'Point', coordinates: coordinate } }
|
? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } }
|
||||||
: feature,
|
: feature,
|
||||||
);
|
);
|
||||||
signalDraftRef.current = features;
|
signalDraftRef.current = features;
|
||||||
signalMoveActiveRef.current = false;
|
signalMoveActiveRef.current = false;
|
||||||
setSignalDirty(true);
|
setSignalDirty(true);
|
||||||
};
|
};
|
||||||
|
const rotateSignal = (uid: string, mastHeading: number) => {
|
||||||
|
const heading = ((mastHeading % 360) + 360) % 360;
|
||||||
|
setState((current) => {
|
||||||
|
if (!current) return current;
|
||||||
|
if (!signalRotateActiveRef.current) {
|
||||||
|
signalUndoRef.current.push(current.trafficSignals.assemblies.features);
|
||||||
|
signalRedoRef.current = [];
|
||||||
|
signalRotateActiveRef.current = true;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...current,
|
||||||
|
trafficSignals: {
|
||||||
|
...current.trafficSignals,
|
||||||
|
assemblies: {
|
||||||
|
...current.trafficSignals.assemblies,
|
||||||
|
features: current.trafficSignals.assemblies.features.map((feature) =>
|
||||||
|
feature.properties?.signal_uid === uid
|
||||||
|
? {
|
||||||
|
...feature,
|
||||||
|
properties: {
|
||||||
|
...feature.properties,
|
||||||
|
mast_heading_deg: heading,
|
||||||
|
face_heading_deg: (((heading - 90) % 360) + 360) % 360,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: feature,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
setSignalDirty(true);
|
||||||
|
};
|
||||||
|
const finishSignalRotate = (uid: string, mastHeading: number) => {
|
||||||
|
const current = state;
|
||||||
|
if (!current) return;
|
||||||
|
const heading = ((mastHeading % 360) + 360) % 360;
|
||||||
|
const features = current.trafficSignals.assemblies.features.map((feature) =>
|
||||||
|
feature.properties?.signal_uid === uid
|
||||||
|
? {
|
||||||
|
...feature,
|
||||||
|
properties: {
|
||||||
|
...feature.properties,
|
||||||
|
mast_heading_deg: heading,
|
||||||
|
face_heading_deg: (((heading - 90) % 360) + 360) % 360,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: feature,
|
||||||
|
);
|
||||||
|
signalDraftRef.current = features;
|
||||||
|
signalRotateActiveRef.current = false;
|
||||||
|
setSignalDirty(true);
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<header>
|
<header>
|
||||||
@@ -809,17 +890,11 @@ function App() {
|
|||||||
<a className="button" href="/api/export.zip" download>
|
<a className="button" href="/api/export.zip" download>
|
||||||
<Download size={15} /> 导出道路包
|
<Download size={15} /> 导出道路包
|
||||||
</a>
|
</a>
|
||||||
<Button onClick={() => void save()} disabled={!staged.length}>
|
|
||||||
<Save size={15} /> 保存修改
|
|
||||||
</Button>
|
|
||||||
<Button onClick={() => void compile()}>
|
<Button onClick={() => void compile()}>
|
||||||
<RefreshCw size={15} /> 保存并重新生成
|
<RefreshCw size={15} /> 保存并重新生成
|
||||||
</Button>
|
</Button>
|
||||||
{editMode ? (
|
{editMode ? (
|
||||||
<>
|
<>
|
||||||
<Button onClick={() => void saveDirectEdits()} disabled={!session?.dirty}>
|
|
||||||
<Save size={15} /> 保存直接编辑
|
|
||||||
</Button>
|
|
||||||
<Button onClick={undoEdit} disabled={!session?.canUndo && !signalUndoRef.current.length}>
|
<Button onClick={undoEdit} disabled={!session?.canUndo && !signalUndoRef.current.length}>
|
||||||
撤销
|
撤销
|
||||||
</Button>
|
</Button>
|
||||||
@@ -898,9 +973,15 @@ function App() {
|
|||||||
scene={scene}
|
scene={scene}
|
||||||
onSelectRoad={setSelected}
|
onSelectRoad={setSelected}
|
||||||
onFeature={handleFeature}
|
onFeature={handleFeature}
|
||||||
onBackground={() => setElement(null)}
|
signalSelectionUid={typeof marking?.signal_uid === 'string' ? marking.signal_uid : null}
|
||||||
|
onBackground={() => {
|
||||||
|
setElement(null);
|
||||||
|
setMarking(null);
|
||||||
|
}}
|
||||||
onSignalDrag={moveSignal}
|
onSignalDrag={moveSignal}
|
||||||
onSignalDragEnd={finishSignalMove}
|
onSignalDragEnd={finishSignalMove}
|
||||||
|
onSignalRotate={rotateSignal}
|
||||||
|
onSignalRotateEnd={finishSignalRotate}
|
||||||
handles={editHandles}
|
handles={editHandles}
|
||||||
ranges={editRanges}
|
ranges={editRanges}
|
||||||
element={elementOutline}
|
element={elementOutline}
|
||||||
@@ -1435,9 +1516,19 @@ function SignalEditor({
|
|||||||
: null;
|
: null;
|
||||||
const saveField = (key: string, value: unknown) => {
|
const saveField = (key: string, value: unknown) => {
|
||||||
if (!feature) return;
|
if (!feature) return;
|
||||||
|
const mastHeading = key === 'mast_heading_deg' ? ((Number(value) % 360) + 360) % 360 : null;
|
||||||
void update(
|
void update(
|
||||||
state.trafficSignals.assemblies.features.map((item) =>
|
state.trafficSignals.assemblies.features.map((item) =>
|
||||||
item === feature ? { ...item, properties: { ...item.properties, [key]: value } } : item,
|
item === feature
|
||||||
|
? {
|
||||||
|
...item,
|
||||||
|
properties: {
|
||||||
|
...item.properties,
|
||||||
|
[key]: mastHeading ?? value,
|
||||||
|
...(mastHeading === null ? {} : { face_heading_deg: (((mastHeading - 90) % 360) + 360) % 360 }),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: item,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -1505,11 +1596,7 @@ function SignalEditor({
|
|||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
灯面朝向(度)
|
灯面朝向(度)
|
||||||
<input
|
<input type="number" value={Number(feature.properties?.face_heading_deg || 0)} readOnly />
|
||||||
type="number"
|
|
||||||
value={Number(feature.properties?.face_heading_deg || 0)}
|
|
||||||
onChange={(event) => saveField('face_heading_deg', Number(event.target.value))}
|
|
||||||
/>
|
|
||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
相位组
|
相位组
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import Feature from 'ol/Feature';
|
import Feature from 'ol/Feature';
|
||||||
|
import LineString from 'ol/geom/LineString';
|
||||||
import Map from 'ol/Map';
|
import Map from 'ol/Map';
|
||||||
|
import Point from 'ol/geom/Point';
|
||||||
import Polygon from 'ol/geom/Polygon';
|
import Polygon from 'ol/geom/Polygon';
|
||||||
import View from 'ol/View';
|
import View from 'ol/View';
|
||||||
import Select from 'ol/interaction/Select';
|
import Select from 'ol/interaction/Select';
|
||||||
import PointerInteraction from 'ol/interaction/Pointer';
|
import PointerInteraction from 'ol/interaction/Pointer';
|
||||||
import VectorLayer from 'ol/layer/Vector';
|
import VectorLayer from 'ol/layer/Vector';
|
||||||
import VectorSource from 'ol/source/Vector';
|
import VectorSource from 'ol/source/Vector';
|
||||||
|
import CircleStyle from 'ol/style/Circle';
|
||||||
import Fill from 'ol/style/Fill';
|
import Fill from 'ol/style/Fill';
|
||||||
import Stroke from 'ol/style/Stroke';
|
import Stroke from 'ol/style/Stroke';
|
||||||
import Style from 'ol/style/Style';
|
import Style from 'ol/style/Style';
|
||||||
@@ -18,6 +21,7 @@ import { EditGhostLayer } from '../edit/ghost-layer';
|
|||||||
import { EditHandleLayer, type ElementOutline } from '../edit/handle-layer';
|
import { EditHandleLayer, type ElementOutline } from '../edit/handle-layer';
|
||||||
import { EditPreviewLayer } from '../edit/preview-layer';
|
import { EditPreviewLayer } from '../edit/preview-layer';
|
||||||
import { fromLonLat, offsetCoordinate, signedMetersAlongAxis, toLonLat, type Coordinate } from '../edit/meters';
|
import { fromLonLat, offsetCoordinate, signedMetersAlongAxis, toLonLat, type Coordinate } from '../edit/meters';
|
||||||
|
import { azimuthBetween } from '../edit/meters';
|
||||||
import type { IntervalRangeHandle } from '../edit/selection';
|
import type { IntervalRangeHandle } from '../edit/selection';
|
||||||
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
|
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
|
||||||
|
|
||||||
@@ -49,6 +53,10 @@ interface Props {
|
|||||||
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
|
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
|
||||||
onSignalDrag?: (signalUid: string, coordinate: Coordinate) => void;
|
onSignalDrag?: (signalUid: string, coordinate: Coordinate) => void;
|
||||||
onSignalDragEnd?: (signalUid: string, coordinate: Coordinate) => void;
|
onSignalDragEnd?: (signalUid: string, coordinate: Coordinate) => void;
|
||||||
|
/** The selected signal gets a temporary pivot marker and rotate handle. */
|
||||||
|
signalSelectionUid?: string | null;
|
||||||
|
onSignalRotate?: (signalUid: string, mastHeading: number) => void;
|
||||||
|
onSignalRotateEnd?: (signalUid: string, mastHeading: number) => void;
|
||||||
/** Authoritative preview geometry; null returns the map to the baseline. */
|
/** Authoritative preview geometry; null returns the map to the baseline. */
|
||||||
preview?: Partial<Record<PreviewLayerName, GeoJson | null>> | null;
|
preview?: Partial<Record<PreviewLayerName, GeoJson | null>> | null;
|
||||||
/** Live value during a drag — the caller debounces the preview request. */
|
/** Live value during a drag — the caller debounces the preview request. */
|
||||||
@@ -83,6 +91,9 @@ export function MapCanvas({
|
|||||||
onLaneSelectionDragEnd,
|
onLaneSelectionDragEnd,
|
||||||
onSignalDrag,
|
onSignalDrag,
|
||||||
onSignalDragEnd,
|
onSignalDragEnd,
|
||||||
|
signalSelectionUid,
|
||||||
|
onSignalRotate,
|
||||||
|
onSignalRotateEnd,
|
||||||
preview,
|
preview,
|
||||||
onHandleDrag,
|
onHandleDrag,
|
||||||
onHandleDragEnd,
|
onHandleDragEnd,
|
||||||
@@ -102,6 +113,8 @@ export function MapCanvas({
|
|||||||
const fittedAreaIdRef = useRef<string | null>(null);
|
const fittedAreaIdRef = useRef<string | null>(null);
|
||||||
const laneSelectionSourceRef = useRef<VectorSource | null>(null);
|
const laneSelectionSourceRef = useRef<VectorSource | null>(null);
|
||||||
const laneSelectionLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
const laneSelectionLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
||||||
|
const signalTransformSourceRef = useRef<VectorSource | null>(null);
|
||||||
|
const signalTransformLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
||||||
const laneSelectionFeaturesRef = useRef<Feature[]>([]);
|
const laneSelectionFeaturesRef = useRef<Feature[]>([]);
|
||||||
const laneSelectionAnchorRef = useRef<Coordinate | null>(null);
|
const laneSelectionAnchorRef = useRef<Coordinate | null>(null);
|
||||||
const hiddenMarkingIdsRef = useRef<ReadonlySet<string>>(new Set());
|
const hiddenMarkingIdsRef = useRef<ReadonlySet<string>>(new Set());
|
||||||
@@ -122,6 +135,8 @@ export function MapCanvas({
|
|||||||
onLaneSelectionDrag,
|
onLaneSelectionDrag,
|
||||||
onSignalDrag,
|
onSignalDrag,
|
||||||
onSignalDragEnd,
|
onSignalDragEnd,
|
||||||
|
onSignalRotate,
|
||||||
|
onSignalRotateEnd,
|
||||||
laneSelectionAxisAzimuth,
|
laneSelectionAxisAzimuth,
|
||||||
ghostPending,
|
ghostPending,
|
||||||
});
|
});
|
||||||
@@ -135,6 +150,8 @@ export function MapCanvas({
|
|||||||
onLaneSelectionDrag,
|
onLaneSelectionDrag,
|
||||||
onSignalDrag,
|
onSignalDrag,
|
||||||
onSignalDragEnd,
|
onSignalDragEnd,
|
||||||
|
onSignalRotate,
|
||||||
|
onSignalRotateEnd,
|
||||||
laneSelectionAxisAzimuth,
|
laneSelectionAxisAzimuth,
|
||||||
ghostPending,
|
ghostPending,
|
||||||
};
|
};
|
||||||
@@ -177,6 +194,26 @@ export function MapCanvas({
|
|||||||
});
|
});
|
||||||
laneSelectionSourceRef.current = laneSelectionSource;
|
laneSelectionSourceRef.current = laneSelectionSource;
|
||||||
laneSelectionLayerRef.current = laneSelectionLayer;
|
laneSelectionLayerRef.current = laneSelectionLayer;
|
||||||
|
const signalTransformSource = new VectorSource();
|
||||||
|
const signalTransformLayer = new VectorLayer({
|
||||||
|
source: signalTransformSource,
|
||||||
|
zIndex: 95,
|
||||||
|
style: (feature) => {
|
||||||
|
const component = feature.get('signal_transform');
|
||||||
|
if (component === 'crosshair') return new Style({ stroke: new Stroke({ color: '#0e7490', width: 2 }) });
|
||||||
|
if (component === 'rotate-leader')
|
||||||
|
return new Style({ stroke: new Stroke({ color: '#2563eb', width: 1.5, lineDash: [4, 3] }) });
|
||||||
|
return new Style({
|
||||||
|
image: new CircleStyle({
|
||||||
|
radius: 8,
|
||||||
|
fill: new Fill({ color: '#2563eb' }),
|
||||||
|
stroke: new Stroke({ color: '#ffffff', width: 2 }),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
signalTransformSourceRef.current = signalTransformSource;
|
||||||
|
signalTransformLayerRef.current = signalTransformLayer;
|
||||||
// Direct-edit sources are always installed; visibility and hit testing follow
|
// Direct-edit sources are always installed; visibility and hit testing follow
|
||||||
// the in-page browser/edit switch.
|
// the in-page browser/edit switch.
|
||||||
const handleLayer = new EditHandleLayer();
|
const handleLayer = new EditHandleLayer();
|
||||||
@@ -194,6 +231,7 @@ export function MapCanvas({
|
|||||||
...Object.values(layers),
|
...Object.values(layers),
|
||||||
...(previewLayer ? previewLayer.all() : []),
|
...(previewLayer ? previewLayer.all() : []),
|
||||||
laneSelectionLayer,
|
laneSelectionLayer,
|
||||||
|
signalTransformLayer,
|
||||||
...(handleLayer ? [handleLayer.layer] : []),
|
...(handleLayer ? [handleLayer.layer] : []),
|
||||||
...(ghost ? [ghost.layer] : []),
|
...(ghost ? [ghost.layer] : []),
|
||||||
],
|
],
|
||||||
@@ -204,31 +242,62 @@ export function MapCanvas({
|
|||||||
condition: click,
|
condition: click,
|
||||||
// Handles are not selectable features: clicking one must not re-target the
|
// Handles are not selectable features: clicking one must not re-target the
|
||||||
// road inspector, and dragging is owned by the edit interaction.
|
// road inspector, and dragging is owned by the edit interaction.
|
||||||
layers: (layer) => layer !== layers.selectedRoad && layer !== handleLayer?.layer && layer !== laneSelectionLayer,
|
layers: (layer) =>
|
||||||
|
layer !== layers.selectedRoad &&
|
||||||
|
layer !== handleLayer?.layer &&
|
||||||
|
layer !== laneSelectionLayer &&
|
||||||
|
layer !== signalTransformLayer,
|
||||||
hitTolerance: 12,
|
hitTolerance: 12,
|
||||||
style: null,
|
style: null,
|
||||||
});
|
});
|
||||||
map.addInteraction(select);
|
map.addInteraction(select);
|
||||||
let signalUid: string | null = null;
|
let signalUid: string | null = null;
|
||||||
|
let rotatePivot: Coordinate | null = null;
|
||||||
map.addInteraction(
|
map.addInteraction(
|
||||||
new PointerInteraction({
|
new PointerInteraction({
|
||||||
handleDownEvent: (event) => {
|
handleDownEvent: (event) => {
|
||||||
if (!editModeRef.current) return false;
|
if (!editModeRef.current) return false;
|
||||||
|
const rotateHit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
||||||
|
layerFilter: (layer) => layer === signalTransformLayer,
|
||||||
|
hitTolerance: 12,
|
||||||
|
});
|
||||||
|
if (rotateHit?.get('signal_transform') === 'rotate-handle') {
|
||||||
|
const uid = rotateHit.get('signal_uid');
|
||||||
|
const pivot = rotateHit.get('signal_pivot');
|
||||||
|
if (typeof uid === 'string' && Array.isArray(pivot) && pivot.length === 2) {
|
||||||
|
signalUid = uid;
|
||||||
|
rotatePivot = pivot as Coordinate;
|
||||||
|
event.map.getViewport().style.cursor = 'grabbing';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
const hit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
const hit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
||||||
layerFilter: (layer) => layer === layers.signals,
|
layerFilter: (layer) => layer === layers.signals,
|
||||||
hitTolerance: 10,
|
hitTolerance: 10,
|
||||||
});
|
});
|
||||||
const uid = hit?.get('signal_uid');
|
const uid = hit?.get('signal_uid');
|
||||||
if (typeof uid !== 'string' || hit?.get('signal_component')) return false;
|
if (!hit || typeof uid !== 'string' || hit.get('signal_component')) return false;
|
||||||
signalUid = uid;
|
signalUid = uid;
|
||||||
|
selectRef.current.onFeature(hit.getProperties());
|
||||||
|
event.map.getViewport().style.cursor = 'grabbing';
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
handleDragEvent: (event) => {
|
handleDragEvent: (event) => {
|
||||||
if (signalUid) dragRef.current.onSignalDrag?.(signalUid, toLonLat(event.coordinate as Coordinate));
|
if (signalUid) {
|
||||||
|
const target = toLonLat(event.coordinate as Coordinate);
|
||||||
|
if (rotatePivot) dragRef.current.onSignalRotate?.(signalUid, azimuthBetween(rotatePivot, target));
|
||||||
|
else dragRef.current.onSignalDrag?.(signalUid, target);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
handleUpEvent: (event) => {
|
handleUpEvent: (event) => {
|
||||||
if (signalUid) dragRef.current.onSignalDragEnd?.(signalUid, toLonLat(event.coordinate as Coordinate));
|
if (signalUid) {
|
||||||
|
const target = toLonLat(event.coordinate as Coordinate);
|
||||||
|
if (rotatePivot) dragRef.current.onSignalRotateEnd?.(signalUid, azimuthBetween(rotatePivot, target));
|
||||||
|
else dragRef.current.onSignalDragEnd?.(signalUid, target);
|
||||||
|
}
|
||||||
signalUid = null;
|
signalUid = null;
|
||||||
|
rotatePivot = null;
|
||||||
|
event.map.getViewport().style.cursor = '';
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@@ -372,6 +441,53 @@ export function MapCanvas({
|
|||||||
);
|
);
|
||||||
laneSelectionFeaturesRef.current = parts;
|
laneSelectionFeaturesRef.current = parts;
|
||||||
}, [laneSelectionBounds, laneSelectionFeatures]);
|
}, [laneSelectionBounds, laneSelectionFeatures]);
|
||||||
|
useEffect(() => {
|
||||||
|
const source = signalTransformSourceRef.current;
|
||||||
|
const layer = signalTransformLayerRef.current;
|
||||||
|
if (!source || !layer) return;
|
||||||
|
source.clear();
|
||||||
|
layer.setVisible(Boolean(editMode && signalSelectionUid));
|
||||||
|
if (!editMode || !signalSelectionUid) return;
|
||||||
|
const signal = state.trafficSignals.assemblies.features.find(
|
||||||
|
(feature) => feature.properties?.signal_uid === signalSelectionUid,
|
||||||
|
);
|
||||||
|
const pivot =
|
||||||
|
signal?.geometry?.type === 'Point' && Array.isArray(signal.geometry.coordinates)
|
||||||
|
? (signal.geometry.coordinates as Coordinate)
|
||||||
|
: null;
|
||||||
|
const mastHeading = Number(signal?.properties?.mast_heading_deg);
|
||||||
|
const reach = Number(signal?.properties?.mast_reach_m);
|
||||||
|
if (!pivot || !Number.isFinite(mastHeading) || !Number.isFinite(reach)) return;
|
||||||
|
const mastEnd = offsetCoordinate(pivot, mastHeading, reach);
|
||||||
|
const rotateHandle = offsetCoordinate(pivot, mastHeading, reach + 1.2);
|
||||||
|
const crosshairRadius = 0.45;
|
||||||
|
source.addFeatures([
|
||||||
|
new Feature({
|
||||||
|
geometry: new LineString([
|
||||||
|
fromLonLat(offsetCoordinate(pivot, 270, crosshairRadius)),
|
||||||
|
fromLonLat(offsetCoordinate(pivot, 90, crosshairRadius)),
|
||||||
|
]),
|
||||||
|
signal_transform: 'crosshair',
|
||||||
|
}),
|
||||||
|
new Feature({
|
||||||
|
geometry: new LineString([
|
||||||
|
fromLonLat(offsetCoordinate(pivot, 180, crosshairRadius)),
|
||||||
|
fromLonLat(offsetCoordinate(pivot, 0, crosshairRadius)),
|
||||||
|
]),
|
||||||
|
signal_transform: 'crosshair',
|
||||||
|
}),
|
||||||
|
new Feature({
|
||||||
|
geometry: new LineString([fromLonLat(mastEnd), fromLonLat(rotateHandle)]),
|
||||||
|
signal_transform: 'rotate-leader',
|
||||||
|
}),
|
||||||
|
new Feature({
|
||||||
|
geometry: new Point(fromLonLat(rotateHandle)),
|
||||||
|
signal_transform: 'rotate-handle',
|
||||||
|
signal_uid: signalSelectionUid,
|
||||||
|
signal_pivot: pivot,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
}, [editMode, signalSelectionUid, state.trafficSignals.assemblies.features]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Selection changes only affect presentation; compiler GeoJSON stays untouched.
|
// Selection changes only affect presentation; compiler GeoJSON stays untouched.
|
||||||
layersRef.current?.markings.changed();
|
layersRef.current?.markings.changed();
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ export function updateLayers(layers: ReturnType<typeof createLayers>, state: Wor
|
|||||||
{
|
{
|
||||||
type: 'Feature' as const,
|
type: 'Feature' as const,
|
||||||
geometry: { type: 'Point', coordinates: faceEnd },
|
geometry: { type: 'Point', coordinates: faceEnd },
|
||||||
properties: { ...properties, signal_component: 'face-tip', signal_heading: faceHeading },
|
properties: { ...properties, signal_component: 'face-tip', signal_heading: faceHeading, signal_origin: point },
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user