95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
/**
|
|
* Shared, pure geo math used by both the client accumulator and the
|
|
* server-side distance recompute. Distances are in meters; the aggregate
|
|
* is stored as an Int (rounded) to avoid float drift in the database.
|
|
*/
|
|
|
|
/** Mean earth radius in meters (WGS-84 volumetric mean). */
|
|
const EARTH_RADIUS_M = 6_371_000;
|
|
|
|
/** Fixes reporting an accuracy worse than this are dropped (anchor unchanged). */
|
|
export const MAX_ACCURACY_M = 50;
|
|
/** A candidate implying a speed above this is a teleport (anchor unchanged). */
|
|
export const MAX_SPEED_MPS = 7;
|
|
/** Segments shorter than this are treated as stationary jitter (not added). */
|
|
export const MIN_SEGMENT_M = 5;
|
|
|
|
export type GeoPoint = {
|
|
lat: number;
|
|
lon: number;
|
|
accuracy?: number | null;
|
|
recordedAt: Date | string;
|
|
};
|
|
|
|
export type GeoCoord = Pick<GeoPoint, 'lat' | 'lon'>;
|
|
|
|
/** Great-circle distance between two coordinates in meters. */
|
|
export function haversine(a: GeoCoord, b: GeoCoord): number {
|
|
const toRad = (deg: number) => (deg * Math.PI) / 180;
|
|
const dLat = toRad(b.lat - a.lat);
|
|
const dLon = toRad(b.lon - a.lon);
|
|
const lat1 = toRad(a.lat);
|
|
const lat2 = toRad(b.lat);
|
|
|
|
const h =
|
|
Math.sin(dLat / 2) ** 2 +
|
|
Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) ** 2;
|
|
|
|
return 2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(h)));
|
|
}
|
|
|
|
function toTime(recordedAt: Date | string): number {
|
|
return new Date(recordedAt).getTime();
|
|
}
|
|
|
|
/**
|
|
* Derive the walked distance (Int meters) from a set of raw fixes.
|
|
*
|
|
* Points are deduped (by `recordedAt` + coords) and sorted by `recordedAt`,
|
|
* then walked with an "anchor". For each candidate:
|
|
* - accuracy worse than {@link MAX_ACCURACY_M} → dropped, anchor unchanged;
|
|
* - implied speed above {@link MAX_SPEED_MPS} → teleport, anchor unchanged;
|
|
* - segment shorter than {@link MIN_SEGMENT_M} → jitter, anchor unchanged;
|
|
* - otherwise the segment is added and the anchor advances.
|
|
*/
|
|
export function computeTrackDistance(points: GeoPoint[]): number {
|
|
const seen = new Set<string>();
|
|
const normalized: (GeoPoint & { time: number })[] = [];
|
|
|
|
for (const p of points) {
|
|
const time = toTime(p.recordedAt);
|
|
if (Number.isNaN(time)) continue;
|
|
const key = `${time}|${p.lat}|${p.lon}`;
|
|
if (seen.has(key)) continue;
|
|
seen.add(key);
|
|
normalized.push({ ...p, time });
|
|
}
|
|
|
|
normalized.sort((a, b) => a.time - b.time);
|
|
|
|
let total = 0;
|
|
let anchor: { lat: number; lon: number; time: number } | null = null;
|
|
|
|
for (const p of normalized) {
|
|
if (p.accuracy != null && p.accuracy > MAX_ACCURACY_M) continue;
|
|
|
|
if (anchor === null) {
|
|
anchor = { lat: p.lat, lon: p.lon, time: p.time };
|
|
continue;
|
|
}
|
|
|
|
const dtSeconds = (p.time - anchor.time) / 1000;
|
|
// Non-positive time delta is ambiguous (duplicate timestamps) → skip.
|
|
if (dtSeconds <= 0) continue;
|
|
|
|
const distance = haversine(anchor, p);
|
|
if (distance / dtSeconds > MAX_SPEED_MPS) continue; // teleport
|
|
if (distance < MIN_SEGMENT_M) continue; // stationary jitter
|
|
|
|
total += distance;
|
|
anchor = { lat: p.lat, lon: p.lon, time: p.time };
|
|
}
|
|
|
|
return Math.round(total);
|
|
}
|