refactor(shared): share the thinning of a recorded line

Ramer-Douglas-Peucker and the even-stride cap move from the Dawarich
helpers into @trek/shared as thinRecordedLine and capLinePoints, so a
second source of recorded days thins its lines the same way. The Dawarich
helpers keep their names for them.
This commit is contained in:
Maurice
2026-11-30 14:32:02 +01:00
parent 535503b73d
commit cd20a0371f
4 changed files with 158 additions and 70 deletions
+66
View File
@@ -0,0 +1,66 @@
// RECLINE-001 to RECLINE-004: thinning a recorded line, shared by the Dawarich overlay and the kept days.
import { capLinePoints, thinRecordedLine } from './recorded-line';
import { describe, expect, it } from 'vitest';
describe('thinRecordedLine', () => {
it('RECLINE-001: leaves a line of two points or fewer alone', () => {
expect(thinRecordedLine([], 0.00005)).toEqual([]);
expect(thinRecordedLine([[1, 2]], 0.00005)).toEqual([[1, 2]]);
expect(
thinRecordedLine(
[
[1, 2],
[3, 4],
],
0.00005,
),
).toEqual([
[1, 2],
[3, 4],
]);
});
it('RECLINE-002: drops the points of a straight run and keeps a corner', () => {
const straight: Array<[number, number]> = Array.from({ length: 50 }, (_, i) => [48 + i * 0.001, 11]);
expect(thinRecordedLine(straight, 0.00005)).toEqual([
[48, 11],
[48 + 49 * 0.001, 11],
]);
const corner: Array<[number, number]> = [
[48, 11],
[48.001, 11],
[48.002, 11],
[48.002, 11.001],
[48.002, 11.002],
];
expect(thinRecordedLine(corner, 0.00005)).toEqual([
[48, 11],
[48.002, 11],
[48.002, 11.002],
]);
});
it('RECLINE-003: thins a line that starts and ends on the same spot', () => {
const loop: Array<[number, number]> = [
[48, 11],
[48.001, 11],
[48.001, 11.001],
[48, 11],
];
expect(thinRecordedLine(loop, 0.00005)).toHaveLength(4);
});
});
describe('capLinePoints', () => {
it('RECLINE-004: keeps an even stride with the first and the last point', () => {
const line: Array<[number, number]> = Array.from({ length: 101 }, (_, i) => [i, 0]);
const capped = capLinePoints(line, 11);
expect(capped).toHaveLength(11);
expect(capped[0]).toEqual([0, 0]);
expect(capped[10]).toEqual([100, 0]);
expect(capped[5]).toEqual([50, 0]);
expect(capLinePoints(line, 200)).toBe(line);
expect(capLinePoints(line, 1)).toBe(line);
});
});
+85
View File
@@ -0,0 +1,85 @@
/**
* Thinning a recorded line, the same way for every source that records one:
* the Dawarich overlay and the days a journey keeps from Dawarich or from a
* GPX file. A day of one-second tracking is tens of thousands of points; what
* a map needs is its corners.
*/
/** `[lat, lng]`, as TREK stores every track. */
type LatLng = [number, number];
/** Perpendicular distance in degrees, longitude scaled so the metric is not latitude-skewed. */
function perpendicularDistance(point: LatLng, start: LatLng, end: LatLng): number {
const scale = Math.cos((point[0] * Math.PI) / 180) || 1;
const px = point[1] * scale;
const py = point[0];
const sx = start[1] * scale;
const sy = start[0];
const ex = end[1] * scale;
const ey = end[0];
const dx = ex - sx;
const dy = ey - sy;
if (dx === 0 && dy === 0) return Math.hypot(px - sx, py - sy);
const t = Math.max(0, Math.min(1, ((px - sx) * dx + (py - sy) * dy) / (dx * dx + dy * dy)));
return Math.hypot(px - (sx + t * dx), py - (sy + t * dy));
}
/**
* Ramer–Douglas–Peucker, iterative, with `epsilon` in degrees (~5e-5 is
* roughly five metres).
*
* Recursion is the textbook shape and the wrong one here: a day of one-second
* tracking is tens of thousands of points and a nearly straight motorway run is
* its worst case, which is exactly where the recursion depth peaks.
*/
export function thinRecordedLine(points: LatLng[], epsilon: number): LatLng[] {
if (points.length <= 2) return points;
const keep = new Uint8Array(points.length);
keep[0] = 1;
keep[points.length - 1] = 1;
const stack: Array<[number, number]> = [[0, points.length - 1]];
for (let range = stack.pop(); range; range = stack.pop()) {
const [first, last] = range;
if (last <= first + 1) continue;
const start = points[first] as LatLng;
const end = points[last] as LatLng;
let maxDist = 0;
let index = first;
for (let i = first + 1; i < last; i++) {
const dist = perpendicularDistance(points[i] as LatLng, start, end);
if (dist > maxDist) {
maxDist = dist;
index = i;
}
}
if (maxDist > epsilon) {
keep[index] = 1;
stack.push([first, index], [index, last]);
}
}
return points.filter((_, i) => keep[i] === 1);
}
/**
* Cap a polyline at `max` points by keeping an even stride.
*
* A backstop behind `thinRecordedLine`, not a replacement: thinning keeps
* corners and drops straights, which is what a route should look like, but a
* sufficiently jittery recording survives it. The first and last point are
* always kept so the line still starts and ends where the day did.
*/
export function capLinePoints(points: LatLng[], max: number): LatLng[] {
if (points.length <= max || max < 2) return points;
const stride = (points.length - 1) / (max - 1);
const out: LatLng[] = [];
for (let i = 0; i < max - 1; i++) out.push(points[Math.round(i * stride)] as LatLng);
out.push(points[points.length - 1] as LatLng);
return out;
}
+1
View File
@@ -121,6 +121,7 @@ export * from './wanderer/wanderer-watch.schema';
export * from './wanderer/wanderer-mcp.schema';
export * from './geo/haversine';
export * from './geo/longitude';
export * from './geo/recorded-line';
export * from './collection/collection.schema';
export * from './collection/collection-file.schema';
export * from './places-library/places-library.schema';