mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
commit
a0d299523d
91 files changed
+1793
-396
No files matched your search
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* The course of a contact whose feed reports neither a ground track nor a
|
||||
* true heading, read from its own fixes.
|
||||
*
|
||||
* The course runs from an anchor fix to the newest fix once they are at least
|
||||
* COURSE_FROM_FIXES_MIN_M apart; the anchor then moves to the newest fix.
|
||||
* Nearer fixes leave the anchor where it is, so frequent fixes of a slow
|
||||
* aircraft still add up to a course, while position noise on a parked one
|
||||
* never makes one. A fix that does not move time on by a second (a repeat, or
|
||||
* an older position from another feed) never moves the anchor or gives a
|
||||
* course, and neither does one reported slower than
|
||||
* COURSE_FROM_FIXES_MIN_SPEED_MPS.
|
||||
*/
|
||||
import { approxDistanceKm } from './approxDistance.js';
|
||||
|
||||
/** Metres between fixes before a course is read from them. */
|
||||
export const COURSE_FROM_FIXES_MIN_M = 30;
|
||||
/** A contact reported slower than this (m/s) is standing: no course is read. */
|
||||
export const COURSE_FROM_FIXES_MIN_SPEED_MPS = 2;
|
||||
|
||||
/**
|
||||
* Initial great-circle bearing from one point to another, in degrees from
|
||||
* true north (0–360), or null when either is missing or they are less than
|
||||
* COURSE_FROM_FIXES_MIN_M apart.
|
||||
*/
|
||||
export function courseBetweenFixes(fromLat, fromLon, toLat, toLon) {
|
||||
if (![fromLat, fromLon, toLat, toLon].every(Number.isFinite)) return null;
|
||||
if (
|
||||
approxDistanceKm(fromLat, fromLon, toLat, toLon) * 1000 <
|
||||
COURSE_FROM_FIXES_MIN_M
|
||||
)
|
||||
return null;
|
||||
const φ1 = (fromLat * Math.PI) / 180;
|
||||
const φ2 = (toLat * Math.PI) / 180;
|
||||
const Δλ = ((toLon - fromLon) * Math.PI) / 180;
|
||||
const y = Math.sin(Δλ) * Math.cos(φ2);
|
||||
const x =
|
||||
Math.cos(φ1) * Math.sin(φ2) - Math.sin(φ1) * Math.cos(φ2) * Math.cos(Δλ);
|
||||
return ((Math.atan2(y, x) * 180) / Math.PI + 360) % 360;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a course from a new fix.
|
||||
* @param {{lat: number, lon: number, ms: number}|null|undefined} anchor The
|
||||
* anchor fix kept from the last call (null at first).
|
||||
* @param {{lat: number, lon: number, ms: number, speedMps?: number|null}} fix
|
||||
* @returns {{courseDeg: number|null, anchor: {lat: number, lon: number, ms: number}|null}}
|
||||
* The course, or null when none can be read, and the anchor to keep.
|
||||
*/
|
||||
export function courseFromFixes(anchor, { lat, lon, ms, speedMps = null }) {
|
||||
const keep = anchor ?? null;
|
||||
if (![lat, lon, ms].every(Number.isFinite))
|
||||
return { courseDeg: null, anchor: keep };
|
||||
if (!keep) return { courseDeg: null, anchor: { lat, lon, ms } };
|
||||
if (ms < keep.ms + 1000) return { courseDeg: null, anchor: keep };
|
||||
if (Number.isFinite(speedMps) && speedMps < COURSE_FROM_FIXES_MIN_SPEED_MPS)
|
||||
return { courseDeg: null, anchor: keep };
|
||||
const courseDeg = courseBetweenFixes(keep.lat, keep.lon, lat, lon);
|
||||
if (courseDeg === null) return { courseDeg: null, anchor: keep };
|
||||
return { courseDeg, anchor: { lat, lon, ms } };
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { courseBetweenFixes, courseFromFixes } from './courseFromFixes.js';
|
||||
|
||||
test('course between fixes reads true bearings, across the antimeridian too', () => {
|
||||
assert.ok(Math.abs(courseBetweenFixes(0, 0, 0, 0.01) - 90) < 1e-6);
|
||||
assert.ok(Math.abs(courseBetweenFixes(0, 0, -0.01, 0) - 180) < 1e-6);
|
||||
assert.ok(
|
||||
Math.abs(courseBetweenFixes(0, 179.9997, 0, -179.9997) - 90) < 1e-6,
|
||||
);
|
||||
assert.equal(courseBetweenFixes(30, -97, 30.0001, -97), null, 'about 11 m');
|
||||
assert.equal(courseBetweenFixes(null, 0, 0, 0.01), null);
|
||||
});
|
||||
|
||||
test('the anchor moves only with a course, and only forward in time', () => {
|
||||
let state = courseFromFixes(null, { lat: 0, lon: 0, ms: 0 });
|
||||
assert.equal(state.courseDeg, null);
|
||||
assert.deepEqual(state.anchor, { lat: 0, lon: 0, ms: 0 });
|
||||
const first = state.anchor;
|
||||
state = courseFromFixes(first, { lat: 0, lon: 0.0001, ms: 1000 });
|
||||
assert.equal(state.courseDeg, null, '11 m: no course');
|
||||
assert.equal(state.anchor, first, 'the anchor stays');
|
||||
state = courseFromFixes(first, { lat: 0, lon: 0.0005, ms: 500 });
|
||||
assert.equal(state.courseDeg, null, 'not a second newer');
|
||||
state = courseFromFixes(first, {
|
||||
lat: 0,
|
||||
lon: 0.0005,
|
||||
ms: 2000,
|
||||
speedMps: 1,
|
||||
});
|
||||
assert.equal(state.courseDeg, null, 'standing');
|
||||
state = courseFromFixes(first, {
|
||||
lat: 0,
|
||||
lon: 0.0005,
|
||||
ms: 2000,
|
||||
speedMps: 8,
|
||||
});
|
||||
assert.ok(Math.abs(state.courseDeg - 90) < 1e-6);
|
||||
assert.deepEqual(state.anchor, { lat: 0, lon: 0.0005, ms: 2000 });
|
||||
state = courseFromFixes(first, { lat: NaN, lon: 0, ms: 3000 });
|
||||
assert.equal(state.anchor, first);
|
||||
});
|
||||
Reference in new issue
Block a user