- 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>
88 lines
2.6 KiB
TypeScript
88 lines
2.6 KiB
TypeScript
const METERS_PER_DEGREE_LAT = 110_540;
|
|
const METERS_PER_DEGREE_LON_AT_EQUATOR = 111_320;
|
|
|
|
export interface Outline {
|
|
id: string;
|
|
polygons: number[][][][];
|
|
}
|
|
|
|
export function containsPoint(polygons: number[][][][], lon: number, lat: number): boolean {
|
|
return polygons.some(
|
|
([outer, ...holes]) =>
|
|
!!outer && insideRing(outer, lon, lat) && !holes.some((hole) => insideRing(hole, lon, lat)),
|
|
);
|
|
}
|
|
|
|
export function distanceToOutline(polygons: number[][][][], lon: number, lat: number): number {
|
|
if (containsPoint(polygons, lon, lat)) {
|
|
return 0;
|
|
}
|
|
const kx = METERS_PER_DEGREE_LON_AT_EQUATOR * Math.cos((lat * Math.PI) / 180);
|
|
const ky = METERS_PER_DEGREE_LAT;
|
|
let best = Infinity;
|
|
for (const polygon of polygons) {
|
|
for (const ring of polygon) {
|
|
for (let i = 1; i < ring.length; i++) {
|
|
const ax = (ring[i - 1][0] - lon) * kx;
|
|
const ay = (ring[i - 1][1] - lat) * ky;
|
|
const bx = (ring[i][0] - lon) * kx;
|
|
const by = (ring[i][1] - lat) * ky;
|
|
best = Math.min(best, distanceToSegment(ax, ay, bx, by));
|
|
}
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
export function assignToOutlines<T>(
|
|
outlines: Outline[],
|
|
items: T[],
|
|
position: (item: T) => [number, number] | null,
|
|
maxDistanceMeters: number,
|
|
): Map<string, T[]> {
|
|
const result = new Map<string, T[]>();
|
|
for (const item of items) {
|
|
const point = position(item);
|
|
if (!point) {
|
|
continue;
|
|
}
|
|
const [lon, lat] = point;
|
|
let bestId: string | null = null;
|
|
let bestDistance = maxDistanceMeters;
|
|
for (const outline of outlines) {
|
|
const distance = distanceToOutline(outline.polygons, lon, lat);
|
|
if (distance <= bestDistance) {
|
|
bestId = outline.id;
|
|
bestDistance = distance;
|
|
if (distance === 0) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (bestId !== null) {
|
|
result.set(bestId, [...(result.get(bestId) ?? []), item]);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function insideRing(ring: number[][], x: number, y: number): boolean {
|
|
let inside = false;
|
|
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
const [xi, yi] = ring[i];
|
|
const [xj, yj] = ring[j];
|
|
if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) {
|
|
inside = !inside;
|
|
}
|
|
}
|
|
return inside;
|
|
}
|
|
|
|
function distanceToSegment(ax: number, ay: number, bx: number, by: number): number {
|
|
const dx = bx - ax;
|
|
const dy = by - ay;
|
|
const lengthSquared = dx * dx + dy * dy;
|
|
const t =
|
|
lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, -(ax * dx + ay * dy) / lengthSquared));
|
|
return Math.hypot(ax + t * dx, ay + t * dy);
|
|
}
|