mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
fix(map): keep places and routes on one world, and start new installs with Journey and Collections
A right-click, click or drag on a repeated world copy reported the longitude it has on that copy (274.9 for -85.1). The place was saved there, the geocoder found nothing, and a route to a place on the real world ran across the whole map (#2976). Longitudes are now brought back onto [-180, 180] where the map hands them out (Leaflet and GL), where they are stored (places, booking endpoints, collection places, so REST, MCP, plugin RPC and importers alike) and before reverse geocoding. A migration moves the ones already stored. Leaflet draws a layer at its literal longitude only, so a trip from Los Angeles to Auckland had its pins at opposite ends of the world while the flight ran off the edge to the next copy (#2975). A trip that crosses the antimeridian is now drawn in one longitude frame, seamed in the widest stretch it leaves empty, so pins, routes, flights and every overlay lie on one copy and the map opens across the Pacific. Every other trip has no frame and draws exactly as before. The GL day fit also takes the short way. A new install now starts with Journey and Collections switched on, next to the trip addons (all but Road trip), Atlas, Vacay and the Naver import that already were. Existing installs keep their addons as they are. Closes #2975 Closes #2976
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { frameLine, frameLng, tripLngFrame } from './lngFrame';
|
||||
|
||||
const LA = -118.4;
|
||||
const AUCKLAND = 174.8;
|
||||
|
||||
describe('tripLngFrame (#2975)', () => {
|
||||
it('gives a trip that stays off the antimeridian no frame, so every layer draws as before', () => {
|
||||
expect(tripLngFrame([2.35, 13.4, -3.7, 12.5])).toBeNull();
|
||||
expect(tripLngFrame([LA, -73.9, -0.1, 139.7])).toBeNull();
|
||||
expect(tripLngFrame([])).toBeNull();
|
||||
expect(tripLngFrame([LA])).toBeNull();
|
||||
});
|
||||
|
||||
it('seams a trip across the Pacific in the middle of the stretch it leaves empty', () => {
|
||||
const frame = tripLngFrame([LA, -118.3, AUCKLAND, 174.76]);
|
||||
expect(frame).not.toBeNull();
|
||||
expect(frame!.seam).toBeCloseTo((-118.3 + 174.76) / 2, 5);
|
||||
expect(frameLng(frame, LA)).toBeCloseTo(LA + 360, 9);
|
||||
expect(frameLng(frame, AUCKLAND)).toBeCloseTo(AUCKLAND, 9);
|
||||
});
|
||||
|
||||
it('seams Fiji to Samoa, a few degrees apart across the date line', () => {
|
||||
const frame = tripLngFrame([178.4, -171.8]);
|
||||
expect(frame).not.toBeNull();
|
||||
expect(frameLng(frame, -171.8) - frameLng(frame, 178.4)).toBeCloseTo(9.8, 9);
|
||||
});
|
||||
|
||||
it('leaves a trip around the world alone when re-seaming would barely narrow it', () => {
|
||||
expect(tripLngFrame([-150, -90, -30, 30, 90, 150])).toBeNull();
|
||||
});
|
||||
|
||||
it('keeps the frame in use while it still fits, so a new place does not slide the map', () => {
|
||||
const first = tripLngFrame([LA, AUCKLAND]);
|
||||
expect(tripLngFrame([LA, AUCKLAND, 151.2], first)).toBe(first);
|
||||
});
|
||||
|
||||
it('reads a longitude from a world copy where it really is', () => {
|
||||
expect(tripLngFrame([LA + 360, AUCKLAND - 720])!.seam).toBeCloseTo(tripLngFrame([LA, AUCKLAND])!.seam, 9);
|
||||
});
|
||||
});
|
||||
|
||||
describe('frameLng', () => {
|
||||
it('draws a longitude where it is without a frame', () => {
|
||||
expect(frameLng(null, LA)).toBe(LA);
|
||||
expect(frameLng(null, 400)).toBe(400);
|
||||
});
|
||||
|
||||
it('moves any longitude into the frame, and one already in it nowhere', () => {
|
||||
const frame = { seam: 28 };
|
||||
expect(frameLng(frame, LA)).toBeCloseTo(241.6, 9);
|
||||
expect(frameLng(frame, 241.6)).toBeCloseTo(241.6, 9);
|
||||
expect(frameLng(frame, 28)).toBe(28);
|
||||
expect(frameLng(frame, 27.9)).toBeCloseTo(387.9, 9);
|
||||
});
|
||||
});
|
||||
|
||||
describe('frameLine', () => {
|
||||
it('hands the line back untouched without a frame', () => {
|
||||
const line: [number, number][] = [
|
||||
[0, 170],
|
||||
[0, -170],
|
||||
];
|
||||
expect(frameLine(null, line)).toBe(line);
|
||||
});
|
||||
|
||||
it('starts the line in the frame and keeps it one line across the antimeridian', () => {
|
||||
const drawn = frameLine({ seam: 28 }, [
|
||||
[34, LA],
|
||||
[0, -160],
|
||||
[-20, 179],
|
||||
[-37, AUCKLAND],
|
||||
]);
|
||||
expect(drawn.map(([, lng]) => Math.round(lng * 10) / 10)).toEqual([241.6, 200, 179, 174.8]);
|
||||
});
|
||||
|
||||
it('runs past the edge of the frame rather than jumping back across the world', () => {
|
||||
const drawn = frameLine({ seam: 28 }, [
|
||||
[0, 30],
|
||||
[0, 20],
|
||||
]);
|
||||
expect(drawn.map(([, lng]) => lng)).toEqual([30, 20]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
// One stretch of longitudes to draw a trip in, for a map that does not repeat its
|
||||
// layers across world copies (Leaflet) (#2975).
|
||||
//
|
||||
// Leaflet draws a marker at its literal longitude only, on one copy of the world.
|
||||
// A trip from Los Angeles (-118) to Auckland (174.8) therefore had its pins at
|
||||
// opposite ends of that copy, while the flight between them, drawn the short way
|
||||
// across the Pacific, ran off the edge to the Los Angeles of the next copy. The
|
||||
// frame moves the seam of the drawn world into the widest stretch of the planet
|
||||
// the trip leaves empty, so the whole trip, flight included, lies on one copy:
|
||||
// Los Angeles at 242, Auckland at 174.8.
|
||||
//
|
||||
// A trip that does not cross the antimeridian gets no frame at all, and every
|
||||
// layer draws exactly what it drew before.
|
||||
|
||||
import { wrapLongitude } from '@trek/shared';
|
||||
|
||||
/** The drawn world runs from `seam` to `seam + 360`. */
|
||||
export interface LngFrame {
|
||||
readonly seam: number;
|
||||
}
|
||||
|
||||
/** Re-seam only when that narrows the trip by at least this much: a world tour stays as it is. */
|
||||
const MIN_SAVING_DEG = 45;
|
||||
/** A previous seam is kept while the trip drawn with it is at most this much wider than the best. */
|
||||
const KEEP_SEAM_SLACK_DEG = 15;
|
||||
|
||||
/** Width of the trip, in degrees, when the world is drawn from `seam` eastwards. */
|
||||
function spanFrom(seam: number, sorted: number[]): number {
|
||||
let min = Infinity;
|
||||
let max = -Infinity;
|
||||
for (const lng of sorted) {
|
||||
const framed = lng < seam ? lng + 360 : lng;
|
||||
if (framed < min) min = framed;
|
||||
if (framed > max) max = framed;
|
||||
}
|
||||
return max - min;
|
||||
}
|
||||
|
||||
/**
|
||||
* The frame for these longitudes, or null when the plain world already holds the
|
||||
* trip narrowly enough. `previous` is the frame in use: it is kept while it still
|
||||
* works, so adding or filtering a place does not slide every layer sideways.
|
||||
*/
|
||||
export function tripLngFrame(lngs: readonly number[], previous: LngFrame | null = null): LngFrame | null {
|
||||
const sorted = lngs
|
||||
.filter(Number.isFinite)
|
||||
.map(wrapLongitude)
|
||||
.sort((a, b) => a - b);
|
||||
if (sorted.length < 2) return null;
|
||||
const last = sorted.length - 1;
|
||||
|
||||
// The widest stretch with no point in it. The one across the antimeridian comes first,
|
||||
// so a trip that leaves the Pacific empty keeps the plain world.
|
||||
let gapSize = sorted[0] + 360 - sorted[last];
|
||||
let gapWest = sorted[last];
|
||||
let crossesAntimeridian = false;
|
||||
for (let i = 0; i < last; i++) {
|
||||
const size = sorted[i + 1] - sorted[i];
|
||||
if (size > gapSize) {
|
||||
gapSize = size;
|
||||
gapWest = sorted[i];
|
||||
crossesAntimeridian = true;
|
||||
}
|
||||
}
|
||||
if (!crossesAntimeridian) return null;
|
||||
|
||||
const plainSpan = sorted[last] - sorted[0];
|
||||
const bestSpan = 360 - gapSize;
|
||||
if (plainSpan - bestSpan < MIN_SAVING_DEG) return null;
|
||||
|
||||
if (previous && spanFrom(previous.seam, sorted) <= bestSpan + KEEP_SEAM_SLACK_DEG) return previous;
|
||||
// The middle of the empty stretch: lines that bulge a little past a stop stay clear of it.
|
||||
return { seam: gapWest + gapSize / 2 };
|
||||
}
|
||||
|
||||
/** Where a longitude is drawn in the frame. Without a frame it is drawn where it is. */
|
||||
export function frameLng(frame: LngFrame | null, lng: number): number {
|
||||
if (!frame) return lng;
|
||||
return frame.seam + ((((lng - frame.seam) % 360) + 360) % 360);
|
||||
}
|
||||
|
||||
/**
|
||||
* A line in the frame: its first point moved into it, every next point kept within
|
||||
* half a world of the one before, so the line stays one line where it crosses the
|
||||
* antimeridian or the seam. Without a frame the line is drawn as it is.
|
||||
*/
|
||||
export function frameLine<T extends readonly [number, number]>(
|
||||
frame: LngFrame | null,
|
||||
line: readonly T[]
|
||||
): [number, number][] {
|
||||
if (!frame) return line as unknown as [number, number][];
|
||||
const out: [number, number][] = [];
|
||||
let prevRaw = 0;
|
||||
let prevDrawn = 0;
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
const [lat, lng] = line[i];
|
||||
if (i === 0) {
|
||||
prevDrawn = frameLng(frame, lng);
|
||||
} else {
|
||||
let delta = lng - prevRaw;
|
||||
delta -= 360 * Math.round(delta / 360);
|
||||
prevDrawn += delta;
|
||||
}
|
||||
prevRaw = lng;
|
||||
out.push([lat, prevDrawn]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { bboxOnRealWorld, onRealWorld } from './worldCopy';
|
||||
|
||||
describe('onRealWorld', () => {
|
||||
it('brings a right-click on the copy east of the real world back to the real point (#2976)', () => {
|
||||
const at = onRealWorld({ lat: 10.14, lng: 274.921875 });
|
||||
expect(at.lat).toBe(10.14);
|
||||
expect(at.lng).toBeCloseTo(-85.078125, 9);
|
||||
});
|
||||
|
||||
it('brings a point on the copy west of it back too', () => {
|
||||
expect(onRealWorld({ lat: 35.68, lng: -220.3 }).lng).toBeCloseTo(139.7, 9);
|
||||
});
|
||||
|
||||
it('hands a point already on the real world back as it is', () => {
|
||||
const point = { lat: 34, lng: -118.4, extra: 1 };
|
||||
expect(onRealWorld(point)).toBe(point);
|
||||
});
|
||||
|
||||
it('keeps any other field of the point', () => {
|
||||
expect(onRealWorld({ lat: 1, lng: 365, name: 'x' })).toEqual({ lat: 1, lng: 5, name: 'x' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('bboxOnRealWorld', () => {
|
||||
it('moves a box looking at a copy of Japan onto Japan', () => {
|
||||
const box = bboxOnRealWorld({ south: 34, west: -225, north: 37, east: -215 });
|
||||
expect(box.west).toBeCloseTo(135, 9);
|
||||
expect(box.east).toBeCloseTo(145, 9);
|
||||
expect([box.south, box.north]).toEqual([34, 37]);
|
||||
});
|
||||
|
||||
it('leaves a box on the real world alone', () => {
|
||||
const box = { south: 40, west: 0, north: 50, east: 20 };
|
||||
expect(bboxOnRealWorld(box)).toBe(box);
|
||||
});
|
||||
|
||||
it('keeps a box across the antimeridian across it', () => {
|
||||
expect(bboxOnRealWorld({ south: -20, west: 170, north: -10, east: 190 })).toEqual({
|
||||
south: -20,
|
||||
west: 170,
|
||||
north: -10,
|
||||
east: 190,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
// Coordinates the map hands out, brought back from a repeated world copy (#2976).
|
||||
//
|
||||
// Leaflet and MapLibre/Mapbox repeat the world sideways, and a pointer on a copy
|
||||
// reports the longitude it has on that copy: 274.9 for -85.1. Anything that
|
||||
// stores a point or asks a server about it wants the point on the real world.
|
||||
|
||||
import { wrapLongitude } from '@trek/shared';
|
||||
|
||||
/** The same point with its longitude on the real world, in [-180, 180]. */
|
||||
export function onRealWorld<T extends { lat: number; lng: number }>(point: T): T {
|
||||
const lng = wrapLongitude(point.lng);
|
||||
return lng === point.lng ? point : { ...point, lng };
|
||||
}
|
||||
|
||||
export interface Bbox {
|
||||
south: number;
|
||||
west: number;
|
||||
north: number;
|
||||
east: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* A viewport box moved whole by a multiple of 360 degrees so its centre lies on
|
||||
* the real world. A box looking at a copy of Japan then asks for Japan. A box
|
||||
* that spans the antimeridian keeps one edge past it, as it always had.
|
||||
*/
|
||||
export function bboxOnRealWorld<T extends Bbox>(bbox: T): T {
|
||||
const centre = (bbox.west + bbox.east) / 2;
|
||||
const shift = wrapLongitude(centre) - centre;
|
||||
if (shift === 0) return bbox;
|
||||
return { ...bbox, west: bbox.west + shift, east: bbox.east + shift };
|
||||
}
|
||||
Reference in New Issue
Block a user