mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
feat(tours): tours read and write through the offline core
A tourRepo replaces the direct toursApi calls: reads fall back to a new Dexie tours table, and a tour saved or edited offline is written there and queued like a place, under one temporary id for the place and its facet. The queue moves both to the real id on replay and drops both when the server refuses the create. Trip sync caches each trip's tours while the addon is on, so a tour no longer reads as a plain place offline. The tour metrics move to shared so the offline copy shows the server's numbers, and useTourPlaceIds keeps one Set per change as intended.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Great-circle distance in metres.
|
||||
*
|
||||
* The clamp keeps `asin` defined: floating point can push the argument a hair
|
||||
* over 1 for near-antipodal points, and a NaN distance silently fails every
|
||||
* comparison it feeds rather than throwing.
|
||||
*/
|
||||
export function haversineMetres(aLat: number, aLng: number, bLat: number, bLng: number): number {
|
||||
const toRad = (deg: number) => (deg * Math.PI) / 180;
|
||||
const dLat = toRad(bLat - aLat);
|
||||
const dLng = toRad(bLng - aLng);
|
||||
const h = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(aLat)) * Math.cos(toRad(bLat)) * Math.sin(dLng / 2) ** 2;
|
||||
return 2 * 6371000 * Math.asin(Math.min(1, Math.sqrt(h)));
|
||||
}
|
||||
@@ -48,6 +48,8 @@ export * from './place/place-website';
|
||||
export * from './roadtrip/roadtrip.schema';
|
||||
export * from './place/track-colors';
|
||||
export * from './tours/tours.schema';
|
||||
export * from './tours/tours.metrics';
|
||||
export * from './geo/haversine';
|
||||
export * from './collection/collection.schema';
|
||||
export * from './collection/collection-file.schema';
|
||||
export * from './trip/trip.schema';
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { computeTourMetrics } from './tours.metrics';
|
||||
import { haversineMetres } from '../geo/haversine';
|
||||
|
||||
describe('computeTourMetrics', () => {
|
||||
it('sums distance and both elevation directions over a 3D track', () => {
|
||||
const m = computeTourMetrics([
|
||||
[47, 11, 1000],
|
||||
[47.01, 11, 1100],
|
||||
[47.02, 11, 1050],
|
||||
]);
|
||||
expect(m.hasElevation).toBe(true);
|
||||
expect(m.elevationGainM).toBe(100);
|
||||
expect(m.elevationLossM).toBe(50);
|
||||
expect(m.distanceKm).toBeCloseTo((haversineMetres(47, 11, 47.01, 11) * 2) / 1000, 6);
|
||||
});
|
||||
|
||||
it('reports no elevation when one point lacks it', () => {
|
||||
const m = computeTourMetrics([
|
||||
[47, 11, 1000],
|
||||
[47.01, 11],
|
||||
]);
|
||||
expect(m).toMatchObject({ hasElevation: false, elevationGainM: null, elevationLossM: null });
|
||||
expect(m.distanceKm).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
it('yields a zero-metric result below two points', () => {
|
||||
expect(computeTourMetrics([[47, 11, 1000]])).toEqual({
|
||||
distanceKm: 0,
|
||||
elevationGainM: null,
|
||||
elevationLossM: null,
|
||||
hasElevation: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('haversineMetres', () => {
|
||||
it('stays finite for antipodal points', () => {
|
||||
expect(haversineMetres(0, 0, 0, 180)).toBeCloseTo(Math.PI * 6371000, 0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
import { haversineMetres } from '../geo/haversine';
|
||||
|
||||
/** One decoded `route_geometry` point: `[lat, lng]` or `[lat, lng, ele]`. */
|
||||
export type GeometryPoint = [number, number] | [number, number, number];
|
||||
|
||||
export interface TourMetrics {
|
||||
/** Sum of segment haversine distances, in km. */
|
||||
distanceKm: number;
|
||||
/** Sum of positive elevation deltas, in metres. Null when the track carries no elevation. */
|
||||
elevationGainM: number | null;
|
||||
/** Sum of negative elevation deltas (as a positive number), in metres. Null when no elevation. */
|
||||
elevationLossM: number | null;
|
||||
/** Whether every point in the geometry carried a 3rd (elevation) value. */
|
||||
hasElevation: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derives distance/elevation from a parsed `route_geometry` array. Never throws:
|
||||
* a track with fewer than 2 points, or with missing/mixed elevation, still
|
||||
* yields a usable (if partial) result rather than blocking import.
|
||||
*
|
||||
* The server stores these numbers on every tour; the client computes the same
|
||||
* ones for a tour saved offline, so both read them from here.
|
||||
*/
|
||||
export function computeTourMetrics(points: readonly GeometryPoint[]): TourMetrics {
|
||||
if (points.length < 2) {
|
||||
return { distanceKm: 0, elevationGainM: null, elevationLossM: null, hasElevation: false };
|
||||
}
|
||||
const hasElevation = points.every((p) => p.length === 3 && Number.isFinite(p[2]));
|
||||
|
||||
let distanceKm = 0;
|
||||
let gain = 0;
|
||||
let loss = 0;
|
||||
for (let i = 1; i < points.length; i++) {
|
||||
distanceKm += haversineMetres(points[i - 1][0], points[i - 1][1], points[i][0], points[i][1]) / 1000;
|
||||
if (hasElevation) {
|
||||
const delta = (points[i][2] as number) - (points[i - 1][2] as number);
|
||||
if (delta > 0) gain += delta;
|
||||
else loss += -delta;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
distanceKm,
|
||||
elevationGainM: hasElevation ? gain : null,
|
||||
elevationLossM: hasElevation ? loss : null,
|
||||
hasElevation,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user