Course from fixes only moves forward in time, from an anchor fix, and not for a standing contact; military contacts too

A fix that is not a second newer (a repeat, or an older position from another
feed) never gives a course, so a source switch cannot turn an aircraft
around. The course runs from an anchor fix that moves only when a course is
read, so frequent short steps add up, and a contact reported under 2 m/s
reads none. The helper moves to src/data/courseFromFixes.js and the military
records use it as well.
This commit is contained in:
Sameh Khamis committed 2026-11-15 11:19:58 -08:00
1 parent e08dcd0326
commit fc4db81d0b
9 files changed
+217 -92

No files matched your search

+10 -27
View File
@@ -1,4 +1,5 @@
import { stickyNumber, stickyText } from '../../data/aircraftMeta.js';
import { courseFromFixes } from '../../data/courseFromFixes.js';
import {
geoidSurfaceLastResortM,
pickRenderAltitudeM,
@@ -20,30 +21,6 @@ import {
} from './recordPolicy.js';
/** Source-independent aircraft metadata and bounded missing-poll retention. */
/** Metres a contact must move between fixes before its course is read from them. */
const COURSE_FROM_FIXES_MIN_M = 30;
/**
* The course from one fix to the next, in degrees from true north, or null
* when either is missing or they are less than COURSE_FROM_FIXES_MIN_M apart
* (position noise on a parked aircraft is not a course).
*/
export function courseFromFixes(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;
}
export class FlightRecords {
constructor({
geoidHeight,
@@ -315,6 +292,12 @@ export class FlightRecords {
observation.positionTimeMs > 0
? observation.positionTimeMs
: this.now();
const course = courseFromFixes(prevMeta?.courseAnchor, {
lat,
lon,
ms: fixEpochMs,
speedMps: velocity,
});
const meta = {
sourceReference: observation.reference,
observedReceiptMs: this.now(),
@@ -334,13 +317,13 @@ export class FlightRecords {
wasAirborne: prevMeta?.wasAirborne === true || !onGround,
velocity: stickyNumber(velocity, prevMeta?.velocity, 0),
// With no reported track or true heading, a moving contact faces the
// way it moved since its last fix; a still one keeps its last course.
// way its fixes went (src/data/courseFromFixes.js); else the last one.
true_track: stickyNumber(
true_track,
courseFromFixes(prevMeta?.rawLat, prevMeta?.rawLon, lat, lon) ??
prevMeta?.true_track,
course.courseDeg ?? prevMeta?.true_track,
0,
),
courseAnchor: course.anchor,
category: cat,
// An adsbdb-enriched type code outranks the coarse OpenSky category.
klass: classifyAircraft({
+42 -52
View File
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { FlightRecords, courseFromFixes } from './records.js';
import { FlightRecords } from './records.js';
const FLEET = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
@@ -320,58 +320,48 @@ test('a ground vehicle or station with no altitude stands on the ground; an airc
assert.equal(unloaded.onGround, false);
});
test('a contact reported with no course faces the way it moved since its last fix', () => {
test('a contact reported with no course faces the way its newer fixes went', () => {
const store = records();
// Taxiing south along a runway with no track or true heading reported.
store.receive(
observation({
latitude: 30.1908,
longitude: -97.6588,
courseDeg: null,
onGround: true,
speedMps: 8,
}),
view(),
const T = 1700000000000;
const taxi = (latitude, seconds, fields = {}) =>
store.receive(
observation({
latitude,
longitude: -97.6588,
courseDeg: null,
onGround: true,
speedMps: 8,
positionTimeMs: T + seconds * 1000,
...fields,
}),
view(),
);
const course = () => store.data.get('abc123').true_track;
taxi(30.1908, 0);
assert.equal(course(), 0, 'nothing known yet');
// Fixes a second apart, 8 m each: they add up until 30 m are covered.
taxi(30.19073, 1);
taxi(30.19066, 2);
assert.equal(course(), 0, 'under 30 m: still nothing');
taxi(30.19059, 3);
taxi(30.19052, 4);
assert.ok(Math.abs(course() - 180) < 0.1, 'southbound');
// An older fix from another feed, behind it: never turns it around.
taxi(30.1912, 2);
assert.ok(Math.abs(course() - 180) < 0.1, 'an older fix gives no course');
taxi(30.1901, 8);
assert.ok(Math.abs(course() - 180) < 0.1);
// Standing (reported under 2 m/s) with 40 m of position noise: kept.
taxi(30.18974, 12, { speedMps: 0.5, longitude: -97.6584 });
assert.ok(
Math.abs(course() - 180) < 0.1,
'a standing contact keeps its course',
);
assert.equal(store.data.get('abc123').true_track, 0, 'nothing known yet');
store.receive(
observation({
latitude: 30.1885,
longitude: -97.6588,
courseDeg: null,
onGround: true,
speedMps: 8,
}),
view(),
);
assert.ok(Math.abs(store.data.get('abc123').true_track - 180) < 0.1);
// Standing still (a metre of position noise): the last course is kept.
store.receive(
observation({
latitude: 30.18849,
longitude: -97.6588,
courseDeg: null,
onGround: true,
speedMps: 0,
}),
view(),
);
assert.ok(Math.abs(store.data.get('abc123').true_track - 180) < 0.1);
// A reported course always wins.
store.receive(
observation({
latitude: 30.188,
longitude: -97.6588,
courseDeg: 175,
onGround: true,
}),
view(),
);
assert.equal(store.data.get('abc123').true_track, 175);
});
test('course from fixes ignores position noise and reads east as 90', () => {
assert.equal(courseFromFixes(30, -97, 30.0001, -97), null, 'about 11 m');
assert.ok(Math.abs(courseFromFixes(0, 0, 0, 0.01) - 90) < 1e-6);
assert.equal(courseFromFixes(null, 0, 0, 0.01), null);
taxi(30.189, 16, { courseDeg: 175 });
assert.equal(course(), 175);
// A reset forgets where it was: it starts again from north.
store.reset();
taxi(30.188, 20);
assert.equal(course(), 0);
});
+15 -1
View File
@@ -1,5 +1,6 @@
import { pickRenderAltitudeM } from '../../data/renderAltitude.js';
import { stickyText, stickyNumber } from '../../data/aircraftMeta.js';
import { courseFromFixes } from '../../data/courseFromFixes.js';
import { classifyAircraft } from '../../data/aircraftClass.js';
import { aircraftEmergency } from '../../data/aircraftEmergency.js';
import {
@@ -175,6 +176,12 @@ export class MilitaryFlightRecords {
// last-known-good (bounded by the layer's eviction, which deletes the entry).
const stickyType = stickyText(type, prevMeta?.type);
const fixEpochMs = aircraft.positionTimeMs ?? observedAtMs;
const course = courseFromFixes(prevMeta?.courseAnchor, {
lat,
lon,
ms: fixEpochMs,
speedMps: aircraft.speedMps,
});
const meta = {
sourceReference: aircraft.reference,
observedReceiptMs: this.now(),
@@ -206,7 +213,14 @@ export class MilitaryFlightRecords {
onGround ? 0 : prevMeta?.speedMps,
0,
),
track: stickyNumber(aircraft.courseDeg, prevMeta?.track, 0),
// With no reported track or true heading, a moving contact faces the
// way its fixes went (src/data/courseFromFixes.js); else the last one.
track: stickyNumber(
aircraft.courseDeg,
course.courseDeg ?? prevMeta?.track,
0,
),
courseAnchor: course.anchor,
// Analyst seam (additive): sticky like the other kinematics.
verticalRateMps: stickyNumber(
verticalRateMps,
+23
View File
@@ -269,3 +269,26 @@ test('a repeated military fix time marks the contact as not updated; removal is
records.forget('aaa001');
assert.equal(records.fixRepeated.has('aaa001'), false);
});
test('a military contact reported with no course faces the way its newer fixes went', () => {
const records = store();
const T = 1700000000000;
const taxi = (latitude, seconds) =>
records.receive(
observation({
latitude,
onGround: true,
baroAltitudeM: null,
courseDeg: null,
speedMps: 8,
positionTimeMs: T + seconds * 1000,
}),
context({ observedAtMs: T + seconds * 1000 + 500 }),
);
assert.equal(taxi(39, 0).meta.track, 0);
assert.ok(Math.abs(taxi(38.9995, 5).meta.track - 180) < 0.1);
assert.ok(
Math.abs(taxi(39.002, 3).meta.track - 180) < 0.1,
'an older fix gives no course',
);
});