diff --git a/changelog.d/course-from-fixes.md b/changelog.d/course-from-fixes.md new file mode 100644 index 000000000..43898dc00 --- /dev/null +++ b/changelog.d/course-from-fixes.md @@ -0,0 +1,3 @@ +### Fixed + +- An aircraft whose feed sends neither a ground track nor a true heading now faces the way it moved since its last position, instead of north, so a taxiing aircraft no longer appears to move sideways or backwards. diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index a935ac0dd..4228e9713 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -1,5 +1,11 @@ # God's Eye View Current State +An aircraft faces its reported ground track, else its true heading (readsb +`true_heading`). With neither, it faces the way it moved since its last fix +when that is at least 30 m (`courseFromFixes`, `src/layers/flights/records.js`); +nearer fixes are position noise and keep its last course, and an aircraft with +no course at all faces north. + Docker Compose mounts the checkout with the shared SELinux `:z` label so containers can read it on enforcing SELinux hosts. The separate image-owned `node_modules` volume and loopback-only port publishing remain unchanged. diff --git a/src/layers/flights/records.js b/src/layers/flights/records.js index 02c50f3d8..84394f7cf 100644 --- a/src/layers/flights/records.js +++ b/src/layers/flights/records.js @@ -20,6 +20,30 @@ 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, @@ -309,7 +333,14 @@ export class FlightRecords { // applies to contacts that actually flew this session. wasAirborne: prevMeta?.wasAirborne === true || !onGround, velocity: stickyNumber(velocity, prevMeta?.velocity, 0), - true_track: stickyNumber(true_track, prevMeta?.true_track, 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. + true_track: stickyNumber( + true_track, + courseFromFixes(prevMeta?.rawLat, prevMeta?.rawLon, lat, lon) ?? + prevMeta?.true_track, + 0, + ), category: cat, // An adsbdb-enriched type code outranks the coarse OpenSky category. klass: classifyAircraft({ diff --git a/src/layers/flights/records.test.mjs b/src/layers/flights/records.test.mjs index cafe55cbf..fe3c789f4 100644 --- a/src/layers/flights/records.test.mjs +++ b/src/layers/flights/records.test.mjs @@ -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 } from './records.js'; +import { FlightRecords, courseFromFixes } from './records.js'; const FLEET = path.resolve( path.dirname(fileURLToPath(import.meta.url)), @@ -319,3 +319,59 @@ test('a ground vehicle or station with no altitude stands on the ground; an airc ).meta; assert.equal(unloaded.onGround, false); }); + +test('a contact reported with no course faces the way it moved since its last fix', () => { + 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(), + ); + 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); +});