307 lines
11 KiB
TypeScript
307 lines
11 KiB
TypeScript
import Feature from 'ol/Feature';
|
|
import GeoJSON from 'ol/format/GeoJSON';
|
|
import LineString from 'ol/geom/LineString';
|
|
import Point from 'ol/geom/Point';
|
|
import VectorLayer from 'ol/layer/Vector';
|
|
import VectorSource from 'ol/source/Vector';
|
|
import CircleStyle from 'ol/style/Circle';
|
|
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 Text from 'ol/style/Text';
|
|
import { offsetCoordinate, type Coordinate } from '../edit/meters';
|
|
import type { Road, WorkbenchState } from '../types/state';
|
|
import type { JunctionDescriptor } from '../junction/hotspots';
|
|
|
|
const geojson = new GeoJSON();
|
|
const source = () => new VectorSource();
|
|
export type LayerName =
|
|
| 'native'
|
|
| 'sidewalks'
|
|
| 'osm'
|
|
| 'reference'
|
|
| 'lanes'
|
|
| 'edgeLines'
|
|
| 'directionArrows'
|
|
| 'markings'
|
|
| 'centerLines'
|
|
| 'controls'
|
|
| 'signals'
|
|
| 'connectors'
|
|
| 'diagnostics'
|
|
| 'selectedRoad'
|
|
| 'junctionHotspots';
|
|
export function createLayers(
|
|
onRoad: () => Road | null,
|
|
scene: () => boolean,
|
|
hiddenMarkingIds: () => ReadonlySet<string> = () => new Set(),
|
|
) {
|
|
const simple = (color: string, width = 1) =>
|
|
new Style({ fill: new Fill({ color: `${color}55` }), stroke: new Stroke({ color, width }) });
|
|
const layers: Record<LayerName, VectorLayer<VectorSource>> = {
|
|
native: new VectorLayer({
|
|
source: source(),
|
|
style: (feature) => {
|
|
if (feature.get('pavement_input')) return undefined;
|
|
if (feature.get('kind') === 'complex-pavement-region')
|
|
return scene() ? new Style({ fill: new Fill({ color: '#3f4b50' }) }) : simple('#296956');
|
|
return scene() ? new Style({ fill: new Fill({ color: '#3f4b50' }) }) : simple('#296956');
|
|
},
|
|
}),
|
|
sidewalks: new VectorLayer({
|
|
source: source(),
|
|
style: (feature) =>
|
|
feature.get('kind') === 'island-green'
|
|
? new Style({ fill: new Fill({ color: '#35b86a' }), zIndex: 1 })
|
|
: scene()
|
|
? new Style({ fill: new Fill({ color: '#b7b9ad' }) })
|
|
: simple('#9b7c40'),
|
|
}),
|
|
osm: new VectorLayer({
|
|
source: source(),
|
|
style: (f) =>
|
|
new Style({
|
|
stroke: new Stroke({
|
|
color: f.get('road_id') === onRoad()?.id ? '#006e91' : '#263630',
|
|
width: f.get('road_id') === onRoad()?.id ? 5 : 2,
|
|
}),
|
|
text: f.get('road_label_visible')
|
|
? new Text({
|
|
text: String(f.get('road_label') || ''),
|
|
font: '600 12px sans-serif',
|
|
fill: new Fill({ color: '#f8fafc' }),
|
|
stroke: new Stroke({ color: '#17211f', width: 4 }),
|
|
overflow: true,
|
|
})
|
|
: undefined,
|
|
}),
|
|
}),
|
|
reference: new VectorLayer({
|
|
source: source(),
|
|
visible: false,
|
|
opacity: 0.55,
|
|
style: new Style({
|
|
fill: new Fill({ color: '#7c3aed22' }),
|
|
stroke: new Stroke({ color: '#7c3aed', width: 2, lineDash: [6, 4] }),
|
|
}),
|
|
}),
|
|
lanes: new VectorLayer({
|
|
source: source(),
|
|
style: new Style({ stroke: new Stroke({ color: '#f5f6ee', width: 1.5, lineDash: [5, 4] }) }),
|
|
}),
|
|
edgeLines: new VectorLayer({
|
|
source: source(),
|
|
visible: false,
|
|
style: new Style({ stroke: new Stroke({ color: '#f5f6ee', width: 1 }) }),
|
|
}),
|
|
directionArrows: new VectorLayer({
|
|
source: source(),
|
|
style: new Style({ fill: new Fill({ color: '#f5f6ee' }), stroke: new Stroke({ color: '#d9dacf', width: 1 }) }),
|
|
}),
|
|
markings: new VectorLayer({
|
|
source: source(),
|
|
style: (feature) =>
|
|
hiddenMarkingIds().has(String(feature.get('native_id')))
|
|
? undefined
|
|
: new Style({ fill: new Fill({ color: '#f5f6ee' }), stroke: new Stroke({ color: '#d9dacf', width: 1 }) }),
|
|
}),
|
|
centerLines: new VectorLayer({
|
|
source: source(),
|
|
style: (feature) => {
|
|
const white = feature.get('color') === 'white';
|
|
const color = white ? '#faf9ee' : '#f5be2a';
|
|
const solid = feature.get('pattern') === 'solid';
|
|
return new Style({
|
|
fill: new Fill({ color }),
|
|
// OpenLayers widths are screen pixels, not meters. The compiler's
|
|
// physical marking width therefore cannot be used here directly.
|
|
stroke: new Stroke({ color: solid ? color : white ? '#aeb0aa' : '#d29d16', width: solid ? 1.5 : 1.25 }),
|
|
});
|
|
},
|
|
}),
|
|
controls: new VectorLayer({
|
|
source: source(),
|
|
style: new Style({ fill: new Fill({ color: '#f5f6ee' }), stroke: new Stroke({ color: '#d9dacf', width: 1 }) }),
|
|
}),
|
|
signals: new VectorLayer({
|
|
source: source(),
|
|
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(),
|
|
style: new Style({ stroke: new Stroke({ color: '#ad3a76', width: 2, lineDash: [7, 5] }) }),
|
|
}),
|
|
diagnostics: new VectorLayer({
|
|
source: source(),
|
|
style: (f) =>
|
|
new Style({
|
|
image: new CircleStyle({
|
|
radius: 6,
|
|
fill: new Fill({ color: f.get('severity') === 'error' ? '#bf3b2e' : '#d49318' }),
|
|
stroke: new Stroke({ color: '#fff', width: 1 }),
|
|
}),
|
|
}),
|
|
}),
|
|
selectedRoad: new VectorLayer({
|
|
source: source(),
|
|
style: new Style({ stroke: new Stroke({ color: '#00a5cf', width: 8 }) }),
|
|
}),
|
|
junctionHotspots: new VectorLayer({
|
|
source: source(),
|
|
zIndex: 80,
|
|
style: (feature) => {
|
|
const status = feature.get('junction_status');
|
|
const color = status === 'editable' ? '#0e7490' : status === 'candidate' ? '#d49318' : '#64748b';
|
|
return new Style({
|
|
image: new CircleStyle({
|
|
radius: status === 'candidate' ? 9 : 8,
|
|
fill: new Fill({ color: `${color}33` }),
|
|
stroke: new Stroke({ color, width: 2 }),
|
|
}),
|
|
});
|
|
},
|
|
}),
|
|
};
|
|
return layers;
|
|
}
|
|
|
|
export function updateJunctionHotspots(
|
|
layers: ReturnType<typeof createLayers>,
|
|
descriptors: JunctionDescriptor[],
|
|
visible = true,
|
|
) {
|
|
const target = layers.junctionHotspots;
|
|
target.setVisible(visible);
|
|
const source = target.getSource()!;
|
|
source.clear();
|
|
source.addFeatures(
|
|
descriptors.map(
|
|
(descriptor) =>
|
|
new Feature({
|
|
geometry: new Point([descriptor.coordinate[0], descriptor.coordinate[1]]).transform('EPSG:4326', 'EPSG:3857'),
|
|
junction_ref: descriptor.ref,
|
|
junction_status: descriptor.status,
|
|
junction_descriptor: descriptor,
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
function features(value: unknown) {
|
|
return geojson.readFeatures((value || { type: 'FeatureCollection', features: [] }) as object, {
|
|
dataProjection: 'EPSG:4326',
|
|
featureProjection: 'EPSG:3857',
|
|
});
|
|
}
|
|
export function updateLayers(
|
|
layers: ReturnType<typeof createLayers>,
|
|
state: WorkbenchState,
|
|
selected: Road | null,
|
|
showRoadLabels = false,
|
|
) {
|
|
const get = (name: LayerName) => layers[name].getSource()!;
|
|
const put = (name: LayerName, value: unknown) => {
|
|
const target = get(name);
|
|
target.clear();
|
|
target.addFeatures(features(value));
|
|
};
|
|
put('native', state.layers.nativeRoadSurface);
|
|
get('native').addFeatures(features(state.layers.nativeIntersectionSurface));
|
|
put('sidewalks', state.layers.nativeSidewalkSurface);
|
|
const unnamedRoadNumber = new Map(
|
|
state.compiled.model.roads.filter((road) => !road.tags.name?.trim()).map((road, index) => [road.id, index + 1]),
|
|
);
|
|
const roads = state.compiled.model.roads.map(
|
|
(road) =>
|
|
new Feature({
|
|
geometry: new LineString(road.centerline).transform('EPSG:4326', 'EPSG:3857'),
|
|
road_id: road.id,
|
|
road_label_visible: showRoadLabels,
|
|
road_label: road.tags.name?.trim() || `道路${unnamedRoadNumber.get(road.id) || 1}`,
|
|
}),
|
|
);
|
|
put('osm', { type: 'FeatureCollection', features: [] });
|
|
get('osm').addFeatures(roads);
|
|
put('reference', state.junctionReference?.converted);
|
|
put('lanes', state.layers.laneCenterlines);
|
|
put('edgeLines', state.layers.edgeLines);
|
|
put('directionArrows', state.layers.directionArrows);
|
|
put('markings', state.layers.laneSeparators);
|
|
get('markings').addFeatures(features(state.layers.turnArrows));
|
|
put('centerLines', state.layers.centerLines);
|
|
put('controls', state.layers.crosswalks);
|
|
get('controls').addFeatures(features(state.layers.vehicleStopLines));
|
|
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, signal_origin: point },
|
|
},
|
|
];
|
|
});
|
|
put('signals', { type: 'FeatureCollection', features: signalFeatures });
|
|
put('connectors', state.layers.connectors);
|
|
put('diagnostics', {
|
|
type: 'FeatureCollection',
|
|
features: state.compiled.diagnostics
|
|
.filter((item) => item.geometry)
|
|
.map(({ geometry, ...properties }) => ({ type: 'Feature', properties, geometry: geometry! })),
|
|
});
|
|
updateSelectedRoad(layers, selected);
|
|
}
|
|
|
|
export function updateSelectedRoad(layers: ReturnType<typeof createLayers>, selected: Road | null) {
|
|
const source = layers.selectedRoad.getSource()!;
|
|
source.clear();
|
|
if (selected)
|
|
source.addFeature(
|
|
new Feature({ geometry: new LineString(selected.centerline).transform('EPSG:4326', 'EPSG:3857') }),
|
|
);
|
|
}
|