Modern Apps git

← Modern-Apps.git

networklocation: the position solver no longer loses the fix at the antimeridian

Commit
5ac026dc008ec9ce99987015fa390b0f5c596406
Author
Vayun Mathur <[email protected]>
When
2026-12-01 04:48 UTC (5 days ago)
Parents
e0e30aa
estimate_lat_lon projects beacons onto a tangent plane using
the raw longitude difference, so beacons straddling the
antimeridian (±~10 m either side of 180°, consistent within
a cluster) project ~38,000 km apart: the solver legitimately
finds no inlier set and returns null — no network fix at all
near Fiji/Tonga/the Russian Far East/Aleutians whenever APs
are seen on both sides of the seam. The same geometry at
longitude 170 solves fine. The back-conversion could also
emit a longitude outside [-180, 180).

The projection now wraps each beacon's longitude difference
into [-180, 180) via wrap_degrees, and wraps the output
longitude the same way; away from the seam the wrapped values
are identical, so this is the sole change site and nothing
else moves. Rust and Kotlin regression tests drive the seam
inputs through the native solver (null pre-fix). Beacon
keys, the WPSDB reader, geocoder and accuracy pessimism were
audited and cleared. Verified: cargo 67 passed;
:networklocation:testDevUnitTest BUILD SUCCESSFUL; lint
errors are pre-existing on an untouched file.

Diff

networklocation/src/main/rust/src/jni.rs
diff --git a/networklocation/src/main/rust/src/jni.rs b/networklocation/src/main/rust/src/jni.rsindex fa035a8f14..693560d00e 100644--- a/networklocation/src/main/rust/src/jni.rs+++ b/networklocation/src/main/rust/src/jni.rs@@ -24,6 +24,17 @@ const M_PER_DEG_LAT: f64 = 111_320.0; /// reporting an implausibly tiny radius cannot dominate the fit. const MIN_ACCURACY_M: f64 = 1.0; +/// Wrap a longitude difference (or a longitude itself, shifted the same way)+/// into `[-180, 180)`. Beacon longitudes live on a circle: two access points+/// either side of the antimeridian (`179.9999` and `-179.9999`) are metres+/// apart, but their raw difference is `359.9998` degrees — without this the+/// tangent-plane projection below places them on opposite sides of the+/// planet and the robust solver (correctly, on that geometry) finds no+/// consistent position and returns no fix at all.+fn wrap_degrees(deg: f64) -> f64 {+    (deg + 180.0).rem_euclid(360.0) - 180.0+}+ /// Estimate the device position from interleaved beacon fixes. /// /// `beacons` is `[lat0, lon0, acc0, lat1, lon1, acc1, ...]` (degrees, degrees,@@ -61,7 +72,7 @@ fn estimate_lat_lon(beacons: &[f64]) -> Option<[f64; 3]> {         }         let sigma = acc.max(MIN_ACCURACY_M);         let six_sigma_squared = sigma * sigma;-        let east = (lon - lon0) * east_scale;+        let east = wrap_degrees(lon - lon0) * east_scale;         let north = (lat - lat0) * M_PER_DEG_LAT;          measurements.push(Measurement {@@ -80,7 +91,9 @@ fn estimate_lat_lon(beacons: &[f64]) -> Option<[f64; 3]> {     let position = estimated.position;      let lat = lat0 + position.y.value / M_PER_DEG_LAT;-    let lon = lon0 + position.x.value / east_scale;+    // Back on the circle: an estimate just past the antimeridian from the+    // reference beacon must come back as e.g. -179.9999, not 180.0001.+    let lon = wrap_degrees(lon0 + position.x.value / east_scale);     let accuracy = (position.x.six_sigma_squared + position.y.six_sigma_squared).sqrt();      Some([lat, lon, accuracy])@@ -138,6 +151,33 @@ mod tests {         assert!((lon - -0.1278).abs() < 0.001, "lon {lon}");     } +    #[test]+    fn beacons_straddling_the_antimeridian_estimate_across_it() {+        // Two beacons just west and two just east of the antimeridian, each+        // ~10 m from the seam: metres apart on the ground, 359.9998 degrees+        // apart in raw longitude. Without wrapping the longitude difference+        // this returned None (the solver saw two clusters a world apart);+        // the identical geometry shifted to lon 170 estimated fine.+        let beacons = [+            -17.0, 179.9999, 20.0,+            -17.0, 179.9998, 20.0,+            -17.0, -179.9999, 20.0,+            -17.0, -179.9998, 20.0,+        ];+        let [lat, lon, acc] = estimate_lat_lon(&beacons).expect("estimate across the antimeridian");+        assert!((lat - -17.0).abs() < 0.001, "lat {lat}");+        assert!(lon.abs() > 179.99, "lon {lon} must sit on the antimeridian");+        assert!(acc.is_finite() && acc > 0.0, "acc {acc}");+    }++    #[test]+    fn wrap_degrees_folds_the_circle() {+        assert_eq!(wrap_degrees(0.0), 0.0);+        assert!((wrap_degrees(179.9999 - -179.9999) - -0.0002).abs() < 1e-9);+        assert_eq!(wrap_degrees(-180.0), -180.0);+        assert!((wrap_degrees(180.0001) - -179.9999).abs() < 1e-9);+    }+     #[test]     fn tiny_accuracy_is_clamped_not_zero_weight() {         // acc = 0 would give six_sigma_squared = 0 and an infinitely
networklocation/src/test/java/com/vayunmathur/networklocation/NativeBridgesTest.kt
diff --git a/networklocation/src/test/java/com/vayunmathur/networklocation/NativeBridgesTest.kt b/networklocation/src/test/java/com/vayunmathur/networklocation/NativeBridgesTest.ktindex 9c7bde3ee9..e6475220a4 100644--- a/networklocation/src/test/java/com/vayunmathur/networklocation/NativeBridgesTest.kt+++ b/networklocation/src/test/java/com/vayunmathur/networklocation/NativeBridgesTest.kt@@ -73,6 +73,26 @@ class NativeBridgesTest {         assertEquals(-122.084, pos[1], 5e-4)     } +    @Test+    fun `beacons straddling the antimeridian still produce a fix`() {+        // Two beacons just west and two just east of 180 degrees, each ~10 m+        // from the seam. Their raw longitude difference is 359.9998 degrees,+        // so without wrapping the solver saw two clusters a world apart and+        // returned null; the same geometry away from the seam fixes fine.+        val pos = NetworkLocationNative.estimatePosition(+            doubleArrayOf(+                -17.0, 179.9999, 20.0,+                -17.0, 179.9998, 20.0,+                -17.0, -179.9999, 20.0,+                -17.0, -179.9998, 20.0,+            ),+        )+        assertNotNull(pos, "beacons across the antimeridian must not lose the fix")+        assertEquals(-17.0, pos[0], 1e-3)+        assertTrue(pos[1] > 179.99 || pos[1] < -179.99, "lon ${pos[1]} must sit on the antimeridian")+        assertTrue(pos[2] > 0.0, "accuracy must be positive, was ${pos[2]}")+    }+     @Test     fun `no usable beacons yields null`() {         assertNull(NetworkLocationNative.estimatePosition(doubleArrayOf()))