Files
gods-eye-view/src/data/courseFromFixes.js
T
Sameh Khamis fc4db81d0b 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.
2026-11-15 11:19:58 -08:00

62 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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 } };
}