diff --git a/src/traffic-signals.js b/src/traffic-signals.js index aa4691e..88e2459 100644 --- a/src/traffic-signals.js +++ b/src/traffic-signals.js @@ -6,6 +6,7 @@ const { parseOsm } = require('./osm'); const EARTH_RADIUS = 6371008.8; const CURB_OFFSET_METERS = 5.2; +const MAX_SIGNAL_POLE_DISTANCE_FROM_STOP_METERS = 40; const MAST_REACH_METERS = 4.5; const SIGNAL_LAYOUT = Object.freeze({ poleHeightMeters: 6.7, @@ -195,6 +196,13 @@ function validateTrafficSignalFeatures(collection) { input.face_heading_deg == null || input.face_heading_deg === '' ? normalizeDegrees((legacyHeading == null ? 0 : legacyHeading) + 180) : 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 { type: 'Feature', geometry: { type: 'Point', coordinates: feature.geometry.coordinates.slice(0, 2).map(Number) }, @@ -212,8 +220,8 @@ function validateTrafficSignalFeatures(collection) { face_heading_deg: faceHeading, phase_group: phaseGroup, mast_reach_m: number('mast_reach_m', { min: 0.1, max: 30 }), - stop_lon: number('stop_lon', { min: -180, max: 180 }), - stop_lat: number('stop_lat', { min: -90, max: 90 }), + stop_lon: stopLon, + stop_lat: stopLat, enabled, z_offset_m: number('z_offset_m', { min: -20, max: 100 }), }, diff --git a/workbench/client/src/App.tsx b/workbench/client/src/App.tsx index 5824f85..bea2625 100644 --- a/workbench/client/src/App.tsx +++ b/workbench/client/src/App.tsx @@ -1,5 +1,5 @@ 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 { MapCanvas } from './components/MapCanvas'; import { ParameterPanel, type ParameterPanelHandle } from './components/ParameterPanel'; @@ -19,6 +19,7 @@ import type { ElementOutline } from './edit/handle-layer'; import { azimuthBetween, coordinateAtStation, + haversineMeters, metersAlongPolyline, polylineLengthMeters, toLonLat, @@ -116,6 +117,7 @@ function App() { const signalRedoRef = useRef([]); const signalSavedRef = useRef(null); const signalMoveActiveRef = useRef(false); + const signalRotateActiveRef = useRef(false); const [layers, setLayers] = useState(defaultLayers); const [scene, setScene] = useState(false); const [status, setStatus] = useState('正在加载工作台...'); @@ -739,6 +741,20 @@ function App() { signalRedoRef.current = []; 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 { ...current, trafficSignals: { @@ -747,7 +763,7 @@ function App() { ...current.trafficSignals.assemblies, features: current.trafficSignals.assemblies.features.map((feature) => feature.properties?.signal_uid === uid - ? { ...feature, geometry: { type: 'Point', coordinates: coordinate } } + ? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } } : feature, ), }, @@ -759,15 +775,80 @@ function App() { const finishSignalMove = (uid: string, coordinate: Coordinate) => { const current = state; 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) => feature.properties?.signal_uid === uid - ? { ...feature, geometry: { type: 'Point', coordinates: coordinate } } + ? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } } : feature, ); signalDraftRef.current = features; signalMoveActiveRef.current = false; 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 ( <>
@@ -809,17 +890,11 @@ function App() { 导出道路包 - {editMode ? ( <> - @@ -898,9 +973,15 @@ function App() { scene={scene} onSelectRoad={setSelected} onFeature={handleFeature} - onBackground={() => setElement(null)} + signalSelectionUid={typeof marking?.signal_uid === 'string' ? marking.signal_uid : null} + onBackground={() => { + setElement(null); + setMarking(null); + }} onSignalDrag={moveSignal} onSignalDragEnd={finishSignalMove} + onSignalRotate={rotateSignal} + onSignalRotateEnd={finishSignalRotate} handles={editHandles} ranges={editRanges} element={elementOutline} @@ -1435,9 +1516,19 @@ function SignalEditor({ : null; const saveField = (key: string, value: unknown) => { if (!feature) return; + const mastHeading = key === 'mast_heading_deg' ? ((Number(value) % 360) + 360) % 360 : null; void update( 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({