feat(tours): paddle portages and locks, paddle time, wind at the start and obstacle data from OpenStreetMap

PAD-03 portages and locks on planned tours and recorded tracks, PAD-07 paddle time with portage and lock stops, PAD-08 wind forecast for the start, PAD-05A regional obstacle import from Geofabrik extracts with admin endpoints and a monthly refresh, PAD-05B first cut of the obstacle layer on Leaflet, GL and 3D. Work in progress: checklist (PAD-06), check bench (PAD-09), admin UI, translations and tests follow.
This commit is contained in:
Maurice
2026-10-16 23:44:51 +02:00
parent d705bd9dd0
commit 2c3b20bb57
117 changed files with 7043 additions and 199 deletions
+102
View File
@@ -0,0 +1,102 @@
import { describe, expect, it } from 'vitest';
import {
loadTrackMarks,
markTrackMarksSaved,
pickOnTrack,
refuseTrackPick,
startTrackPick,
trackMarkEdits,
useTrackMarkToolStore,
} from '../store/trackMarkToolStore';
import {
NO_TRACK_MARKS,
projectOntoTrack,
splitTrackByPortages,
withLock,
withLockTimes,
withoutPortage,
withPortage,
withPortageExtra,
} from './paddleTrackMarks';
/** A straight track east along 53°N, one point per kilometre (near enough). */
const TRACK: [number, number][] = [0, 1, 2, 3, 4].map((km) => [53, 13 + km / 66.9]);
describe('paddle track marks (PAD-03 on a recorded track)', () => {
it('snaps a click to its distance along the track and says how far off the line it was', () => {
const on = projectOntoTrack(TRACK, { lat: 53, lng: 13 + 1.5 / 66.9 });
expect(on?.atM).toBeGreaterThan(1450);
expect(on?.atM).toBeLessThan(1550);
expect(on?.offM).toBeLessThan(1);
const off = projectOntoTrack(TRACK, { lat: 53.01, lng: 13 + 2 / 66.9 });
expect(off?.offM).toBeGreaterThan(1000);
expect(projectOntoTrack([[53, 13]], { lat: 53, lng: 13 })).toBeNull();
});
it('adds locks in order and portages from either end, never one over another', () => {
const locked = withLock(withLock(NO_TRACK_MARKS, 3000), 1000);
expect(locked.locks.map((lock) => lock.at_m)).toEqual([1000, 3000]);
const one = withPortage(NO_TRACK_MARKS, 900, 400);
expect(one.portages).toEqual([{ from_m: 400, to_m: 900, extra_minutes: null }]);
expect(withPortage(one, 800, 1200)).toBe(one);
expect(withPortage(one, 500, 500)).toBe(one);
const two = withPortage(one, 2000, 1500);
expect(two.portages.map((p) => p.from_m)).toEqual([400, 1500]);
});
it('changes and removes what is marked', () => {
const marks = withLock(withPortage(NO_TRACK_MARKS, 100, 300), 2000);
expect(withPortageExtra(marks, 0, 20).portages[0].extra_minutes).toBe(20);
expect(withoutPortage(marks, 0).portages).toEqual([]);
expect(withLockTimes(marks, 0, { wait_minutes: 5, transit_minutes: 10 }).locks[0]).toMatchObject({
at_m: 2000,
wait_minutes: 5,
});
expect(withLockTimes(marks, 0, null).locks).toEqual([]);
});
it('cuts the track where a portage starts and ends, the parts meeting exactly', () => {
const parts = splitTrackByPortages(TRACK, [{ from_m: 1500, to_m: 2500, extra_minutes: null }]);
expect(parts.map((part) => part.land)).toEqual([false, true, false]);
expect(parts[0].coordinates[parts[0].coordinates.length - 1]).toEqual(parts[1].coordinates[0]);
expect(parts[1].coordinates[parts[1].coordinates.length - 1]).toEqual(parts[2].coordinates[0]);
expect(splitTrackByPortages(TRACK, [])).toHaveLength(1);
});
});
describe('trackMarkToolStore', () => {
it('marks a lock with one click and a portage with two, then turns the tool off', () => {
loadTrackMarks(7, null, { paddle: null, portage: null });
startTrackPick('lock');
pickOnTrack(1200);
expect(useTrackMarkToolStore.getState()).toMatchObject({ pick: null, dirty: true });
startTrackPick('portage');
pickOnTrack(400);
expect(useTrackMarkToolStore.getState().pendingFromM).toBe(400);
pickOnTrack(800);
const state = useTrackMarkToolStore.getState();
expect(state.marks.portages).toEqual([{ from_m: 400, to_m: 800, extra_minutes: null }]);
expect(state.marks.locks.map((lock) => lock.at_m)).toEqual([1200]);
});
it('says why a click placed nothing: off the line, or on a portage already there', () => {
loadTrackMarks(7, { portages: [{ from_m: 100, to_m: 500, extra_minutes: null }], locks: [] }, { paddle: null, portage: null });
startTrackPick('portage');
refuseTrackPick();
expect(useTrackMarkToolStore.getState().refused).toBe('off_line');
pickOnTrack(200);
pickOnTrack(700);
expect(useTrackMarkToolStore.getState().refused).toBe('overlap');
expect(useTrackMarkToolStore.getState().marks.portages).toHaveLength(1);
});
it('keeps edits until saved, and a save leaves the marks as the server has them', () => {
loadTrackMarks(7, null, { paddle: null, portage: null });
trackMarkEdits.setSpeeds({ portage: 3 });
expect(useTrackMarkToolStore.getState()).toMatchObject({ dirty: true, speeds: { portage: 3 } });
pickOnTrack(100);
expect(useTrackMarkToolStore.getState().marks.locks).toEqual([]);
markTrackMarksSaved(null, { paddle: 4, portage: 3 });
expect(useTrackMarkToolStore.getState()).toMatchObject({ dirty: false, marks: NO_TRACK_MARKS });
});
});
+123
View File
@@ -0,0 +1,123 @@
import type { PaddleLock, TrackMarks } from '@trek/shared';
import { haversineKm } from './geo';
/**
* PAD-03 for a recorded track: portages and locks laid onto the line as it
* was recorded, by their distance from its start. Nothing here moves a point
* of the track; a mark is only ever a distance along it.
*/
export const NO_TRACK_MARKS: TrackMarks = { portages: [], locks: [] };
/** How far from the line, in metres, a click still counts as a click on it. */
export const TRACK_PICK_TOLERANCE_M = 250;
interface Projection {
/** Metres along the track to the nearest point of it. */
atM: number;
/** How far the click was from that point, in metres. */
offM: number;
}
/**
* The nearest point of the track to a click, as its distance along the line
* and the gap to it. The nearest point is found flat within each segment,
* near enough at the length of a segment; null for a track of fewer than two points.
*/
export function projectOntoTrack(
track: readonly (readonly [number, number])[],
point: { lat: number; lng: number }
): Projection | null {
if (track.length < 2) return null;
const toRad = Math.PI / 180;
const metresPerLat = 111_195;
let along = 0;
let best: Projection | null = null;
for (let i = 1; i < track.length; i += 1) {
const [aLat, aLng] = track[i - 1];
const [bLat, bLng] = track[i];
const metresPerLng = metresPerLat * Math.cos(((aLat + bLat) / 2) * toRad);
const bx = (bLng - aLng) * metresPerLng;
const by = (bLat - aLat) * metresPerLat;
const px = (point.lng - aLng) * metresPerLng;
const py = (point.lat - aLat) * metresPerLat;
const lengthSq = bx * bx + by * by;
const t = lengthSq === 0 ? 0 : Math.max(0, Math.min(1, (px * bx + py * by) / lengthSq));
const offM = Math.hypot(px - t * bx, py - t * by);
// Along the line by great circle, as every other measure of the track is taken.
const length = haversineKm({ lat: aLat, lng: aLng }, { lat: bLat, lng: bLng }) * 1000;
if (!best || offM < best.offM) best = { atM: along + t * length, offM };
along += length;
}
return best;
}
/** The marks with a lock added at a distance along the track, its times still to enter. */
export function withLock(marks: TrackMarks, atM: number): TrackMarks {
const lock = { at_m: Math.round(atM), wait_minutes: null, transit_minutes: null };
return { ...marks, locks: [...marks.locks, lock].sort((a, b) => a.at_m - b.at_m) };
}
/**
* The marks with a portage between two distances, in either order. A portage
* that would overlap one already there is not added: the marks come back as
* they were, and the caller can say why.
*/
export function withPortage(marks: TrackMarks, aM: number, bM: number): TrackMarks {
const fromM = Math.round(Math.min(aM, bM));
const toM = Math.round(Math.max(aM, bM));
if (toM <= fromM) return marks;
if (marks.portages.some((p) => fromM < p.to_m && toM > p.from_m)) return marks;
const portage = { from_m: fromM, to_m: toM, extra_minutes: null };
return { ...marks, portages: [...marks.portages, portage].sort((a, b) => a.from_m - b.from_m) };
}
export function withoutPortage(marks: TrackMarks, index: number): TrackMarks {
return { ...marks, portages: marks.portages.filter((_, i) => i !== index) };
}
export function withPortageExtra(marks: TrackMarks, index: number, minutes: number | null): TrackMarks {
return { ...marks, portages: marks.portages.map((p, i) => (i === index ? { ...p, extra_minutes: minutes } : p)) };
}
/** The lock at this index with new times, or gone with null. */
export function withLockTimes(marks: TrackMarks, index: number, lock: PaddleLock | null): TrackMarks {
if (!lock) return { ...marks, locks: marks.locks.filter((_, i) => i !== index) };
return { ...marks, locks: marks.locks.map((l, i) => (i === index ? { ...l, ...lock } : l)) };
}
/**
* The track cut into the stretches paddled and the stretches carried, for the
* map to draw the portages in their own colour. Each cut falls on the
* interpolated point at the portage's distance, so the parts meet exactly.
*/
export function splitTrackByPortages(
track: readonly (readonly [number, number])[],
portages: TrackMarks['portages']
): Array<{ coordinates: [number, number][]; land: boolean }> {
if (track.length < 2 || portages.length === 0) return [{ coordinates: track.map(([a, b]) => [a, b]), land: false }];
const cuts = portages.flatMap((p) => [p.from_m, p.to_m]).sort((a, b) => a - b);
const parts: Array<{ coordinates: [number, number][]; land: boolean }> = [];
let current: [number, number][] = [[track[0][0], track[0][1]]];
let land = false;
let along = 0;
let cut = 0;
for (let i = 1; i < track.length; i += 1) {
const a = track[i - 1];
const b = track[i];
const length = haversineKm({ lat: a[0], lng: a[1] }, { lat: b[0], lng: b[1] }) * 1000;
while (cut < cuts.length && cuts[cut] <= along + length) {
const t = length === 0 ? 0 : (cuts[cut] - along) / length;
const at: [number, number] = [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
current.push(at);
if (current.length >= 2) parts.push({ coordinates: current, land });
current = [at];
land = !land;
cut += 1;
}
current.push([b[0], b[1]]);
along += length;
}
if (current.length >= 2) parts.push({ coordinates: current, land });
return parts;
}