rustflexstack/facilities/local_dynamic_map/ldm_constants.rs
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2024 Fundació Privada Internet i Innovació Digital a Catalunya (i2CAT)
3
4//! LDM constants — ITS-AID values, epoch offset and spatial helpers.
5//!
6//! Mirrors the constant definitions spread across
7//! `flexstack/facilities/local_dynamic_map/` in the Python reference.
8//!
9//! # Haversine fix
10//! The Python reference implementation computed spatial distances using raw
11//! ETSI integer values (tenths-of-microdegrees) without unit conversion,
12//! producing meaningless metre-scale thresholds that were actually in the
13//! ~1e7 range. The `haversine_m` function here converts both coordinates to
14//! degrees first, then applies the standard Haversine formula over the WGS-84
15//! sphere, giving correct distances in metres.
16
17/// ITS-S application identifiers (ETSI TS 102 965).
18pub const ITS_AID_DENM: u32 = 1;
19pub const ITS_AID_CAM: u32 = 2;
20pub const ITS_AID_POI: u32 = 3;
21pub const ITS_AID_SAEM: u32 = 4;
22pub const ITS_AID_EEBL: u32 = 5;
23pub const ITS_AID_IVI: u32 = 6;
24pub const ITS_AID_TLC: u32 = 7;
25pub const ITS_AID_GPC: u32 = 9;
26pub const ITS_AID_GNSS: u32 = 10;
27pub const ITS_AID_TPG: u32 = 11;
28pub const ITS_AID_CRL: u32 = 12;
29pub const ITS_AID_CRT: u32 = 13;
30pub const ITS_AID_SRM: u32 = 14;
31pub const ITS_AID_SSM: u32 = 15;
32pub const ITS_AID_VAM: u32 = 16;
33pub const ITS_AID_IMZM: u32 = 17;
34pub const ITS_AID_PAM: u32 = 21;
35
36/// Milliseconds between UNIX epoch (1970-01-01) and the ITS epoch (2004-01-01).
37/// Used to convert `std::time::SystemTime` to ITS milliseconds.
38///
39/// 2004-01-01T00:00:00 UTC in Unix ms = 1_072_915_200_000
40pub const ITS_EPOCH_MS: u64 = 1_072_915_200_000;
41
42/// Earth mean radius in metres (WGS-84 approximation used for Haversine).
43pub const EARTH_RADIUS_M: f64 = 6_371_000.0;
44
45/// Maximum altitude difference (in cm) between an LDM record and the local
46/// station before the record is considered out-of-area for GC purposes.
47/// (Python reference had `15`, which was in incorrect units.)
48pub const MAINTENANCE_MAX_ALT_DIFF_CM: i32 = 1500;
49
50/// Convert an ETSI timestamp (milliseconds since ITS epoch) to milliseconds
51/// since UNIX epoch.
52#[inline]
53pub fn its_ms_to_unix_ms(its_ms: u64) -> u64 {
54 its_ms + ITS_EPOCH_MS
55}
56
57/// Return the current time as milliseconds since the ITS epoch (2004-01-01).
58/// Panics on platforms where `SystemTime::now()` is before the UNIX epoch
59/// (should never happen in practice).
60pub fn now_its_ms() -> u64 {
61 use std::time::{SystemTime, UNIX_EPOCH};
62 let unix_ms = SystemTime::now()
63 .duration_since(UNIX_EPOCH)
64 .expect("system time before UNIX epoch")
65 .as_millis() as u64;
66 unix_ms.saturating_sub(ITS_EPOCH_MS)
67}
68
69/// Compute the great-circle distance in metres between two positions given as
70/// ETSI integer coordinates (tenths of microdegrees, i.e. × 10⁻⁷ degrees).
71///
72/// This correctly handles the unit conversion that the Python reference
73/// implementation omitted, fixing the spatial filtering bug.
74///
75/// # Arguments
76/// * `lat1_etsi`, `lon1_etsi` — first point (ETSI × 1e7 format)
77/// * `lat2_etsi`, `lon2_etsi` — second point (ETSI × 1e7 format)
78///
79/// # Returns
80/// Great-circle distance in metres.
81pub fn haversine_m(lat1_etsi: i32, lon1_etsi: i32, lat2_etsi: i32, lon2_etsi: i32) -> f64 {
82 // Convert ETSI integer to degrees, then to radians.
83 let lat1 = (lat1_etsi as f64 * 1e-7).to_radians();
84 let lat2 = (lat2_etsi as f64 * 1e-7).to_radians();
85 let dlat = lat2 - lat1;
86 let dlon = ((lon2_etsi - lon1_etsi) as f64 * 1e-7).to_radians();
87
88 let a = (dlat / 2.0).sin().powi(2) + lat1.cos() * lat2.cos() * (dlon / 2.0).sin().powi(2);
89 let c = 2.0 * a.sqrt().asin();
90
91 EARTH_RADIUS_M * c
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 /// Two identical positions → 0 m.
99 #[test]
100 fn haversine_same_point() {
101 let d = haversine_m(415520000, 21340000, 415520000, 21340000);
102 assert!(d.abs() < 1e-6, "same point should be 0 m, got {d}");
103 }
104
105 /// ~111 km per degree of latitude at the equator.
106 #[test]
107 fn haversine_one_degree_lat() {
108 // 0°N,0°E → 1°N,0°E ≈ 111 195 m
109 let d = haversine_m(0, 0, 10_000_000, 0);
110 assert!((d - 111_195.0).abs() < 200.0, "expected ~111 km, got {d}");
111 }
112}