Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(geomap): split a track where its recording leapt, one journey each side
Elian Doran56e0c6fddfba
diff --git a/apps/client/src/services/gpx.spec.ts b/apps/client/src/services/gpx.spec.tsindex 8af5c9a719..4444059666 100644--- a/apps/client/src/services/gpx.spec.ts+++ b/apps/client/src/services/gpx.spec.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { parseGpxStats } from "./gpx";+import { parseGpxStats, readTrackLines } from "./gpx";/** One degree of longitude on the equator, which the distance assertions are stated in. */const DEGREE_M = (Math.PI / 180) * 6371000;@@ -10,6 +10,44 @@ function gpx(body: string) {<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="test">${body}</gpx>`;}+describe("readTrackLines", () => {+ function read(body: string) {+ return readTrackLines(new DOMParser().parseFromString(gpx(body), "application/xml"));+ }++ it("keeps a paused track whole, and splits one whose recording leapt", () => {+ const tracks = read(`+ <trk><name>Sunday</name>+ <trkseg><trkpt lat="0" lon="0"/><trkpt lat="0" lon="0.01"/></trkseg>+ <trkseg><trkpt lat="0" lon="0.011"/><trkpt lat="0" lon="0.02"/></trkseg>+ <trkseg><trkpt lat="0" lon="1"/><trkpt lat="0" lon="1.01"/></trkseg>+ </trk>+ `);++ // The second segment resumes ~110 m on — a pause, one journey still, its segments kept+ // apart within it. The third leaps ~110 km — a drive nobody logged — and what follows is a+ // journey of its own, keeping the track's name.+ expect(tracks).toEqual([+ { name: "Sunday", lines: [ [ [ 0, 0 ], [ 0.01, 0 ] ], [ [ 0.011, 0 ], [ 0.02, 0 ] ] ] },+ { name: "Sunday", lines: [ [ [ 1, 0 ], [ 1.01, 0 ] ] ] }+ ]);+ });++ it("reads each track and route as a journey of its own, named where the file names one", () => {+ const tracks = read(`+ <trk><name>Out</name><trkseg><trkpt lat="1" lon="1"/></trkseg></trk>+ <trk><trkseg><trkpt lat="2" lon="2"/></trkseg></trk>+ <rte><name>Planned</name><rtept lat="3" lon="3"/></rte>+ `);++ expect(tracks).toEqual([+ { name: "Out", lines: [ [ [ 1, 1 ] ] ] },+ { name: undefined, lines: [ [ [ 2, 2 ] ] ] },+ { name: "Planned", lines: [ [ [ 3, 3 ] ] ] }+ ]);+ });+});+describe("parseGpxStats", () => {it("counts the pieces and sums the distance within segments, not across the gaps", () => {const stats = parseGpxStats(gpx(`diff --git a/apps/client/src/services/gpx.ts b/apps/client/src/services/gpx.tsindex 3b82014600..ef004f0b4c 100644--- a/apps/client/src/services/gpx.ts+++ b/apps/client/src/services/gpx.ts@@ -145,9 +145,13 @@ export interface GpxTrackLines {}/**- * The lines a GPX file draws, one entry per track and one per route — a file may hold several, and- * each is a journey of its own: flagged, named and focused for itself (see GpxTrack and the pane's- * camera in DetailPane), where flattening them strung one journey's flags across another's ground.+ * The lines a GPX file draws, one entry per journey — a file may hold several, and each is flagged,+ * named and focused for itself (see GpxTrack and the pane's camera in DetailPane), where flattening+ * them strung one journey's flags across another's ground.+ *+ * A journey is a track or a route, but not always whole: a track whose recording leaps is split at+ * the leap (see {@link splitAtJumps}), since the runs either side of it read on the map as the+ * separate lines they are. The entries share the track's name.** Points that name no readable position are dropped. A line of one point is kept: it draws nothing,* but it is still where a journey began or ended, which is what the marks are placed from. A file@@ -163,9 +167,9 @@ export function readTrackLines(doc: Document): GpxTrackLines[] {? [ readCoordinates(container.querySelectorAll("rtept")) ]: [ ...container.querySelectorAll("trkseg") ].map((segment) => readCoordinates(segment.querySelectorAll("trkpt")));- const kept = lines.filter((line) => line.length > 0);- if (kept.length > 0) {- tracks.push({ name: childText(container, "name")?.trim() || undefined, lines: kept });+ const name = childText(container, "name")?.trim() || undefined;+ for (const run of splitAtJumps(lines.filter((line) => line.length > 0))) {+ tracks.push({ name, lines: run });}}@@ -179,6 +183,39 @@ export function readTrackLines(doc: Document): GpxTrackLines[] {return tracks;}+/**+ * How far a track may leap between two of its segments and still be one journey, in metres.+ *+ * A pause resumes more or less where it stopped — a traffic light, a lunch, a lost signal — and+ * two runs a stroll apart are one line to the eye as well as one journey in fact. A leap of+ * kilometres is a drive nobody logged: the runs either side of it draw as separate lines, and a+ * journey whose start flag stands on one line and end flag on another reads as a relationship+ * between two places that nothing on the map connects.+ */+const JOURNEY_JUMP_M = 1000;++/** One track's segments grouped into contiguous runs, split wherever the recording leapt further+ * than {@link JOURNEY_JUMP_M} — each run one journey's worth of lines. */+function splitAtJumps(lines: [number, number][][]): [number, number][][][] {+ const runs: [number, number][][][] = [];+ let current: [number, number][][] = [];++ for (const line of lines) {+ const previous = current[current.length - 1]?.[current[current.length - 1].length - 1];+ const [ lon, lat ] = line[0];+ if (previous && haversine({ lat: previous[1], lon: previous[0] }, { lat, lon }) > JOURNEY_JUMP_M) {+ runs.push(current);+ current = [];+ }+ current.push(line);+ }++ if (current.length > 0) {+ runs.push(current);+ }+ return runs;+}+/*** The `[lng, lat]` GeoJSON wants, for each element that carries a readable pair. One that cannot* say where it is is skipped rather than defaulted: falling back to zero put it in the Gulf ofdiff --git a/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsx b/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsxindex 267b63e8b2..fab068e3ec 100644--- a/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsx@@ -94,7 +94,8 @@ function fakeMap() {};}-/** A track of two segments, a good way apart, plus a waypoint between them. */+/** A track of two segments a short pause apart — near enough to stay one journey (see the jump+ * splitting in services/gpx) — plus a waypoint off to one side. */const TWO_SEGMENT_GPX = `<?xml version="1.0" encoding="UTF-8"?><gpx version="1.1" creator="test" xmlns="http://www.topografix.com/GPX/1/1"><wpt lat="45.9" lon="24.1"><name>A rest</name></wpt>@@ -104,7 +105,7 @@ const TWO_SEGMENT_GPX = `<?xml version="1.0" encoding="UTF-8"?><trkpt lat="45.81" lon="24.14" /></trkseg><trkseg>- <trkpt lat="45.96" lon="24.16" />+ <trkpt lat="45.813" lon="24.142" /><trkpt lat="45.89" lon="24.08" /></trkseg></trk>@@ -240,7 +241,7 @@ describe("GpxTrack", () => {type: "MultiLineString",coordinates: [[ [ 24.13, 45.79 ], [ 24.14, 45.81 ] ],- [ [ 24.16, 45.96 ], [ 24.08, 45.89 ] ]+ [ [ 24.142, 45.813 ], [ 24.08, 45.89 ] ]]});});