OpenDataVisualizer/client/src/hydrology/format.ts
Claudio Schaad 58e4079ced Add live water layer from BAFU hydrology data (#6)
- New Schaad.OpenData.Hydrology project: LindasHydroClient fetches the
  current level, discharge, water temperature and danger level of nine
  BAFU stations around Thurgau from LINDAS every 10 minutes
- LINDAS has no history, so the server keeps readings per day (60 days)
  and fills gaps from the 7-day plot JSON of hydrodaten.admin.ch
- API: /api/hydrology/meta, /stations, /stations/{id}/history
- Combined map: "Gewässer (live)" layer with river circles (size by
  discharge, colour by danger level or water temperature), lake level
  badges, station panel with 7-day chart, overview and municipality
  context entries
- Docs: docs/datasets/gewaesser.md, overview in docs/datasets.md,
  CLAUDE.md; Docker and appsettings wiring

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>
2026-11-18 19:41:23 +00:00

101 lines
3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { HydroStation } from '../api/hydrology';
export type HydroMetric = 'discharge' | 'temperature';
export const HYDRO_METRICS: { key: HydroMetric; label: string }[] = [
{ key: 'discharge', label: 'Abfluss' },
{ key: 'temperature', label: 'Wassertemperatur' },
];
export const DANGER_LEVELS = [
{ level: 1, label: 'keine oder geringe Gefahr', color: '#16a34a' },
{ level: 2, label: 'mässige Gefahr', color: '#eab308' },
{ level: 3, label: 'erhebliche Gefahr', color: '#f97316' },
{ level: 4, label: 'grosse Gefahr', color: '#dc2626' },
{ level: 5, label: 'sehr grosse Gefahr', color: '#7f1d1d' },
];
const NO_VALUE_COLOR = '#94a3b8';
const TEMPERATURE_STOPS: [number, string][] = [
[0, '#1e3a8a'],
[5, '#2563eb'],
[10, '#0ea5e9'],
[15, '#14b8a6'],
[20, '#f59e0b'],
[25, '#dc2626'],
];
export function dangerColor(level: number | null): string {
return DANGER_LEVELS.find((d) => d.level === level)?.color ?? NO_VALUE_COLOR;
}
export function dangerLabel(level: number | null): string {
return DANGER_LEVELS.find((d) => d.level === level)?.label ?? 'keine Angabe';
}
export function temperatureColor(value: number | null): string {
if (value === null) {
return NO_VALUE_COLOR;
}
let color = TEMPERATURE_STOPS[0][1];
for (const [threshold, stopColor] of TEMPERATURE_STOPS) {
if (value >= threshold) {
color = stopColor;
}
}
return color;
}
export const TEMPERATURE_LEGEND = TEMPERATURE_STOPS.map(([threshold, color]) => ({
label: `ab ${threshold} °C`,
color,
}));
export function stationColor(station: HydroStation, metric: HydroMetric): string {
return metric === 'temperature'
? temperatureColor(station.waterTemperature)
: dangerColor(station.dangerLevel);
}
export function formatDecimal(value: number | null, digits: number): string {
return value === null
? '–'
: value.toLocaleString('de-CH', {
minimumFractionDigits: digits,
maximumFractionDigits: digits,
});
}
export function formatDischarge(value: number | null): string {
return value === null ? '–' : `${formatDecimal(value, value < 10 ? 2 : 1)} m³/s`;
}
export function formatLevelChange(meters: number | null): string {
if (meters === null) {
return '';
}
const cm = Math.round(meters * 100);
return cm === 0 ? '± 0 cm' : `${cm > 0 ? '▲ +' : '▼ −'}${Math.abs(cm)} cm`;
}
export function dischargeTrend(station: HydroStation): string {
if (station.discharge === null || station.discharge24hAgo === null) {
return '';
}
const change = station.discharge - station.discharge24hAgo;
if (Math.abs(change) < Math.max(0.05, station.discharge24hAgo * 0.03)) {
return '→';
}
return change > 0 ? '↗' : '↘';
}
export function formatTime(value: string | null): string {
return value
? new Date(value).toLocaleTimeString('de-CH', { hour: '2-digit', minute: '2-digit' })
: '–';
}
export function stationUrl(id: string): string {
return `https://www.hydrodaten.admin.ch/de/seen-und-fluesse/stationen-und-daten/${encodeURIComponent(id)}`;
}