Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): let a waypoint's flag say the name the file gives it

Elian Doran1c9ddaea0141
diff --git a/apps/client/src/services/gpx.spec.ts b/apps/client/src/services/gpx.spec.ts
index 46e80abc0e..8af5c9a719 100644
--- a/apps/client/src/services/gpx.spec.ts
+++ b/apps/client/src/services/gpx.spec.ts
@@ -85,18 +85,32 @@ describe("parseGpxStats", () => {
expect(stats?.elevation).toMatchObject({ gain: 20, loss: 0, min: 100, max: 510 });
});
- it("reads the travel time from the points' own timestamps, not from extensions", () => {
+ it("reads the travel time from track points alone — not from extensions, not from routes", () => {
const stats = parseGpxStats(gpx(`<trk><trkseg>
<trkpt lat="0" lon="0"><time>2024-06-01T10:00:00Z</time></trkpt>
<trkpt lat="0" lon="0.01"><extensions><time>1999-01-01T00:00:00Z</time></extensions></trkpt>
<trkpt lat="0" lon="0.02"><time>2024-06-01T11:30:00Z</time></trkpt>
- </trkseg></trk>`));
-
+ </trkseg></trk>
+ <rte>
+ <rtept lat="0" lon="1"><time>2001-06-02T03:26:55Z</time></rtept>
+ <rtept lat="0" lon="1.01"><time>2001-11-28T21:05:28Z</time></rtept>
+ </rte>`));
+
+ // The route's timestamps say when its points were authored, months apart (as in the GPX
+ // spec's own fells_loop sample) — counting them would report a journey of half a year.
expect(stats?.time?.start.toISOString()).toBe("2024-06-01T10:00:00.000Z");
expect(stats?.time?.end.toISOString()).toBe("2024-06-01T11:30:00.000Z");
expect(stats?.time?.duration).toBe(90 * 60 * 1000);
});
+ it("reports no time at all for a routes-only file, however timestamped", () => {
+ const stats = parseGpxStats(gpx(`<rte>
+ <rtept lat="0" lon="0"><time>2001-06-02T03:26:55Z</time></rtept>
+ <rtept lat="0" lon="0.01"><time>2001-11-28T21:05:28Z</time></rtept>
+ </rte>`));
+ expect(stats?.time).toBeUndefined();
+ });
+
it("prefers the metadata name and description over the first track's", () => {
const stats = parseGpxStats(gpx(`
<metadata><name>The file</name><desc>All of it</desc></metadata>
@@ -105,6 +119,15 @@ describe("parseGpxStats", () => {
expect(stats).toMatchObject({ name: "The file", description: "All of it" });
});
+ it("reads the name a GPX 1.0 file keeps directly on the root, having no metadata", () => {
+ const stats = parseGpxStats(`<?xml version="1.0"?>
+<gpx version="1.0" xmlns="http://www.topografix.com/GPX/1/0">
+ <name>Fells loop</name><desc>A ramble</desc>
+ <wpt lat="42.4" lon="-71.1"><name>Crossing</name></wpt>
+</gpx>`);
+ expect(stats).toMatchObject({ name: "Fells loop", description: "A ramble", waypointCount: 1 });
+ });
+
it("reads points sitting outside any segment or route, as the map does", () => {
const stats = parseGpxStats(gpx(`<trkpt lat="0" lon="0"/><trkpt lat="0" lon="0.01"/>`));
expect(stats?.pointCount).toBe(2);
diff --git a/apps/client/src/services/gpx.ts b/apps/client/src/services/gpx.ts
index 6d39eec608..83261ce212 100644
--- a/apps/client/src/services/gpx.ts
+++ b/apps/client/src/services/gpx.ts
@@ -38,7 +38,10 @@ export interface GpxStats {
/** The track flattened to elevation-over-distance, decimated for drawing. */
profile: GpxElevationSample[];
};
- /** Absent when no point carries a timestamp. */
+ /** Absent when no track point carries a timestamp. Read from track points alone: a track was
+ * recorded, so its times are a journey — a route was planned, and the times its points carry
+ * (when tools write any, as the GPX spec's own sample does) say when each point was authored,
+ * which totalled up would report a "journey" of months. */
time?: {
start: Date;
end: Date;
@@ -161,7 +164,7 @@ function readSegments(doc: Document): GpxPoint[][] {
const segments: GpxPoint[][] = [];
for (const container of doc.querySelectorAll("trkseg, rte")) {
- const points = readPoints(container.querySelectorAll("trkpt, rtept"));
+ const points = readPoints(container.querySelectorAll("trkpt, rtept"), { withTime: container.localName !== "rte" });
if (points.length > 0) {
segments.push(points);
}
@@ -177,8 +180,12 @@ function readSegments(doc: Document): GpxPoint[][] {
return segments;
}
-/** Each point that carries a readable position; one that cannot say where it is is skipped. */
-function readPoints(elements: Iterable<Element>): GpxPoint[] {
+/**
+ * Each point that carries a readable position; one that cannot say where it is is skipped.
+ * `withTime: false` leaves the timestamps unread — what a route's points carry is authoring
+ * time, not travel time (see the note on {@link GpxStats.time}).
+ */
+function readPoints(elements: Iterable<Element>, { withTime = true } = {}): GpxPoint[] {
const points: GpxPoint[] = [];
for (const element of elements) {
@@ -193,9 +200,11 @@ function readPoints(elements: Iterable<Element>): GpxPoint[] {
point.elevation = elevation;
}
- const time = Date.parse(childText(element, "time") ?? "");
- if (Number.isFinite(time)) {
- point.time = time;
+ if (withTime) {
+ const time = Date.parse(childText(element, "time") ?? "");
+ if (Number.isFinite(time)) {
+ point.time = time;
+ }
}
points.push(point);
@@ -208,9 +217,10 @@ function countPositioned(elements: Iterable<Element>): number {
return readPoints(elements).length;
}
-/** What the file calls the given field: its metadata's, else its first track's or route's. */
+/** What the file calls the given field: its metadata's (kept directly on the root in GPX 1.0,
+ * which had no `<metadata>`), else its first track's or route's. */
function readMetadataOrFirst(root: Element, name: string): string | undefined {
- for (const container of [ childNamed(root, "metadata"), childNamed(root, "trk"), childNamed(root, "rte") ]) {
+ for (const container of [ childNamed(root, "metadata"), root, childNamed(root, "trk"), childNamed(root, "rte") ]) {
const text = container && childText(container, name)?.trim();
if (text) {
return text;
@@ -222,9 +232,10 @@ function readMetadataOrFirst(root: Element, name: string): string | undefined {
/**
* A direct child's text, matched by local name so a namespaced document reads the same as a bare
* one — and only a direct child, so a `<time>` buried in a point's `<extensions>` (heart rate and
- * the like travel there) is not mistaken for the point's own.
+ * the like travel there) is not mistaken for the point's own. Exported for the map, which reads a
+ * waypoint's `<name>` the same way to title its mark (see GpxTrack).
*/
-function childText(element: Element, name: string): string | undefined {
+export function childText(element: Element, name: string): string | undefined {
return childNamed(element, name)?.textContent ?? undefined;
}
diff --git a/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsx b/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsx
index 4a1b465c72..df31485594 100644
--- a/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/GpxTrack.spec.tsx
@@ -258,12 +258,16 @@ describe("GpxTrack", () => {
]);
});
- it("marks the waypoints", () => {
+ it("marks the waypoints, each saying its own name", () => {
map.loadStyle();
renderTrack({ styleLoaded: true });
- expect(marks().filter((mark) => mark.properties?.icon === WAYPOINT_IMAGE).map((mark) => mark.geometry))
+ const waypoints = marks().filter((mark) => mark.properties?.icon === WAYPOINT_IMAGE);
+ expect(waypoints.map((mark) => mark.geometry))
.toEqual([ { type: "Point", coordinates: [ 24.1, 45.9 ] } ]);
+ // The waypoint's own <name>, not the note's: a file full of named crossings would
+ // otherwise put up pins that answer only to a click.
+ expect(waypoints[0].properties).toMatchObject({ id: NOTE_ID, name: "A rest" });
});
it("marks the waypoints of a file that draws no line at all", async () => {
diff --git a/apps/client/src/widgets/collections/geomap/GpxTrack.tsx b/apps/client/src/widgets/collections/geomap/GpxTrack.tsx
index be8489a219..a9cc2192d9 100644
--- a/apps/client/src/widgets/collections/geomap/GpxTrack.tsx
+++ b/apps/client/src/widgets/collections/geomap/GpxTrack.tsx
@@ -1,7 +1,7 @@
import type { FilterSpecification, Map as MapLibreGLMap } from "maplibre-gl";
import { useContext, useEffect } from "preact/hooks";
-import { GPX_MIME } from "../../../services/gpx";
+import { childText, GPX_MIME } from "../../../services/gpx";
import { MapStyleLoaded, ParentMap } from "./map";
import { buildMarkerImage, LABEL_LAYOUT, LABEL_PAINT, MARKER_SHADOW_PADDING, markerImageId } from "./Markers";
@@ -321,7 +321,9 @@ export function trackHitLayers(map: MapLibreGLMap) {
* The marks a track is flagged with, as features of the same source its line is drawn from: where
* the whole track begins and where it ends — not one pair per segment, since a track broken into
* segments is still one journey, and a flag at every pause would be a flag at every traffic light —
- * and one at every waypoint. Only the start says the note's name; the pin says the rest.
+ * and one at every waypoint. The start says the note's name and a waypoint its own `<name>` — a
+ * file like the GPX spec's own sample carries dozens of named crossings, and a pin that will not
+ * say which one it is answers only to a click; the end's pin says the rest.
*/
function readMarks(gpxDoc: Document, lines: [number, number][][], { noteId, title, pinColor, iconClass }: {
noteId: string; title: string; pinColor: string; iconClass: string;
@@ -347,8 +349,11 @@ function readMarks(gpxDoc: Document, lines: [number, number][][], { noteId, titl
}
}
- for (const waypoint of readPoints(gpxDoc.querySelectorAll("wpt"))) {
- addMark(waypoint, WAYPOINT_ICON);
+ for (const waypoint of gpxDoc.querySelectorAll("wpt")) {
+ const [ coordinates ] = readPoints([ waypoint ]);
+ if (coordinates) {
+ addMark(coordinates, WAYPOINT_ICON, childText(waypoint, "name")?.trim() ?? "");
+ }
}
return marks;