Add Thurgau traffic visualizer and start page #1
11 changed files with 548 additions and 173 deletions
|
|
@ -66,6 +66,7 @@ When the geometry algorithm changes, bump `GeometryFileName` in `TrafficCacheSto
|
|||
|
||||
### Client
|
||||
|
||||
- Routing with `react-router-dom`: `/` is the start page (`pages/HomePage.tsx`) with one large tile per map, each map has its own page (`/verkehr/thurgau` → `pages/TrafficThurgauPage.tsx`). Tiles come from the registry in `src/maps.ts`; a new map needs an entry there (with a preview image in `public/previews/`) and a route in `App.tsx`. Deep links work because the server falls back to `index.html`.
|
||||
- `traffic/flowAnimator.ts` owns the animation clock (time of day in hours, play/pause, speed) and draws on a canvas above Leaflet's overlay pane. Dots spawn per direction at a rate proportional to the interpolated hourly count, colored and sized by vehicle group, offset to the right of the driving direction. It reports the time to React at ~10 Hz via `onTick`; React components only get the integer hour, so markers and charts don't re-render every frame.
|
||||
- The vehicle class order in `traffic/vehicleClasses.ts` must match the `VehicleClass` enum / `VehicleCounts.ToArray()` order; count arrays in the API are positional.
|
||||
- UI language is German (de-CH). Base maps are swisstopo WMTS layers (grey `pixelkarte-grau` as default, colour `pixelkarte-farbe`, aerial `swissimage`), switchable via Leaflet's layer control; the choice is stored in localStorage (`odv.baseMap`).
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>OpenDataVisualizer – Verkehr Thurgau</title>
|
||||
<title>OpenDataVisualizer</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
|
|
|||
41
client/package-lock.json
generated
41
client/package-lock.json
generated
|
|
@ -13,7 +13,8 @@
|
|||
"leaflet": "^1.9.4",
|
||||
"react": "^18.3.1",
|
||||
"react-chartjs-2": "^5.2.0",
|
||||
"react-dom": "^18.3.1"
|
||||
"react-dom": "^18.3.1",
|
||||
"react-router-dom": "^6.30.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/leaflet": "^1.9.12",
|
||||
|
|
@ -754,6 +755,14 @@
|
|||
"resolved": "https://registry.npmjs.org/@kurkle/color/-/color-0.3.4.tgz",
|
||||
"integrity": "sha512-M5UknZPHRu3DEDWoipU6sE8PdkZ6Z/S+v4dD+Ke8IaNlpdSQah50lz1KtcFBa2vsdOnwbbnxJwVM4wty6udA5w=="
|
||||
},
|
||||
"node_modules/@remix-run/router": {
|
||||
"version": "1.23.4",
|
||||
"resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.23.4.tgz",
|
||||
"integrity": "sha512-q7j5geK7xs3UJSdm9/iytUNclBnLmYx1EnSeCFXHPeutdqgIMeFeHtUZgS3EhlKxdBEAu8OwtJCwmLrEzpSs7Q==",
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@rolldown/pluginutils": {
|
||||
"version": "1.0.0-beta.27",
|
||||
"resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.0-beta.27.tgz",
|
||||
|
|
@ -1625,6 +1634,36 @@
|
|||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/react-router": {
|
||||
"version": "6.30.6",
|
||||
"resolved": "https://registry.npmjs.org/react-router/-/react-router-6.30.6.tgz",
|
||||
"integrity": "sha512-5HfK7k5im7LTOB0EqCQmfvy4C13G92Ssj1VTmouTK3AJvyjKTnFuCV0vcMAD/JS+JC4DvDIBRrlAeJIFjh5VWg==",
|
||||
"dependencies": {
|
||||
"@remix-run/router": "1.23.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8"
|
||||
}
|
||||
},
|
||||
"node_modules/react-router-dom": {
|
||||
"version": "6.30.6",
|
||||
"resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.30.6.tgz",
|
||||
"integrity": "sha512-0RHKZz7wwffvkU+2MFVT2NnjK44ssLEV+m0CAJaS2Ksmorrwj7WxH00jO0SOCW26/tINUnJHToXblDs33I38YQ==",
|
||||
"dependencies": {
|
||||
"@remix-run/router": "1.23.4",
|
||||
"react-router": "6.30.6"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8",
|
||||
"react-dom": ">=16.8"
|
||||
}
|
||||
},
|
||||
"node_modules/rollup": {
|
||||
"version": "4.68.0",
|
||||
"resolved": "https://registry.npmjs.org/rollup/-/rollup-4.68.0.tgz",
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@
|
|||
"leaflet": "^1.9.4",
|
||||
"react": "^18.3.1",
|
||||
"react-chartjs-2": "^5.2.0",
|
||||
"react-dom": "^18.3.1"
|
||||
"react-dom": "^18.3.1",
|
||||
"react-router-dom": "^6.30.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/leaflet": "^1.9.12",
|
||||
|
|
|
|||
74
client/public/previews/traffic-thurgau.svg
Normal file
74
client/public/previews/traffic-thurgau.svg
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 450">
|
||||
<defs>
|
||||
<linearGradient id="land" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0" stop-color="#eef2ec"/>
|
||||
<stop offset="1" stop-color="#e3e8e1"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<rect width="800" height="450" fill="url(#land)"/>
|
||||
<path d="M520 0 C560 60 640 90 800 70 L800 0 Z" fill="#cfe0ee"/>
|
||||
<path d="M590 0 C610 30 660 45 720 40 C760 37 790 30 800 25 L800 0 Z" fill="#bcd4e8"/>
|
||||
<path d="M0 330 C120 300 210 330 300 300 C380 272 420 300 520 290" fill="none" stroke="#d6dcd3" stroke-width="10"/>
|
||||
<g fill="none" stroke="#9aa5b1" stroke-linecap="round">
|
||||
<path d="M-20 260 C120 250 220 210 330 220 C450 230 520 180 640 170 C720 163 780 150 820 140" stroke-width="14"/>
|
||||
<path d="M140 470 C170 380 230 300 330 220 C400 165 430 100 470 -20" stroke-width="12"/>
|
||||
<path d="M330 220 C380 270 470 330 620 360 C700 375 760 400 820 420" stroke-width="10"/>
|
||||
<path d="M640 170 C650 230 640 300 620 360" stroke-width="8"/>
|
||||
</g>
|
||||
<g fill="none" stroke="#ffffff" stroke-width="1.5" stroke-dasharray="10 9" opacity="0.8">
|
||||
<path d="M-20 260 C120 250 220 210 330 220 C450 230 520 180 640 170 C720 163 780 150 820 140"/>
|
||||
<path d="M140 470 C170 380 230 300 330 220 C400 165 430 100 470 -20"/>
|
||||
</g>
|
||||
<g>
|
||||
<circle cx="40" cy="252" r="5" fill="#2563eb"/>
|
||||
<circle cx="78" cy="250" r="5" fill="#2563eb"/>
|
||||
<circle cx="105" cy="248" r="5.5" fill="#16a34a"/>
|
||||
<circle cx="150" cy="242" r="5" fill="#2563eb"/>
|
||||
<circle cx="196" cy="230" r="7" fill="#ea580c"/>
|
||||
<circle cx="232" cy="219" r="5" fill="#2563eb"/>
|
||||
<circle cx="268" cy="213" r="5" fill="#2563eb"/>
|
||||
<circle cx="300" cy="212" r="5" fill="#0d9488"/>
|
||||
<circle cx="60" cy="266" r="5" fill="#2563eb"/>
|
||||
<circle cx="128" cy="261" r="5" fill="#2563eb"/>
|
||||
<circle cx="170" cy="252" r="5" fill="#2563eb"/>
|
||||
<circle cx="250" cy="231" r="5.5" fill="#16a34a"/>
|
||||
<circle cx="290" cy="227" r="5" fill="#2563eb"/>
|
||||
<circle cx="372" cy="216" r="5" fill="#2563eb"/>
|
||||
<circle cx="420" cy="220" r="5" fill="#2563eb"/>
|
||||
<circle cx="470" cy="210" r="7" fill="#9333ea"/>
|
||||
<circle cx="520" cy="190" r="5" fill="#2563eb"/>
|
||||
<circle cx="566" cy="174" r="5" fill="#2563eb"/>
|
||||
<circle cx="612" cy="166" r="7" fill="#ea580c"/>
|
||||
<circle cx="690" cy="157" r="5" fill="#2563eb"/>
|
||||
<circle cx="740" cy="146" r="5" fill="#2563eb"/>
|
||||
<circle cx="398" cy="236" r="5" fill="#2563eb"/>
|
||||
<circle cx="448" cy="234" r="5" fill="#2563eb"/>
|
||||
<circle cx="545" cy="196" r="5.5" fill="#16a34a"/>
|
||||
<circle cx="660" cy="180" r="5" fill="#2563eb"/>
|
||||
<circle cx="720" cy="168" r="5" fill="#2563eb"/>
|
||||
<circle cx="168" cy="420" r="5" fill="#2563eb"/>
|
||||
<circle cx="190" cy="370" r="5" fill="#2563eb"/>
|
||||
<circle cx="228" cy="320" r="7" fill="#ea580c"/>
|
||||
<circle cx="270" cy="280" r="5" fill="#2563eb"/>
|
||||
<circle cx="388" cy="166" r="5" fill="#2563eb"/>
|
||||
<circle cx="414" cy="124" r="5.5" fill="#16a34a"/>
|
||||
<circle cx="440" cy="66" r="5" fill="#2563eb"/>
|
||||
<circle cx="200" cy="400" r="5" fill="#2563eb"/>
|
||||
<circle cx="246" cy="318" r="5" fill="#2563eb"/>
|
||||
<circle cx="372" cy="196" r="5" fill="#2563eb"/>
|
||||
<circle cx="402" cy="146" r="5" fill="#2563eb"/>
|
||||
<circle cx="380" cy="268" r="5" fill="#2563eb"/>
|
||||
<circle cx="440" cy="306" r="5" fill="#2563eb"/>
|
||||
<circle cx="520" cy="338" r="7" fill="#ea580c"/>
|
||||
<circle cx="590" cy="350" r="5" fill="#2563eb"/>
|
||||
<circle cx="700" cy="384" r="5" fill="#2563eb"/>
|
||||
<circle cx="648" cy="230" r="5" fill="#2563eb"/>
|
||||
<circle cx="640" cy="300" r="5.5" fill="#16a34a"/>
|
||||
</g>
|
||||
<g fill="#ffffff" stroke="#0f172a" stroke-width="3">
|
||||
<circle cx="330" cy="220" r="14"/>
|
||||
<circle cx="640" cy="170" r="11"/>
|
||||
<circle cx="620" cy="360" r="9"/>
|
||||
<circle cx="96" cy="256" r="8"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.8 KiB |
|
|
@ -1,174 +1,13 @@
|
|||
import { keepPreviousData, useQuery } from '@tanstack/react-query';
|
||||
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { fetchDay, fetchMeta, fetchStations } from './api/traffic';
|
||||
import { Legend } from './components/Legend';
|
||||
import { StationPanel } from './components/StationPanel';
|
||||
import { SPEEDS, TimeControls } from './components/TimeControls';
|
||||
import { TrafficMap } from './components/TrafficMap';
|
||||
import type { FlowAnimator } from './traffic/flowAnimator';
|
||||
import { formatNumber } from './traffic/format';
|
||||
import { sumCounts } from './traffic/vehicleClasses';
|
||||
|
||||
const MemoStationPanel = memo(StationPanel);
|
||||
|
||||
const INITIAL_TIME = 7.5;
|
||||
import { Navigate, Route, Routes } from 'react-router-dom';
|
||||
import { HomePage } from './pages/HomePage';
|
||||
import { TrafficThurgauPage } from './pages/TrafficThurgauPage';
|
||||
|
||||
export function App() {
|
||||
const metaQuery = useQuery({
|
||||
queryKey: ['meta'],
|
||||
queryFn: fetchMeta,
|
||||
refetchInterval: 10 * 60_000,
|
||||
});
|
||||
const stationsQuery = useQuery({
|
||||
queryKey: ['stations'],
|
||||
queryFn: fetchStations,
|
||||
refetchInterval: (query) =>
|
||||
query.state.data?.some((s) => s.directions.some((d) => d.path.length < 2)) ? 30_000 : false,
|
||||
});
|
||||
|
||||
const dates = useMemo(() => metaQuery.data?.dates ?? [], [metaQuery.data]);
|
||||
const [date, setDate] = useState<string | null>(null);
|
||||
const [time, setTime] = useState(INITIAL_TIME);
|
||||
const [playing, setPlaying] = useState(false);
|
||||
const [hoursPerSecond, setHoursPerSecond] = useState(SPEEDS[1].hoursPerSecond);
|
||||
const [selectedCode, setSelectedCode] = useState<number | null>(null);
|
||||
const animatorRef = useRef<FlowAnimator | null>(null);
|
||||
const datesRef = useRef(dates);
|
||||
datesRef.current = dates;
|
||||
|
||||
useEffect(() => {
|
||||
if (!date && dates.length > 0) {
|
||||
setDate(dates[dates.length - 1]);
|
||||
}
|
||||
}, [date, dates]);
|
||||
|
||||
const dayQuery = useQuery({
|
||||
queryKey: ['day', date],
|
||||
queryFn: () => fetchDay(date!),
|
||||
enabled: !!date,
|
||||
placeholderData: keepPreviousData,
|
||||
staleTime: 5 * 60_000,
|
||||
});
|
||||
const day = dayQuery.data ?? null;
|
||||
|
||||
const handleAnimatorReady = useCallback((animator: FlowAnimator) => {
|
||||
animatorRef.current = animator;
|
||||
animator.time = INITIAL_TIME;
|
||||
animator.onTick = setTime;
|
||||
animator.onDayEnd = () =>
|
||||
setDate((current) => {
|
||||
const all = datesRef.current;
|
||||
const index = current ? all.indexOf(current) : -1;
|
||||
return all.length === 0 ? current : all[(index + 1) % all.length];
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (animatorRef.current) {
|
||||
animatorRef.current.playing = playing;
|
||||
}
|
||||
}, [playing]);
|
||||
|
||||
useEffect(() => {
|
||||
if (animatorRef.current) {
|
||||
animatorRef.current.hoursPerSecond = hoursPerSecond;
|
||||
}
|
||||
}, [hoursPerSecond]);
|
||||
|
||||
const handleTimeChange = useCallback((value: number) => {
|
||||
if (animatorRef.current) {
|
||||
animatorRef.current.time = value;
|
||||
}
|
||||
setTime(value);
|
||||
}, []);
|
||||
|
||||
const handleClose = useCallback(() => setSelectedCode(null), []);
|
||||
|
||||
const hour = Math.min(23, Math.floor(time));
|
||||
const stations = useMemo(() => stationsQuery.data ?? [], [stationsQuery.data]);
|
||||
const selectedStation = stations.find((s) => s.code === selectedCode) ?? null;
|
||||
|
||||
const hourTotal = useMemo(
|
||||
() =>
|
||||
day?.stations.reduce(
|
||||
(sum, station) =>
|
||||
sum + station.directions.reduce((s, d) => s + sumCounts(d.hours[hour]), 0),
|
||||
0,
|
||||
) ?? 0,
|
||||
[day, hour],
|
||||
);
|
||||
|
||||
const error = metaQuery.error ?? stationsQuery.error ?? dayQuery.error;
|
||||
|
||||
return (
|
||||
<div className={`app ${selectedStation ? 'with-panel' : ''}`}>
|
||||
<header className="app-header">
|
||||
<div className="title">
|
||||
<h1>OpenDataVisualizer</h1>
|
||||
<span className="subtitle">
|
||||
Strassenverkehr Kanton Thurgau · stündliche Zählungen nach Fahrzeugklassen
|
||||
</span>
|
||||
</div>
|
||||
<div className="stats">
|
||||
<div>
|
||||
<span className="stat-value">{stations.length}</span>
|
||||
<span className="stat-label">Zählstellen</span>
|
||||
</div>
|
||||
<div>
|
||||
<span className="stat-value">{formatNumber(hourTotal)}</span>
|
||||
<span className="stat-label">Fahrzeuge in dieser Stunde</span>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main className="map-area">
|
||||
<TrafficMap
|
||||
stations={stations}
|
||||
day={day}
|
||||
hour={hour}
|
||||
selectedCode={selectedCode}
|
||||
onSelect={setSelectedCode}
|
||||
onAnimatorReady={handleAnimatorReady}
|
||||
/>
|
||||
|
||||
<div className="overlay-bottom">
|
||||
{date && (
|
||||
<TimeControls
|
||||
dates={dates}
|
||||
date={date}
|
||||
time={time}
|
||||
playing={playing}
|
||||
hoursPerSecond={hoursPerSecond}
|
||||
onDateChange={setDate}
|
||||
onTimeChange={handleTimeChange}
|
||||
onPlayingChange={setPlaying}
|
||||
onSpeedChange={setHoursPerSecond}
|
||||
/>
|
||||
)}
|
||||
<Legend />
|
||||
</div>
|
||||
|
||||
{(metaQuery.isLoading || (metaQuery.data && dates.length === 0)) && (
|
||||
<div className="notice">
|
||||
Verkehrsdaten werden geladen… (erster Start lädt die Daten von data.tg.ch)
|
||||
</div>
|
||||
)}
|
||||
{error && <div className="notice error">Fehler beim Laden: {String(error.message)}</div>}
|
||||
</main>
|
||||
|
||||
{selectedStation && (
|
||||
<MemoStationPanel station={selectedStation} day={day} hour={hour} onClose={handleClose} />
|
||||
)}
|
||||
|
||||
<footer className="app-footer">
|
||||
Quelle:{' '}
|
||||
<a href={metaQuery.data?.sourceUrl} target="_blank" rel="noreferrer">
|
||||
{metaQuery.data?.publisher ?? 'Kanton Thurgau'}
|
||||
</a>{' '}
|
||||
· Lizenz {metaQuery.data?.license ?? 'CC0 1.0'}
|
||||
{metaQuery.data?.lastRefresh &&
|
||||
` · aktualisiert ${new Date(metaQuery.data.lastRefresh).toLocaleString('de-CH')}`}
|
||||
</footer>
|
||||
</div>
|
||||
<Routes>
|
||||
<Route path="/" element={<HomePage />} />
|
||||
<Route path="/verkehr/thurgau" element={<TrafficThurgauPage />} />
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { BrowserRouter } from 'react-router-dom';
|
||||
import { StrictMode } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { App } from './App';
|
||||
|
|
@ -11,7 +12,9 @@ const queryClient = new QueryClient({
|
|||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BrowserRouter>
|
||||
<App />
|
||||
</BrowserRouter>
|
||||
</QueryClientProvider>
|
||||
</StrictMode>,
|
||||
);
|
||||
|
|
|
|||
22
client/src/maps.ts
Normal file
22
client/src/maps.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
export interface MapEntry {
|
||||
path: string;
|
||||
title: string;
|
||||
region: string;
|
||||
description: string;
|
||||
source: string;
|
||||
tags: string[];
|
||||
preview: string;
|
||||
}
|
||||
|
||||
export const MAPS: MapEntry[] = [
|
||||
{
|
||||
path: '/verkehr/thurgau',
|
||||
title: 'Strassenverkehr',
|
||||
region: 'Kanton Thurgau',
|
||||
description:
|
||||
'Fahrzeuge fahren animiert über die Strassen der 64 Zählstellen – stündlich nach Fahrzeugklasse, mit Tagesverlauf und Auswertungen pro Zählstelle.',
|
||||
source: 'Tiefbauamt Kanton Thurgau',
|
||||
tags: ['Verkehr', 'Stundenwerte', 'Animation'],
|
||||
preview: '/previews/traffic-thurgau.svg',
|
||||
},
|
||||
];
|
||||
47
client/src/pages/HomePage.tsx
Normal file
47
client/src/pages/HomePage.tsx
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { useEffect } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { MAPS } from '../maps';
|
||||
|
||||
export function HomePage() {
|
||||
useEffect(() => {
|
||||
document.title = 'OpenDataVisualizer';
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="home">
|
||||
<header className="home-header">
|
||||
<h1>OpenDataVisualizer</h1>
|
||||
<p>
|
||||
Karten und Visualisierungen zu offenen Daten der Schweiz von{' '}
|
||||
<a href="https://opendata.swiss" target="_blank" rel="noreferrer">
|
||||
opendata.swiss
|
||||
</a>
|
||||
.
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<main className="map-tiles">
|
||||
{MAPS.map((map) => (
|
||||
<Link key={map.path} to={map.path} className="map-tile">
|
||||
<div className="map-tile-preview">
|
||||
<img src={map.preview} alt="" />
|
||||
</div>
|
||||
<div className="map-tile-body">
|
||||
<span className="map-tile-region">{map.region}</span>
|
||||
<h2>{map.title}</h2>
|
||||
<p>{map.description}</p>
|
||||
<div className="map-tile-tags">
|
||||
{map.tags.map((tag) => (
|
||||
<span key={tag} className="tag">
|
||||
{tag}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
<span className="map-tile-source">Quelle: {map.source}</span>
|
||||
</div>
|
||||
</Link>
|
||||
))}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
187
client/src/pages/TrafficThurgauPage.tsx
Normal file
187
client/src/pages/TrafficThurgauPage.tsx
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
import { keepPreviousData, useQuery } from '@tanstack/react-query';
|
||||
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { fetchDay, fetchMeta, fetchStations } from '../api/traffic';
|
||||
import { Legend } from '../components/Legend';
|
||||
import { StationPanel } from '../components/StationPanel';
|
||||
import { SPEEDS, TimeControls } from '../components/TimeControls';
|
||||
import { TrafficMap } from '../components/TrafficMap';
|
||||
import type { FlowAnimator } from '../traffic/flowAnimator';
|
||||
import { formatNumber } from '../traffic/format';
|
||||
import { sumCounts } from '../traffic/vehicleClasses';
|
||||
|
||||
const MemoStationPanel = memo(StationPanel);
|
||||
|
||||
const INITIAL_TIME = 7.5;
|
||||
|
||||
export function TrafficThurgauPage() {
|
||||
const metaQuery = useQuery({
|
||||
queryKey: ['meta'],
|
||||
queryFn: fetchMeta,
|
||||
refetchInterval: 10 * 60_000,
|
||||
});
|
||||
const stationsQuery = useQuery({
|
||||
queryKey: ['stations'],
|
||||
queryFn: fetchStations,
|
||||
refetchInterval: (query) =>
|
||||
query.state.data?.some((s) => s.directions.some((d) => d.path.length < 2)) ? 30_000 : false,
|
||||
});
|
||||
|
||||
const dates = useMemo(() => metaQuery.data?.dates ?? [], [metaQuery.data]);
|
||||
const [date, setDate] = useState<string | null>(null);
|
||||
const [time, setTime] = useState(INITIAL_TIME);
|
||||
const [playing, setPlaying] = useState(false);
|
||||
const [hoursPerSecond, setHoursPerSecond] = useState(SPEEDS[1].hoursPerSecond);
|
||||
const [selectedCode, setSelectedCode] = useState<number | null>(null);
|
||||
const animatorRef = useRef<FlowAnimator | null>(null);
|
||||
useEffect(() => {
|
||||
document.title = 'Strassenverkehr Thurgau – OpenDataVisualizer';
|
||||
}, []);
|
||||
|
||||
const datesRef = useRef(dates);
|
||||
datesRef.current = dates;
|
||||
|
||||
useEffect(() => {
|
||||
if (!date && dates.length > 0) {
|
||||
setDate(dates[dates.length - 1]);
|
||||
}
|
||||
}, [date, dates]);
|
||||
|
||||
const dayQuery = useQuery({
|
||||
queryKey: ['day', date],
|
||||
queryFn: () => fetchDay(date!),
|
||||
enabled: !!date,
|
||||
placeholderData: keepPreviousData,
|
||||
staleTime: 5 * 60_000,
|
||||
});
|
||||
const day = dayQuery.data ?? null;
|
||||
|
||||
const handleAnimatorReady = useCallback((animator: FlowAnimator) => {
|
||||
animatorRef.current = animator;
|
||||
animator.time = INITIAL_TIME;
|
||||
animator.onTick = setTime;
|
||||
animator.onDayEnd = () =>
|
||||
setDate((current) => {
|
||||
const all = datesRef.current;
|
||||
const index = current ? all.indexOf(current) : -1;
|
||||
return all.length === 0 ? current : all[(index + 1) % all.length];
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (animatorRef.current) {
|
||||
animatorRef.current.playing = playing;
|
||||
}
|
||||
}, [playing]);
|
||||
|
||||
useEffect(() => {
|
||||
if (animatorRef.current) {
|
||||
animatorRef.current.hoursPerSecond = hoursPerSecond;
|
||||
}
|
||||
}, [hoursPerSecond]);
|
||||
|
||||
const handleTimeChange = useCallback((value: number) => {
|
||||
if (animatorRef.current) {
|
||||
animatorRef.current.time = value;
|
||||
}
|
||||
setTime(value);
|
||||
}, []);
|
||||
|
||||
const handleClose = useCallback(() => setSelectedCode(null), []);
|
||||
|
||||
const hour = Math.min(23, Math.floor(time));
|
||||
const stations = useMemo(() => stationsQuery.data ?? [], [stationsQuery.data]);
|
||||
const selectedStation = stations.find((s) => s.code === selectedCode) ?? null;
|
||||
|
||||
const hourTotal = useMemo(
|
||||
() =>
|
||||
day?.stations.reduce(
|
||||
(sum, station) =>
|
||||
sum + station.directions.reduce((s, d) => s + sumCounts(d.hours[hour]), 0),
|
||||
0,
|
||||
) ?? 0,
|
||||
[day, hour],
|
||||
);
|
||||
|
||||
const error = metaQuery.error ?? stationsQuery.error ?? dayQuery.error;
|
||||
|
||||
return (
|
||||
<div className={`app ${selectedStation ? 'with-panel' : ''}`}>
|
||||
<header className="app-header">
|
||||
<div className="title-row">
|
||||
<Link to="/" className="back-link" aria-label="Zur Übersicht" title="Zur Übersicht">
|
||||
←
|
||||
</Link>
|
||||
<div className="title">
|
||||
<h1>
|
||||
<Link to="/" className="brand-link">
|
||||
OpenDataVisualizer
|
||||
</Link>{' '}
|
||||
<span className="title-separator">/</span> Strassenverkehr Thurgau
|
||||
</h1>
|
||||
<span className="subtitle">Stündliche Zählungen nach Fahrzeugklassen</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="stats">
|
||||
<div>
|
||||
<span className="stat-value">{stations.length}</span>
|
||||
<span className="stat-label">Zählstellen</span>
|
||||
</div>
|
||||
<div>
|
||||
<span className="stat-value">{formatNumber(hourTotal)}</span>
|
||||
<span className="stat-label">Fahrzeuge in dieser Stunde</span>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main className="map-area">
|
||||
<TrafficMap
|
||||
stations={stations}
|
||||
day={day}
|
||||
hour={hour}
|
||||
selectedCode={selectedCode}
|
||||
onSelect={setSelectedCode}
|
||||
onAnimatorReady={handleAnimatorReady}
|
||||
/>
|
||||
|
||||
<div className="overlay-bottom">
|
||||
{date && (
|
||||
<TimeControls
|
||||
dates={dates}
|
||||
date={date}
|
||||
time={time}
|
||||
playing={playing}
|
||||
hoursPerSecond={hoursPerSecond}
|
||||
onDateChange={setDate}
|
||||
onTimeChange={handleTimeChange}
|
||||
onPlayingChange={setPlaying}
|
||||
onSpeedChange={setHoursPerSecond}
|
||||
/>
|
||||
)}
|
||||
<Legend />
|
||||
</div>
|
||||
|
||||
{(metaQuery.isLoading || (metaQuery.data && dates.length === 0)) && (
|
||||
<div className="notice">
|
||||
Verkehrsdaten werden geladen… (erster Start lädt die Daten von data.tg.ch)
|
||||
</div>
|
||||
)}
|
||||
{error && <div className="notice error">Fehler beim Laden: {String(error.message)}</div>}
|
||||
</main>
|
||||
|
||||
{selectedStation && (
|
||||
<MemoStationPanel station={selectedStation} day={day} hour={hour} onClose={handleClose} />
|
||||
)}
|
||||
|
||||
<footer className="app-footer">
|
||||
Quelle:{' '}
|
||||
<a href={metaQuery.data?.sourceUrl} target="_blank" rel="noreferrer">
|
||||
{metaQuery.data?.publisher ?? 'Kanton Thurgau'}
|
||||
</a>{' '}
|
||||
· Lizenz {metaQuery.data?.license ?? 'CC0 1.0'}
|
||||
{metaQuery.data?.lastRefresh &&
|
||||
` · aktualisiert ${new Date(metaQuery.data.lastRefresh).toLocaleString('de-CH')}`}
|
||||
</footer>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -358,3 +358,165 @@ select {
|
|||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
|
||||
.title-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--border);
|
||||
color: var(--text);
|
||||
text-decoration: none;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.back-link:hover {
|
||||
background: var(--bg);
|
||||
}
|
||||
|
||||
.brand-link {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.brand-link:hover {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.title-separator {
|
||||
color: var(--muted);
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.home {
|
||||
height: 100%;
|
||||
overflow-y: auto;
|
||||
padding: 48px 32px 64px;
|
||||
}
|
||||
|
||||
.home-header {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto 32px;
|
||||
}
|
||||
|
||||
.home-header h1 {
|
||||
margin: 0 0 6px;
|
||||
font-size: 32px;
|
||||
letter-spacing: -0.02em;
|
||||
}
|
||||
|
||||
.home-header p {
|
||||
margin: 0;
|
||||
color: var(--muted);
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.home-header a {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.map-tiles {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(min(100%, 480px), 1fr));
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.map-tile {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 16px;
|
||||
overflow: hidden;
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
|
||||
transition:
|
||||
transform 0.15s,
|
||||
box-shadow 0.15s,
|
||||
border-color 0.15s;
|
||||
}
|
||||
|
||||
.map-tile:hover,
|
||||
.map-tile:focus-visible {
|
||||
transform: translateY(-3px);
|
||||
border-color: #bfdbfe;
|
||||
box-shadow: 0 12px 32px rgba(15, 23, 42, 0.12);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.map-tile-preview {
|
||||
aspect-ratio: 16 / 9;
|
||||
background: #e3e8e1;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.map-tile-preview img {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.map-tile-body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
padding: 20px 22px 22px;
|
||||
}
|
||||
|
||||
.map-tile-region {
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.map-tile h2 {
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
.map-tile p {
|
||||
margin: 0;
|
||||
color: #334155;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.map-tile-tags {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.tag {
|
||||
font-size: 12px;
|
||||
padding: 3px 9px;
|
||||
border-radius: 999px;
|
||||
background: #eff6ff;
|
||||
color: #1d4ed8;
|
||||
}
|
||||
|
||||
.map-tile-source {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.home {
|
||||
padding: 28px 16px 40px;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue