- One shared Leaflet map (MapHost) with the traffic, watercraft and vehicle maps turned into layer components that attach to it and clean up on unmount; each layer adds its own data credit - Data loading and per-dataset state moved into hooks used by both the single pages and the combined page - /karte/thurgau: layer panel with on/off switch, controls and legend per dataset, shared year controls for the yearly layers, traffic day/time controls, layers and year in the URL - Side panel shows the selected municipality, harbour or station, or an overview of all active layers; the municipality panel lists the counting stations and harbours in that municipality - Start page shows only the combined map; single maps are linked from the layer cards - Docs: combined map in docs/datasets.md and CLAUDE.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
157 lines
4.7 KiB
TypeScript
157 lines
4.7 KiB
TypeScript
import L from 'leaflet';
|
||
import { useEffect, useRef } from 'react';
|
||
import type { DayView, Station } from '../api/traffic';
|
||
import { escapeHtml } from '../map/baseLayers';
|
||
import { useMap, useMapAttribution } from '../map/mapContext';
|
||
import { FlowAnimator } from '../traffic/flowAnimator';
|
||
import { sumCounts } from '../traffic/vehicleClasses';
|
||
|
||
interface TrafficLayerProps {
|
||
stations: Station[];
|
||
day: DayView | null;
|
||
hour: number;
|
||
selectedCode: number | null;
|
||
onSelect: (code: number) => void;
|
||
onAnimatorReady: (animator: FlowAnimator) => void;
|
||
fitToData?: boolean;
|
||
}
|
||
|
||
const DATA_ATTRIBUTION =
|
||
'Verkehr: <a href="https://data.tg.ch/explore/dataset/dbu-tba-1/">Kanton Thurgau</a>';
|
||
|
||
export function TrafficLayer({
|
||
stations,
|
||
day,
|
||
hour,
|
||
selectedCode,
|
||
onSelect,
|
||
onAnimatorReady,
|
||
fitToData = true,
|
||
}: TrafficLayerProps) {
|
||
const map = useMap();
|
||
useMapAttribution(DATA_ATTRIBUTION);
|
||
const animatorRef = useRef<FlowAnimator | null>(null);
|
||
const roadsRef = useRef<L.LayerGroup | null>(null);
|
||
const markersRef = useRef<Map<number, L.CircleMarker>>(new Map());
|
||
const onSelectRef = useRef(onSelect);
|
||
onSelectRef.current = onSelect;
|
||
const fittedRef = useRef(!fitToData);
|
||
const dayRef = useRef(day);
|
||
dayRef.current = day;
|
||
|
||
useEffect(() => {
|
||
const canvas = document.createElement('canvas');
|
||
canvas.className = 'flow-canvas';
|
||
map.getContainer().appendChild(canvas);
|
||
|
||
const roads = L.layerGroup().addTo(map);
|
||
const animator = new FlowAnimator(map, canvas);
|
||
animator.start();
|
||
|
||
roadsRef.current = roads;
|
||
animatorRef.current = animator;
|
||
onAnimatorReady(animator);
|
||
const markers = markersRef.current;
|
||
|
||
return () => {
|
||
animator.dispose();
|
||
canvas.remove();
|
||
roads.remove();
|
||
markers.forEach((marker) => marker.remove());
|
||
markers.clear();
|
||
roadsRef.current = null;
|
||
animatorRef.current = null;
|
||
};
|
||
}, [map, onAnimatorReady]);
|
||
|
||
useEffect(() => {
|
||
const roads = roadsRef.current;
|
||
if (!roads) {
|
||
return;
|
||
}
|
||
|
||
roads.clearLayers();
|
||
markersRef.current.forEach((marker) => marker.remove());
|
||
markersRef.current.clear();
|
||
|
||
for (const station of stations) {
|
||
for (const direction of station.directions.slice(0, 1)) {
|
||
if (direction.path.length >= 2) {
|
||
L.polyline(direction.path, {
|
||
color: '#334155',
|
||
weight: 5,
|
||
opacity: 0.18,
|
||
interactive: false,
|
||
}).addTo(roads);
|
||
}
|
||
}
|
||
|
||
const marker = L.circleMarker([station.lat, station.lon], {
|
||
radius: 5,
|
||
color: '#0f172a',
|
||
weight: 1.5,
|
||
fillColor: '#ffffff',
|
||
fillOpacity: 0.85,
|
||
})
|
||
.on('click', () => {
|
||
onSelectRef.current(station.code);
|
||
if (map.getZoom() < 14) {
|
||
map.flyTo([station.lat, station.lon], 15, { duration: 0.8 });
|
||
}
|
||
})
|
||
.addTo(map);
|
||
markersRef.current.set(station.code, marker);
|
||
}
|
||
|
||
animatorRef.current?.setStations(stations);
|
||
animatorRef.current?.setDay(dayRef.current);
|
||
|
||
if (stations.length > 0 && !fittedRef.current) {
|
||
fittedRef.current = true;
|
||
map.fitBounds(L.latLngBounds(stations.map((s) => [s.lat, s.lon] as L.LatLngTuple)), {
|
||
padding: [40, 40],
|
||
});
|
||
}
|
||
}, [map, stations]);
|
||
|
||
useEffect(() => {
|
||
animatorRef.current?.setDay(day);
|
||
}, [day]);
|
||
|
||
useEffect(() => {
|
||
const hourIndex = Math.min(23, Math.max(0, hour));
|
||
const dayByStation = new Map(day?.stations.map((s) => [s.code, s]) ?? []);
|
||
|
||
for (const station of stations) {
|
||
const marker = markersRef.current.get(station.code);
|
||
if (!marker) {
|
||
continue;
|
||
}
|
||
|
||
const stationDay = dayByStation.get(station.code);
|
||
const perDirection = station.directions.map((direction) => {
|
||
const hours = stationDay?.directions.find((d) => d.direction === direction.name)?.hours;
|
||
return { name: direction.name, total: sumCounts(hours?.[hourIndex]) };
|
||
});
|
||
const total = perDirection.reduce((sum, d) => sum + d.total, 0);
|
||
const selected = station.code === selectedCode;
|
||
|
||
marker.setRadius(3 + Math.sqrt(total) / 6);
|
||
marker.setStyle({
|
||
color: selected ? '#dc2626' : '#0f172a',
|
||
weight: selected ? 3 : 1.5,
|
||
fillColor: selected ? '#fee2e2' : '#ffffff',
|
||
});
|
||
marker.bindTooltip(
|
||
`<strong>${escapeHtml(station.name)}</strong><br/>` +
|
||
`${escapeHtml(station.road)} · ${String(hourIndex).padStart(2, '0')}:00–${String(hourIndex + 1).padStart(2, '0')}:00<br/>` +
|
||
perDirection
|
||
.map((d) => `${escapeHtml(d.name)}: <strong>${d.total}</strong> Fz/h`)
|
||
.join('<br/>'),
|
||
{ direction: 'top', offset: [0, -6] },
|
||
);
|
||
}
|
||
}, [map, stations, day, hour, selectedCode]);
|
||
|
||
return null;
|
||
}
|