Something went wrong. Try again.
A port of FlightAware's dump1090, a Mode S and ADS-B decoder for software defined radios, to Zig 0.16.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538// SPDX-FileCopyrightText: © 2012 Salvatore Sanfilippo <antirez@gmail.com>// SPDX-FileCopyrightText: © 2014-2016 Oliver Jowett <oliver@mutability.co.uk>// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: GPL-2.0-or-later
//! Compact Position Reporting: turning the 17-bit latitude and longitude//! fragments in a position message back into a place on the earth.//!//! CPR exists because a full-precision position will not fit in an extended//! squitter. Instead the globe is cut into zones and a message carries only//! the aircraft's fractional offset within its zone, which is ambiguous on its//! own — the same pair of numbers names one point per zone, thousands of them.//! Two zone layouts are used alternately, an `.even` one of 60 latitude bands//! and an `.odd` one of 59, and the ambiguity is resolved in one of two ways://!//! * **Globally**, by taking one message of each layout and finding the single//! latitude that both agree on. This needs no prior knowledge, which is what//! makes it the way a new aircraft is first located, but it needs two//! messages close enough in time that the aircraft has not left the zone//! they were solved in.//! * **Relative to a reference** — the receiver, or where the aircraft was//! last seen — by picking the candidate nearest to it. This works from a//! single message, and is wrong rather than absent if the reference is more//! than half a zone away, so every decode here is range-checked against it.//!//! Surface positions use a quarter-size grid, four times the resolution over a//! quarter of the range, so even a global surface solve needs a reference to//! choose a quadrant.
const std = @import("std");
const geo = @import("geo");
/// Which of the two interleaved zone layouts a message was encoded in, the/// `F` bit of a position message.pub const Frame = enum(u1) { /// 60 latitude bands. even = 0, /// 59 latitude bands, offset from the even ones. odd = 1,};
/// Whether a position is an airborne one or a surface one, which decides the/// size of the grid it was encoded against.pub const Kind = enum { airborne, surface,
/// The latitude covered by the whole grid: the full globe in the air, a /// quarter of it on the ground. fn span(self: Kind) f64 { return switch (self) { .airborne => 360.0, .surface => 90.0, }; }};
/// The raw 17-bit fields as they appear in a position message.pub const Encoded = struct { latitude: u17, longitude: u17,
/// 2^17: the number of steps the 17-bit fields divide a zone into. const steps = 131072.0;
fn fractionalLatitude(self: Encoded) f64 { return @as(f64, @floatFromInt(self.latitude)) / steps; }
fn fractionalLongitude(self: Encoded) f64 { return @as(f64, @floatFromInt(self.longitude)) / steps; }};
/// A decoded position, in degrees. zig-geo's, so that the distances the/// tracker checks positions against can be asked of it directly.pub const Position = geo.Position;
/// Why a global solve failed.pub const GlobalError = error{ /// The two messages solved into different latitude zones, so the pair /// cannot be combined. The aircraft crossed a zone boundary between them; /// the next pair will very likely work. ZoneMismatch, /// The solve produced a latitude off the earth, which means the message /// contents were not a real position. OutOfRange,};
/// Why a solve against a reference failed.pub const RelativeError = error{ /// The solve produced a latitude off the earth. OutOfRange, /// The nearest candidate is more than half a zone from the reference, so /// there is no way to know it is the right one. TooFarFromReference,};
/// NL: how many longitude zones the globe is cut into at a given latitude.////// The zones are sized to stay roughly constant in ground distance, so there/// are fewer of them the closer to a pole you get — 59 at the equator, one at/// the poles themselves. This is the transition table from 1090-WP-9-14/// rather than the closed form, because the standard defines the table to be/// normative and the two disagree on the boundaries.pub fn longitudeZones(latitude: f64) u8 { // The table is symmetric about the equator. const lat = @abs(latitude); for (nl_boundaries, 0..) |boundary, i| { if (lat < boundary) return @intCast(59 - i); } return 1;}
/// The latitude below which there are 59, 58, ... 2 longitude zones.const nl_boundaries = [_]f64{ 10.47047130, 14.82817437, 18.18626357, 21.02939493, 23.54504487, 25.82924707, 27.93898710, 29.91135686, 31.77209708, 33.53993436, 35.22899598, 36.85025108, 38.41241892, 39.92256684, 41.38651832, 42.80914012, 44.19454951, 45.54626723, 46.86733252, 48.16039128, 49.42776439, 50.67150166, 51.89342469, 53.09516153, 54.27817472, 55.44378444, 56.59318756, 57.72747354, 58.84763776, 59.95459277, 61.04917774, 62.13216659, 63.20427479, 64.26616523, 65.31845310, 66.36171008, 67.39646774, 68.42322022, 69.44242631, 70.45451075, 71.45986473, 72.45884545, 73.45177442, 74.43893416, 75.42056257, 76.39684391, 77.36789461, 78.33374083, 79.29428225, 80.24923213, 81.19801349, 82.13956981, 83.07199445, 83.99173563, 84.89166191, 85.75541621, 86.53536998, 87.00000000,};
/// The number of longitude zones a particular frame uses, which is one fewer/// than NL for odd frames — that offset is the whole trick that makes the two/// layouts disambiguate each other.fn zonesForFrame(latitude: f64, frame: Frame) u8 { const nl = longitudeZones(latitude); return switch (frame) { .even => nl, .odd => if (nl > 1) nl - 1 else 1, };}
/// The width in degrees of one longitude zone.fn longitudeZoneWidth(latitude: f64, frame: Frame, kind: Kind) f64 { return kind.span() / @as(f64, @floatFromInt(zonesForFrame(latitude, frame)));}
/// Solve an airborne position from one even and one odd message, with no/// reference position needed.////// `latest` says which of the two arrived most recently; the answer is that/// message's position, since the other is by then out of date.pub fn decodeGlobalAirborne(even: Encoded, odd: Encoded, latest: Frame) GlobalError!Position { const latitudes = try solveLatitudes(even, odd, .airborne); const latitude = latitudes.of(latest);
return .{ .latitude = latitude, .longitude = normalizeLongitude(solveLongitude(even, odd, latitudes, latest, .airborne)), };}
/// Solve a surface position from one even and one odd message.////// The surface grid covers only a quarter of the globe, so the solve names/// four candidate positions rather than one and `reference` — the receiver's/// own location — picks between them. The chosen quadrant is the one nearest/// the reference, which is not always the one the reference is in.pub fn decodeGlobalSurface(reference: Position, even: Encoded, odd: Encoded, latest: Frame) GlobalError!Position { var latitudes = solveLatitudesUnchecked(even, odd, .surface); latitudes.even = nearestSurfaceLatitude(latitudes.even, reference.latitude); latitudes.odd = nearestSurfaceLatitude(latitudes.odd, reference.latitude); try latitudes.check();
const latitude = latitudes.of(latest); var longitude = solveLongitude(even, odd, latitudes, latest, .surface);
// Unlike latitude, all four longitude quadrants are legitimate: step // whole quadrants towards the reference until we are within half of one. longitude += @floor((reference.longitude - longitude + 45.0) / 90.0) * 90.0;
return .{ .latitude = latitude, .longitude = normalizeLongitude(longitude) };}
/// Solve a position from a single message, by picking the candidate nearest/// `reference` — either the receiver's location or where this aircraft was/// last seen.////// Fails rather than guessing if the answer lands more than half a zone from/// the reference, because beyond that the nearest candidate is not reliably/// the right one.pub fn decodeRelative(reference: Position, encoded: Encoded, frame: Frame, kind: Kind) RelativeError!Position { const latitude_zone = latitudeZoneHeight(frame, kind); const fractional_latitude = encoded.fractionalLatitude();
// Which latitude zone the reference sits in, adjusted by whichever of the // neighbouring zones puts the aircraft closest to it. Eq 38 of // 1090-WP29-07: the rounding is applied to the whole difference, not to // the zone offsets separately. const j = @floor(reference.latitude / latitude_zone) + @floor(0.5 + @mod(reference.latitude, latitude_zone) / latitude_zone - fractional_latitude);
var latitude = latitude_zone * (j + fractional_latitude); if (latitude >= 270.0) latitude -= 360.0;
if (latitude < -90.0 or latitude > 90.0) return error.OutOfRange; if (@abs(latitude - reference.latitude) > latitude_zone / 2.0) return error.TooFarFromReference;
const longitude_zone = longitudeZoneWidth(latitude, frame, kind); const fractional_longitude = encoded.fractionalLongitude();
const m = @floor(reference.longitude / longitude_zone) + @floor(0.5 + @mod(reference.longitude, longitude_zone) / longitude_zone - fractional_longitude);
var longitude = longitude_zone * (m + fractional_longitude); if (longitude > 180.0) longitude -= 360.0;
if (@abs(longitude - reference.longitude) > longitude_zone / 2.0) return error.TooFarFromReference;
return .{ .latitude = latitude, .longitude = longitude };}
/// The height of one latitude zone: the grid divided into 60 bands for even/// frames and 59 for odd ones.fn latitudeZoneHeight(frame: Frame, kind: Kind) f64 { const bands: f64 = switch (frame) { .even => 60.0, .odd => 59.0, }; return kind.span() / bands;}
/// The latitude each of the two messages solves to. They must agree on which/// longitude zone they are in, or the pair is unusable.const Latitudes = struct { even: f64, odd: f64,
fn of(self: Latitudes, frame: Frame) f64 { return switch (frame) { .even => self.even, .odd => self.odd, }; }
fn check(self: Latitudes) GlobalError!void { if (self.even < -90.0 or self.even > 90.0 or self.odd < -90.0 or self.odd > 90.0) return error.OutOfRange; if (longitudeZones(self.even) != longitudeZones(self.odd)) return error.ZoneMismatch; }};
/// The latitude half of the global solve, common to both grids.////// The latitude index `j` is the one value consistent with both the 60-band/// and the 59-band layout, which is what makes a pair of messages locate an/// aircraft that neither could locate alone.fn solveLatitudesUnchecked(even: Encoded, odd: Encoded, kind: Kind) Latitudes { const even_zone = latitudeZoneHeight(.even, kind); const odd_zone = latitudeZoneHeight(.odd, kind);
const lat0: f64 = @floatFromInt(even.latitude); const lat1: f64 = @floatFromInt(odd.latitude);
const j: i32 = @intFromFloat(@floor(((59.0 * lat0 - 60.0 * lat1) / Encoded.steps) + 0.5));
return .{ .even = even_zone * (@as(f64, @floatFromInt(@mod(j, 60))) + even.fractionalLatitude()), .odd = odd_zone * (@as(f64, @floatFromInt(@mod(j, 59))) + odd.fractionalLatitude()), };}
fn solveLatitudes(even: Encoded, odd: Encoded, kind: Kind) GlobalError!Latitudes { var latitudes = solveLatitudesUnchecked(even, odd, kind); // The airborne grid wraps the whole globe, so a solution in 270..360 is // really a southern one. if (latitudes.even >= 270.0) latitudes.even -= 360.0; if (latitudes.odd >= 270.0) latitudes.odd -= 360.0; try latitudes.check(); return latitudes;}
/// Choose between the northern and southern reading of a surface latitude.////// Only two of the four quadrants are valid, since no real message encodes a/// latitude outside -90..+90. If the northern reading is more than 45 degrees/// north of the reference, the southern one is nearer. Zero is the awkward/// case: -90, 0 and +90 all encode to it, so the reference alone decides.fn nearestSurfaceLatitude(latitude: f64, reference_latitude: f64) f64 { if (latitude == 0.0) { if (reference_latitude < -45.0) return -90.0; if (reference_latitude > 45.0) return 90.0; return 0.0; } if (latitude - reference_latitude > 45.0) return latitude - 90.0; return latitude;}
/// The longitude half of the global solve, once the latitude is known.fn solveLongitude(even: Encoded, odd: Encoded, latitudes: Latitudes, latest: Frame, kind: Kind) f64 { const latitude = latitudes.of(latest); const nl: f64 = @floatFromInt(longitudeZones(latitude));
const lon0: f64 = @floatFromInt(even.longitude); const lon1: f64 = @floatFromInt(odd.longitude);
const m: i32 = @intFromFloat(@floor(((lon0 * (nl - 1.0) - lon1 * nl) / Encoded.steps) + 0.5)); const zones: i32 = zonesForFrame(latitude, latest);
const fractional = switch (latest) { .even => even.fractionalLongitude(), .odd => odd.fractionalLongitude(), };
return longitudeZoneWidth(latitude, latest, kind) * (@as(f64, @floatFromInt(@mod(m, zones))) + fractional);}
/// Fold a longitude into -180..+180.fn normalizeLongitude(longitude: f64) f64 { return longitude - @floor((longitude + 180.0) / 360.0) * 360.0;}
const testing = std.testing;
/// Upstream's tolerance in `cprtests.c`.const tolerance = 1e-6;
fn expectPosition(latitude: f64, longitude: f64, actual: Position) !void { try testing.expectApproxEqAbs(latitude, actual.latitude, tolerance); try testing.expectApproxEqAbs(longitude, actual.longitude, tolerance);}
test "NL is symmetric about the equator and shrinks towards the poles" { try testing.expectEqual(@as(u8, 59), longitudeZones(0.0)); try testing.expectEqual(@as(u8, 59), longitudeZones(10.0)); try testing.expectEqual(@as(u8, 58), longitudeZones(-11.0)); try testing.expectEqual(@as(u8, 36), longitudeZones(52.5)); try testing.expectEqual(@as(u8, 2), longitudeZones(86.6)); try testing.expectEqual(@as(u8, 1), longitudeZones(90.0)); try testing.expectEqual(@as(u8, 1), longitudeZones(-90.0));
for (nl_boundaries, 0..) |boundary, i| { const expected: u8 = @intCast(59 - i); try testing.expectEqual(expected, longitudeZones(boundary - 1e-9)); try testing.expectEqual(expected, longitudeZones(-(boundary - 1e-9))); try testing.expect(longitudeZones(boundary) < expected); }}
// The vectors below are upstream's, from `cprtests.c`.
test "global airborne solve" { const cases = [_]struct { even: Encoded, odd: Encoded, even_latitude: f64, even_longitude: f64, odd_latitude: f64, odd_longitude: f64, }{ .{ .even = .{ .latitude = 80536, .longitude = 9432 }, .odd = .{ .latitude = 61720, .longitude = 9192 }, .even_latitude = 51.686646, .even_longitude = 0.700156, .odd_latitude = 51.686763, .odd_longitude = 0.701294, }, .{ .even = .{ .latitude = 80534, .longitude = 9413 }, .odd = .{ .latitude = 61714, .longitude = 9144 }, .even_latitude = 51.686554, .even_longitude = 0.698745, .odd_latitude = 51.686484, .odd_longitude = 0.697632, }, };
for (cases) |c| { try expectPosition(c.even_latitude, c.even_longitude, try decodeGlobalAirborne(c.even, c.odd, .even)); try expectPosition(c.odd_latitude, c.odd_longitude, try decodeGlobalAirborne(c.even, c.odd, .odd)); }}
test "global surface solve picks the quadrant nearest the reference" { // A real position on the Cambridge (UK) airport apron, decoded against // references scattered around the globe. The reference moves; the answer // should only move by whole quadrants. const apron_even: Encoded = .{ .latitude = 105730, .longitude = 9259 }; const apron_odd: Encoded = .{ .latitude = 29693, .longitude = 8997 };
const longitude_cases = [_]struct { reference_longitude: f64, offset: f64 }{ .{ .reference_longitude = -180.00, .offset = -180.0 }, .{ .reference_longitude = -140.00, .offset = -180.0 }, .{ .reference_longitude = -130.00, .offset = -90.0 }, .{ .reference_longitude = -50.00, .offset = -90.0 }, .{ .reference_longitude = -40.00, .offset = 0.0 }, .{ .reference_longitude = -10.00, .offset = 0.0 }, .{ .reference_longitude = 0.00, .offset = 0.0 }, .{ .reference_longitude = 10.00, .offset = 0.0 }, .{ .reference_longitude = 40.00, .offset = 0.0 }, .{ .reference_longitude = 50.00, .offset = 90.0 }, .{ .reference_longitude = 130.00, .offset = 90.0 }, .{ .reference_longitude = 140.00, .offset = -180.0 }, .{ .reference_longitude = 180.00, .offset = -180.0 }, };
for (longitude_cases) |c| { const reference: Position = .{ .latitude = 52.00, .longitude = c.reference_longitude }; try expectPosition(52.209984, 0.176601 + c.offset, try decodeGlobalSurface(reference, apron_even, apron_odd, .even)); try expectPosition(52.209976, 0.176507 + c.offset, try decodeGlobalSurface(reference, apron_even, apron_odd, .odd)); }
// Latitude has only the two quadrants. The longitude changes with it, // because the zone width does. const latitude_cases = [_]struct { reference_latitude: f64, even_latitude: f64, even_longitude: f64, odd_latitude: f64, odd_longitude: f64, }{ .{ .reference_latitude = 90.00, .even_latitude = 52.209984, .even_longitude = 0.176601, .odd_latitude = 52.209976, .odd_longitude = 0.176507 }, .{ .reference_latitude = 52.00, .even_latitude = 52.209984, .even_longitude = 0.176601, .odd_latitude = 52.209976, .odd_longitude = 0.176507 }, .{ .reference_latitude = 8.00, .even_latitude = 52.209984, .even_longitude = 0.176601, .odd_latitude = 52.209976, .odd_longitude = 0.176507 }, .{ .reference_latitude = 7.00, .even_latitude = 52.209984 - 90.0, .even_longitude = 0.135269, .odd_latitude = 52.209976 - 90.0, .odd_longitude = 0.134299 }, .{ .reference_latitude = -52.00, .even_latitude = 52.209984 - 90.0, .even_longitude = 0.135269, .odd_latitude = 52.209976 - 90.0, .odd_longitude = 0.134299 }, .{ .reference_latitude = -90.00, .even_latitude = 52.209984 - 90.0, .even_longitude = 0.135269, .odd_latitude = 52.209976 - 90.0, .odd_longitude = 0.134299 }, };
for (latitude_cases) |c| { const reference: Position = .{ .latitude = c.reference_latitude, .longitude = 0.0 }; try expectPosition(c.even_latitude, c.even_longitude, try decodeGlobalSurface(reference, apron_even, apron_odd, .even)); try expectPosition(c.odd_latitude, c.odd_longitude, try decodeGlobalSurface(reference, apron_even, apron_odd, .odd)); }}
test "global surface solve at the poles and the equator" { // All-zero fields encode -90, 0 and +90 alike; only the reference tells // them apart. const zero: Encoded = .{ .latitude = 0, .longitude = 0 }; const cases = [_]struct { reference_latitude: f64, latitude: f64 }{ .{ .reference_latitude = -46.00, .latitude = -90.0 }, .{ .reference_latitude = -44.00, .latitude = 0.0 }, .{ .reference_latitude = 44.00, .latitude = 0.0 }, .{ .reference_latitude = 46.00, .latitude = 90.0 }, };
for (cases) |c| { const reference: Position = .{ .latitude = c.reference_latitude, .longitude = -180.00 }; try expectPosition(c.latitude, -180.0, try decodeGlobalSurface(reference, zero, zero, .even)); try expectPosition(c.latitude, -180.0, try decodeGlobalSurface(reference, zero, zero, .odd)); }}
test "relative solve" { const cases = [_]struct { reference: Position, encoded: Encoded, frame: Frame, kind: Kind, latitude: f64, longitude: f64, }{ // Airborne, reference right on top of the aircraft. .{ .reference = .{ .latitude = 52.00, .longitude = 0.00 }, .encoded = .{ .latitude = 80536, .longitude = 9432 }, .frame = .even, .kind = .airborne, .latitude = 51.686646, .longitude = 0.700156 }, .{ .reference = .{ .latitude = 52.00, .longitude = 0.00 }, .encoded = .{ .latitude = 61720, .longitude = 9192 }, .frame = .odd, .kind = .airborne, .latitude = 51.686763, .longitude = 0.701294 }, .{ .reference = .{ .latitude = 52.00, .longitude = 0.00 }, .encoded = .{ .latitude = 80534, .longitude = 9413 }, .frame = .even, .kind = .airborne, .latitude = 51.686554, .longitude = 0.698745 }, .{ .reference = .{ .latitude = 52.00, .longitude = 0.00 }, .encoded = .{ .latitude = 61714, .longitude = 9144 }, .frame = .odd, .kind = .airborne, .latitude = 51.686484, .longitude = 0.697632 },
// Airborne latitude tolerance: the zone is 360/60 = 6 degrees, so the // reference has about 3 to play with. .{ .reference = .{ .latitude = 48.70, .longitude = 0.00 }, .encoded = .{ .latitude = 80536, .longitude = 9432 }, .frame = .even, .kind = .airborne, .latitude = 51.686646, .longitude = 0.700156 }, .{ .reference = .{ .latitude = 54.60, .longitude = 0.00 }, .encoded = .{ .latitude = 61720, .longitude = 9192 }, .frame = .odd, .kind = .airborne, .latitude = 51.686763, .longitude = 0.701294 },
// Airborne longitude tolerance: about 4.8 degrees at this latitude. .{ .reference = .{ .latitude = 52.00, .longitude = 5.40 }, .encoded = .{ .latitude = 80536, .longitude = 9432 }, .frame = .even, .kind = .airborne, .latitude = 51.686646, .longitude = 0.700156 }, .{ .reference = .{ .latitude = 52.00, .longitude = -4.10 }, .encoded = .{ .latitude = 61720, .longitude = 9192 }, .frame = .odd, .kind = .airborne, .latitude = 51.686763, .longitude = 0.701294 },
// Surface, same Cambridge apron. The zone is 90/60 = 1.5 degrees. .{ .reference = .{ .latitude = 52.00, .longitude = 0.00 }, .encoded = .{ .latitude = 105730, .longitude = 9259 }, .frame = .even, .kind = .surface, .latitude = 52.209984, .longitude = 0.176601 }, .{ .reference = .{ .latitude = 52.00, .longitude = 0.00 }, .encoded = .{ .latitude = 29693, .longitude = 8997 }, .frame = .odd, .kind = .surface, .latitude = 52.209976, .longitude = 0.176507 }, .{ .reference = .{ .latitude = 51.46, .longitude = 0.00 }, .encoded = .{ .latitude = 105730, .longitude = 9259 }, .frame = .even, .kind = .surface, .latitude = 52.209984, .longitude = 0.176601 }, .{ .reference = .{ .latitude = 52.95, .longitude = 0.00 }, .encoded = .{ .latitude = 29693, .longitude = 8997 }, .frame = .odd, .kind = .surface, .latitude = 52.209976, .longitude = 0.176507 }, .{ .reference = .{ .latitude = 52.00, .longitude = 1.40 }, .encoded = .{ .latitude = 105730, .longitude = 9259 }, .frame = .even, .kind = .surface, .latitude = 52.209984, .longitude = 0.176601 }, .{ .reference = .{ .latitude = 52.00, .longitude = -1.05 }, .encoded = .{ .latitude = 29693, .longitude = 8997 }, .frame = .odd, .kind = .surface, .latitude = 52.209976, .longitude = 0.176507 }, };
for (cases) |c| { try expectPosition(c.latitude, c.longitude, try decodeRelative(c.reference, c.encoded, c.frame, c.kind)); }}
test "a relative solve can alias if the reference is too far away" { // The half-zone checks below catch a solve that lands off the earth, but // they cannot catch this: once the reference is more than half a zone from // the aircraft, the *neighbouring* zone's solution is the nearer one, and // it is returned without complaint. Keeping the reference close is the // caller's job, and this is what it costs to get it wrong. const encoded: Encoded = .{ .latitude = 80536, .longitude = 9432 };
// 3 degrees below the true 51.686646 still solves correctly. try expectPosition(51.686646, 0.700156, try decodeRelative(.{ .latitude = 48.70, .longitude = 0.0 }, encoded, .even, .airborne)); // A tenth of a degree further and the answer is a whole 6-degree zone out. try expectPosition(45.686646, 0.631848, try decodeRelative(.{ .latitude = 48.60, .longitude = 0.0 }, encoded, .even, .airborne));
// The same in longitude, where the zone is about 9.7 degrees wide here. try expectPosition(51.686646, 0.700156, try decodeRelative(.{ .latitude = 52.0, .longitude = 5.40 }, encoded, .even, .airborne)); try expectPosition(51.686646, 10.429885, try decodeRelative(.{ .latitude = 52.0, .longitude = 5.60 }, encoded, .even, .airborne));}
test "a relative solve refuses a position off the earth" { // A surface solve near the pole can land just past 90 degrees north. try testing.expectError( error.OutOfRange, decodeRelative(.{ .latitude = 89.5, .longitude = 0.0 }, .{ .latitude = 7, .longitude = 0 }, .even, .surface), );}
test "a global solve rejects pairs it cannot combine" { const even: Encoded = .{ .latitude = 80536, .longitude = 9432 };
// Latitudes that solve into different NL zones cannot be combined; the // next pair of messages very likely can be. try testing.expectError( error.ZoneMismatch, decodeGlobalAirborne(even, .{ .latitude = 60431, .longitude = 9192 }, .even), );
// And a pair that solves to a latitude off the earth was never a position. try testing.expectError( error.OutOfRange, decodeGlobalAirborne(even, .{ .latitude = 0, .longitude = 9192 }, .even), );}