From 7e45d4d989823262cb088164a74dd335424da49a Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Fri, 13 Nov 2026 12:53:18 -0800 Subject: [PATCH] fix(flights): a trail does not join its track history across a landing or take-off that was not seen --- CHANGELOG.md | 4 + src/data/trailContinuity.js | 94 ++++++++++++++++++--- src/data/trailGroundJoin.test.mjs | 105 ++++++++++++++++++++++++ src/layers/flights/snapshotRenderer.js | 3 + src/layers/flights/tracking.js | 9 +- src/layers/military/snapshotRenderer.js | 3 + src/layers/military/tracking.js | 9 +- 7 files changed, 215 insertions(+), 12 deletions(-) create mode 100644 src/data/trailGroundJoin.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fe6c88be..1252cac41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +- A tracked aircraft's trail no longer draws a straight line from its track + history across a landing or take-off that was not seen: a parked or + taxiing aircraft whose history ends in the air (often its previous flight) + starts its trail on the ground (samehkhamis). - Aircraft from adsb.lol that report no ground track (parked or taxiing slowly) now face their reported true heading instead of north, so parked models line up with their gates (samehkhamis). diff --git a/src/data/trailContinuity.js b/src/data/trailContinuity.js index 396611305..3f3c50165 100644 --- a/src/data/trailContinuity.js +++ b/src/data/trailContinuity.js @@ -12,7 +12,10 @@ * longer than 48 minutes (a coverage hole or a separate flight), when the * implied great-circle speed is over 1,500 kt, or when either point has no * reported altitude. A parked aircraft is the exception: a pair under three - * hours apart within 0.1 nm stays one path. + * hours apart within 0.1 nm stays one path. A history backfill that ends in + * the air does not join an aircraft now on the ground (or the reverse) when + * more than five minutes lie between them: the landing or take-off was not + * seen, and is often a different flight. * * Civil and military flight tracking share these rules. */ @@ -40,6 +43,13 @@ export const TRAIL_STATIONARY_HOLD_MAX_GAP_MS = 3 * 60 * 60_000; /** @constant {number} How close (nautical miles) two points must be to count as parked. */ export const TRAIL_STATIONARY_HOLD_MAX_DISTANCE_NM = 0.1; +/** + * @constant {number} An airborne point and a ground point more than this far + * apart in time do not join: the landing or take-off between them was not + * seen. A last waypoint on short final is a few minutes from the runway. + */ +export const TRAIL_UNSEEN_TRANSITION_MAX_GAP_MS = 5 * 60_000; + /** @constant {number} The head never joins a body point farther than this from the aircraft. */ export const TRAIL_HEAD_JOIN_MAX_M = 150_000; @@ -139,21 +149,76 @@ export function trailBackfillFixes(waypoints) { return { fixes, breakAfter: skipped }; } +/** + * Whether the line from an airborne point to a ground point (in either + * order) skips a landing or take-off that was not seen. + * @param {boolean} prevOnGround - Whether the earlier point is on the ground. + * @param {boolean} nextOnGround - Whether the later point is on the ground. + * @param {number} deltaMs - Later point's time minus the earlier one's. + * @returns {boolean} + */ +export function trailGroundTransitionBreaks( + prevOnGround, + nextOnGround, + deltaMs, +) { + return ( + prevOnGround !== nextOnGround && + !(deltaMs <= TRAIL_UNSEEN_TRANSITION_MAX_GAP_MS) + ); +} + /** * Put backfill fixes ahead of the live trail. The join follows the same * rule, so a distant history tail never connects to the first live fix. - * Past `maxPoints` the oldest recorded points go first, then the oldest - * reports. + * A backfill whose last waypoint is in the air while the first live report + * is on the ground (or the reverse), more than five minutes before it, does + * not join it either. A live report that does not say uses the aircraft's + * state now; with no live fix to start after it, the backfill is checked + * against now and left out, so the head never draws that line. Past `maxPoints` the oldest + * recorded points go first, then the oldest reports. * @param {{fixes: object[], breakAfter: boolean}} backfill * @param {object[]} live - Live fixes; the first one's break is recomputed. * @param {number} maxPoints - Point limit across all segments. + * @param {object} [ground] - Ground state across the join. + * @param {boolean} [ground.backfillEndsOnGround] - The last waypoint's. + * @param {boolean} [ground.onGroundNow] - The aircraft's now, for a live + * report that does not carry its own `onGround`. + * @param {number} [ground.nowMs] - Time the aircraft is at now. * @returns {object[]} The joined trail. */ -export function joinTrailBackfill({ fixes, breakAfter }, live, maxPoints) { +export function joinTrailBackfill( + { fixes, breakAfter }, + live, + maxPoints, + ground = null, +) { if (!fixes.length) return live; - if (live.length) - live[0].breakBefore = - breakAfter || trailFixBreaks(fixes[fixes.length - 1], live[0]); + const last = fixes[fixes.length - 1]; + const unseen = (laterMs, laterOnGround) => + ground !== null && + typeof ground.backfillEndsOnGround === 'boolean' && + typeof laterOnGround === 'boolean' && + trailGroundTransitionBreaks( + ground.backfillEndsOnGround, + laterOnGround, + laterMs - last.timeMs, + ); + if (!live.length) + return Number.isFinite(ground?.nowMs) && + unseen(ground.nowMs, ground.onGroundNow) + ? live + : keepTrailLimit(fixes, maxPoints); + const first = live[0]; + live[0].breakBefore = + breakAfter || + trailFixBreaks(last, first) || + unseen( + first.timeMs, + typeof first.onGround === 'boolean' + ? first.onGround + : ground?.onGroundNow, + ); return keepTrailLimit(fixes.concat(live), maxPoints); } @@ -499,9 +564,10 @@ function reportFrom(fixes, i) { * @param {{x: number, y: number, z: number}} position - Owned position. * @param {number} timeMs - The report's epoch ms. * @param {number} maxPoints - Point limit across all segments. - * @param {{nullAltitude?: boolean, displayMs?: number, settleMs?: number}} [options] + * @param {{nullAltitude?: boolean, displayMs?: number, settleMs?: number, onGround?: boolean}} [options] * `displayMs`: the time the aircraft is drawn at as the report arrives; - * `settleMs`: how long the icon takes to ease onto a correction. + * `settleMs`: how long the icon takes to ease onto a correction; + * `onGround`: whether the report was on the ground, when known. * @returns {object[]} `fixes`. */ export function insertTrailFix( @@ -509,7 +575,12 @@ export function insertTrailFix( position, timeMs, maxPoints, - { nullAltitude = false, displayMs = NaN, settleMs = 0 } = {}, + { + nullAltitude = false, + displayMs = NaN, + settleMs = 0, + onGround = undefined, + } = {}, ) { const entry = { position: nullAltitude ? null : position, @@ -518,6 +589,9 @@ export function insertTrailFix( drawn: false, breakBefore: false, }; + // Whether the report was on the ground, when known: a history backfill + // joins it only across a landing or take-off it saw. + if (typeof onGround === 'boolean') entry.onGround = onGround; let i = insertionIndex(fixes, timeMs); // The guess drawn after a report the display already passed. fixes.splice(i, reportFrom(fixes, i) - i); diff --git a/src/data/trailGroundJoin.test.mjs b/src/data/trailGroundJoin.test.mjs new file mode 100644 index 000000000..1f3f30639 --- /dev/null +++ b/src/data/trailGroundJoin.test.mjs @@ -0,0 +1,105 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import * as Cesium from 'cesium'; +import { + joinTrailBackfill, + trailGroundTransitionBreaks, +} from './trailContinuity.js'; + +// A history backfill that ends in the air does not join an aircraft now on +// the ground (or the reverse) across a landing or take-off nobody saw: a +// parked plane's track history is often its last flight. + +const MIN = 60_000; +const LIMIT = 2_000; +const enu = Cesium.Transforms.eastNorthUpToFixedFrame( + Cesium.Cartesian3.fromDegrees(-122.21, 37.7, 0), +); +const at = (e, n, u) => + Cesium.Matrix4.multiplyByPoint( + enu, + new Cesium.Cartesian3(e, n, u), + new Cesium.Cartesian3(), + ); +const fix = (position, timeMs) => ({ + position, + timeMs, + nullAltitude: false, + drawn: false, + breakBefore: false, +}); + +/** The last flight's final waypoint: 5.8 km up, 35 minutes before the taxi. */ +const lastFlight = () => ({ + fixes: [fix(at(0, 900, 5_800), 0)], + breakAfter: false, +}); +const taxi = () => [fix(at(0, 0, 2), 35 * MIN), fix(at(40, -20, 2), 36 * MIN)]; + +test('an unseen landing or take-off breaks the join', () => { + assert.equal(trailGroundTransitionBreaks(false, true, 35 * MIN), true); + assert.equal(trailGroundTransitionBreaks(true, false, 35 * MIN), true); + assert.equal(trailGroundTransitionBreaks(false, true, 4 * MIN), false); + assert.equal(trailGroundTransitionBreaks(false, false, 35 * MIN), false); + assert.equal(trailGroundTransitionBreaks(true, true, 35 * MIN), false); +}); + +test('an airborne history does not join a taxiing aircraft', () => { + const joined = joinTrailBackfill(lastFlight(), taxi(), LIMIT, { + backfillEndsOnGround: false, + onGroundNow: true, + nowMs: 36 * MIN, + }); + assert.equal(joined.length, 3); + assert.equal(joined[1].breakBefore, true); +}); + +test('without the ground state the join is unchanged', () => { + const joined = joinTrailBackfill(lastFlight(), taxi(), LIMIT); + assert.equal(joined[1].breakBefore, false); +}); + +test('a landing seen in the history still joins', () => { + const history = { + fixes: [fix(at(0, 900, 300), 31 * MIN)], + breakAfter: false, + }; + const joined = joinTrailBackfill(history, taxi(), LIMIT, { + backfillEndsOnGround: false, + onGroundNow: true, + nowMs: 36 * MIN, + }); + assert.equal(joined[1].breakBefore, false); +}); + +test('with no live fix, an unseen transition leaves the history out', () => { + const ground = { + backfillEndsOnGround: false, + onGroundNow: true, + nowMs: 35 * MIN, + }; + assert.deepEqual(joinTrailBackfill(lastFlight(), [], LIMIT, ground), []); + assert.equal( + joinTrailBackfill(lastFlight(), [], LIMIT, { + ...ground, + onGroundNow: false, + }).length, + 1, + ); +}); + +test('the first live report decides, not the aircraft now', () => { + // Held short with no history waypoints, then took off inside the live + // window: the history and the first live report are both on the ground. + const held = { + fixes: [fix(at(0, 0, 2), 0)], + breakAfter: false, + }; + const live = taxi().map((f) => ({ ...f, onGround: true })); + const joined = joinTrailBackfill(held, live, LIMIT, { + backfillEndsOnGround: true, + onGroundNow: false, + nowMs: 36 * MIN, + }); + assert.equal(joined[1].breakBefore, false); +}); diff --git a/src/layers/flights/snapshotRenderer.js b/src/layers/flights/snapshotRenderer.js index d8a4ade98..65fd8c667 100644 --- a/src/layers/flights/snapshotRenderer.js +++ b/src/layers/flights/snapshotRenderer.js @@ -113,6 +113,9 @@ export function createFlightSnapshotRenderer({ // Drawn at an invented height: a trail seeded from history keeps no // position for it. inventedHeight: meta.renderAltitudeInvented === true, + // Whether this report was on the ground: a trail backfill joins it + // only across a landing or take-off it saw. + onGround: meta.onGround === true, }); if (history.length > POSITION_HISTORY_LIMIT) { history.shift(); diff --git a/src/layers/flights/tracking.js b/src/layers/flights/tracking.js index 4b43c5ead..02bc8fa0c 100644 --- a/src/layers/flights/tracking.js +++ b/src/layers/flights/tracking.js @@ -550,7 +550,7 @@ export function createTracking({ clone(fix.position), historyFixEpochMs(fix), TRAIL_MAX_POINTS, - { nullAltitude: fix.inventedHeight === true }, + { nullAltitude: fix.inventedHeight === true, onGround: fix.onGround }, ); } if (!flightState._trail && flightState._viewer) { @@ -677,10 +677,17 @@ export function createTracking({ }; }), ); + // A history that ends in the air does not join an aircraft now on the + // ground (or the reverse) across a landing or take-off nobody saw. flightState._trailFixes = joinTrailBackfill( backfill, flightState._trailFixes, TRAIL_MAX_POINTS, + { + backfillEndsOnGround: parsed[parsed.length - 1].onGround, + onGroundNow: flightState.records.data.get(icao24)?.onGround, + nowMs: flightState.now(), + }, ); _refreshTrailDisplay(); } diff --git a/src/layers/military/snapshotRenderer.js b/src/layers/military/snapshotRenderer.js index 6e4a80d4b..12350c178 100644 --- a/src/layers/military/snapshotRenderer.js +++ b/src/layers/military/snapshotRenderer.js @@ -87,6 +87,9 @@ export function createMilitarySnapshotRenderer({ // Drawn at an invented height: a trail seeded from history keeps no // position for it. inventedHeight: meta.renderAltitudeInvented === true, + // Whether this report was on the ground: a trail backfill joins it + // only across a landing or take-off it saw. + onGround: meta.onGround === true, }); if (history.length > POSITION_HISTORY_LIMIT) { history.shift(); diff --git a/src/layers/military/tracking.js b/src/layers/military/tracking.js index 8c32989ee..61dbdaeaf 100644 --- a/src/layers/military/tracking.js +++ b/src/layers/military/tracking.js @@ -752,7 +752,7 @@ export function createTracking({ _trailFloorPosition(clone(fix.position)), historyFixEpochMs(fix), TRAIL_MAX_POINTS, - { nullAltitude: fix.inventedHeight === true }, + { nullAltitude: fix.inventedHeight === true, onGround: fix.onGround }, ); } if (!flightState._trail && flightState._viewer) { @@ -881,10 +881,17 @@ export function createTracking({ }; }), ); + // A history that ends in the air does not join an aircraft now on the + // ground (or the reverse) across a landing or take-off nobody saw. flightState._trailFixes = joinTrailBackfill( backfill, flightState._trailFixes, TRAIL_MAX_POINTS, + { + backfillEndsOnGround: parsed[parsed.length - 1].onGround, + onGroundNow: flightState.records.data.get(icao24)?.onGround, + nowMs: flightState.now(), + }, ); _refreshTrailDisplay(); }