Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): frame a track across the antimeridian, not the whole world

Elian Doran168f686dead7
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
index 8ab900a1d7..5dc365ab66 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
@@ -366,6 +366,38 @@ describe("DetailPane", () => {
} ]);
});
+ /**
+ * A track that crosses the antimeridian holds points on either side of the ±180° seam — 179.9°
+ * and -179.9° are a stroll apart on the ground. Read as raw minimum and maximum, they stand a
+ * whole world apart instead, and the fit flies out to frame the globe rather than the crossing.
+ */
+ it("frames a track that crosses the antimeridian, not the world", async () => {
+ buildNote({ id: "root", title: "root", children: [ { id: "hikeseam", title: "Across the seam", mime: GPX_MIME } ] });
+ const note = froca.notes["hikeseam"];
+ const map = fakeMap();
+ map.addSource(trackSourceId(note.noteId), {
+ type: "FeatureCollection",
+ features: [
+ { type: "Feature", properties: {}, geometry: { type: "MultiLineString", coordinates: [ [ [ 179.9, 52.0 ], [ -179.9, 52.2 ] ] ] } }
+ ]
+ });
+ await mount([ note ], map);
+
+ map.setUnderPointer([ trackFeature(note) ]);
+ await act(async () => map.click());
+ await settle();
+
+ expect(map.fitted).toHaveLength(1);
+ const { bounds } = map.fitted[0] as { bounds: [ [ number, number ], [ number, number ] ] };
+ const [ [ west, south ], [ east, north ] ] = bounds;
+ expect([ south, north ]).toEqual([ 52.0, 52.2 ]);
+ // The box is 0.2° of longitude around the seam, however the crossing is written — an east
+ // unwrapped past 180°, or a west standing east of its east, either of which fitBounds
+ // takes. Measured around the circle so the writing is not prejudged; only a box that goes
+ // the long way round fails it.
+ expect(((east - west) % 360 + 360) % 360).toBeLessThan(1);
+ });
+
/**
* A clicked flag is a place the reader chose, not a request to re-frame the file: it is stood
* clear of the pane at the zoom they were reading at, exactly as a note marker is — flying out
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
index b4434ca4ee..aa420f5cb9 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
@@ -338,17 +338,30 @@ function featureFocus(feature: MapGeoJSONFeature): PaneFocus | undefined {
* Given a `track`, only the journey under that index counts (see the `track` property in GpxTrack):
* its flags stand on its line, and another journey's ground — or a waypoint hung off in a third
* place — is exactly what focusing one track is meant to leave out of frame.
+ *
+ * Longitude is read in two frames at once, because it is a circle wearing a seam: a track across
+ * the antimeridian holds points either side of ±180° that are a stroll apart on the ground, and
+ * their raw minimum and maximum span nearly the whole world. The same longitudes are therefore
+ * also read with the seam moved to 0° — each western value pushed a turn east — and whichever
+ * frame drew the narrower box is the one answered. A crossing of one seam is whole in the other
+ * frame; only a track truly girdling half the earth stays wide in both, and then wide is the
+ * truth. The shifted answer may name longitudes past 180°, which `fitBounds` takes in stride.
*/
async function trackBounds(map: MapLibreGLMap, noteId: string, track?: number): Promise<[[number, number], [number, number]] | null> {
const data = await map.getSource<GeoJSONSource>(trackSourceId(noteId))?.getData();
if (!data || data.type !== "FeatureCollection") return null;
let west = Infinity, south = Infinity, east = -Infinity, north = -Infinity;
+ let westShifted = Infinity, eastShifted = -Infinity;
const extend = ([ lng, lat ]: number[]) => {
west = Math.min(west, lng);
south = Math.min(south, lat);
east = Math.max(east, lng);
north = Math.max(north, lat);
+
+ const shifted = lng < 0 ? lng + 360 : lng;
+ westShifted = Math.min(westShifted, shifted);
+ eastShifted = Math.max(eastShifted, shifted);
};
for (const { geometry, properties } of data.features) {
@@ -365,7 +378,11 @@ async function trackBounds(map: MapLibreGLMap, noteId: string, track?: number):
}
}
- return Number.isFinite(west) ? [ [ west, south ], [ east, north ] ] : null;
+ if (!Number.isFinite(west)) return null;
+
+ return eastShifted - westShifted < east - west
+ ? [ [ westShifted, south ], [ eastShifted, north ] ]
+ : [ [ west, south ], [ east, north ] ];
}
/**