Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / spatial.rs
spatial.rs177 lines · 7.2 KiBRaw
| 1 | //! Spatial primitives shared by both tools. |
| 2 | //! |
| 3 | //! Both functions are on-disk / cross-tool contracts: |
| 4 | //! * [`latlng_to_spatial`] must agree bit-for-bit with |
| 5 | //! `Graph::latlng_to_spatial` in `maps/src/main/rust/src/graph.rs`, because the |
| 6 | //! device binary-searches `nodes.bin` and `poi_index.bin` by this key. |
| 7 | //! * [`accurate_dist_mm`] produces the `dist_mm` stored in every edge, so the |
| 8 | //! radius `R` must stay at the value the router's cost model was tuned against. |
| 9 | |
| 10 | pub const DEG_TO_RAD: f64 = std::f64::consts::PI / 180.0; |
| 11 | |
| 12 | /// Earth radius in millimetres, as used by the router. |
| 13 | const R_MM: f64 = 6_371_000_800.0; |
| 14 | |
| 15 | /// 64-bit Morton (Z-order) code of a lat/lon pair. |
| 16 | /// |
| 17 | /// Hilbert was measured as the alternative and is not better at the scale that |
| 18 | /// matters — see `Graph::latlng_to_spatial`, which carries the numbers. |
| 19 | pub fn latlng_to_spatial(lat: f64, lon: f64) -> u64 { |
| 20 | let x = (lon + 180.0) / 360.0; |
| 21 | let y = (lat + 90.0) / 180.0; |
| 22 | let ix = (x * 4_294_967_295.0) as u32; |
| 23 | let iy = (y * 4_294_967_295.0) as u32; |
| 24 | let mut res: u64 = 0; |
| 25 | for i in 0..32u64 { |
| 26 | res |= (((ix >> i) & 1) as u64) << (2 * i); |
| 27 | res |= (((iy >> i) & 1) as u64) << (2 * i + 1); |
| 28 | } |
| 29 | res |
| 30 | } |
| 31 | |
| 32 | #[inline] |
| 33 | pub fn spatial_from_e7(lat_e7: i32, lon_e7: i32) -> u64 { |
| 34 | latlng_to_spatial(lat_e7 as f64 * 1e-7, lon_e7 as f64 * 1e-7) |
| 35 | } |
| 36 | |
| 37 | /// Haversine great-circle distance in millimetres, saturating at `u32::MAX`. |
| 38 | pub fn accurate_dist_mm(lat1_e7: i32, lon1_e7: i32, lat2_e7: i32, lon2_e7: i32) -> u32 { |
| 39 | let phi1 = (lat1_e7 as f64 * 1e-7) * DEG_TO_RAD; |
| 40 | let phi2 = (lat2_e7 as f64 * 1e-7) * DEG_TO_RAD; |
| 41 | // Widened before subtracting: two points either side of the antimeridian are |
| 42 | // ~3.6e9 apart in 1e-7 degrees, which overflows i32. |
| 43 | let delta_phi = (lat2_e7 as i64 - lat1_e7 as i64) as f64 * 1e-7 * DEG_TO_RAD; |
| 44 | let delta_lambda = (lon2_e7 as i64 - lon1_e7 as i64) as f64 * 1e-7 * DEG_TO_RAD; |
| 45 | let s_dphi = (delta_phi / 2.0).sin(); |
| 46 | let s_dlamb = (delta_lambda / 2.0).sin(); |
| 47 | let a = s_dphi * s_dphi + phi1.cos() * phi2.cos() * s_dlamb * s_dlamb; |
| 48 | let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt()); |
| 49 | (R_MM * c) as u32 |
| 50 | } |
| 51 | |
| 52 | #[cfg(test)] |
| 53 | mod tests { |
| 54 | use super::*; |
| 55 | |
| 56 | /// The reader's copy of the same function, transcribed from |
| 57 | /// `maps/src/main/rust/src/graph.rs`. If either drifts the device's spatial |
| 58 | /// binary search silently returns the wrong nodes, so they are compared here |
| 59 | /// rather than trusted. |
| 60 | fn graph_rs_latlng_to_spatial(lat: f64, lon: f64) -> u64 { |
| 61 | let x = (lon + 180.0) / 360.0; |
| 62 | let y = (lat + 90.0) / 180.0; |
| 63 | let ix = (x * 4_294_967_295.0) as u32; |
| 64 | let iy = (y * 4_294_967_295.0) as u32; |
| 65 | let mut res: u64 = 0; |
| 66 | for i in 0..32u64 { |
| 67 | res |= (((ix >> i) & 1) as u64) << (2 * i); |
| 68 | res |= (((iy >> i) & 1) as u64) << (2 * i + 1); |
| 69 | } |
| 70 | res |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn spatial_key_matches_the_router() { |
| 75 | let coords = [ |
| 76 | (0.0, 0.0), |
| 77 | (37.7749, -122.4194), // San Francisco |
| 78 | (35.2828, -120.6596), // San Luis Obispo |
| 79 | (-33.8688, 151.2093), // Sydney |
| 80 | (90.0, 180.0), |
| 81 | (-90.0, -180.0), |
| 82 | (0.0000001, -0.0000001), |
| 83 | ]; |
| 84 | for (lat, lon) in coords { |
| 85 | assert_eq!( |
| 86 | latlng_to_spatial(lat, lon), |
| 87 | graph_rs_latlng_to_spatial(lat, lon), |
| 88 | "({lat}, {lon})" |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn morton_interleaves_x_into_even_bits() { |
| 95 | // lon = -180 -> ix = 0; lat = -90 -> iy = 0. |
| 96 | assert_eq!(latlng_to_spatial(-90.0, -180.0), 0); |
| 97 | // Every odd bit set means iy = u32::MAX-ish and ix = 0. |
| 98 | let k = latlng_to_spatial(90.0, -180.0); |
| 99 | assert_eq!(k & 0x5555_5555_5555_5555, 0, "no even (lon) bit set"); |
| 100 | assert_ne!(k, 0); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn morton_is_monotonic_in_a_single_axis() { |
| 105 | let a = latlng_to_spatial(37.0, -122.0); |
| 106 | let b = latlng_to_spatial(37.0, -121.0); |
| 107 | assert!(a < b, "increasing longitude must increase the key"); |
| 108 | } |
| 109 | |
| 110 | /// Z-order's defining flaw, pinned rather than merely commented: two cells that |
| 111 | /// touch on the ground can be a whole quadrant apart in key, so two *adjacent |
| 112 | /// nodes* can be far apart in the node array. |
| 113 | /// |
| 114 | /// This is why `edges.bin` cannot store `target` as a small delta from its |
| 115 | /// source without an escape table, and it is what `road_graph --stats` measures |
| 116 | /// the cost of. Hilbert removes this jump and still does not help at the `i16` |
| 117 | /// boundary — see `Graph::latlng_to_spatial` for the measurement — so the |
| 118 | /// discontinuity is documented here instead of designed away. |
| 119 | #[test] |
| 120 | fn morton_jumps_a_whole_quadrant_at_a_quadrant_boundary() { |
| 121 | // Two points either side of the equator at the same longitude, a hair |
| 122 | // apart: the y bit at the top level flips, so the keys differ in bit 63. |
| 123 | let below = latlng_to_spatial(-0.0000001, 10.0); |
| 124 | let above = latlng_to_spatial(0.0000001, 10.0); |
| 125 | assert!( |
| 126 | above.abs_diff(below) > 1 << 62, |
| 127 | "expected a top-level jump, got {}", |
| 128 | above.abs_diff(below) |
| 129 | ); |
| 130 | // Whereas the same separation away from any boundary is tiny. |
| 131 | let a = latlng_to_spatial(10.0, 10.0); |
| 132 | let b = latlng_to_spatial(10.0000001, 10.0); |
| 133 | assert!(a.abs_diff(b) < 1 << 20, "got {}", a.abs_diff(b)); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn haversine_matches_known_city_pairs() { |
| 138 | // Distances are within 0.5% of the published great-circle values, which |
| 139 | // catches a transcription error (wrong radius, swapped lat/lon, degrees |
| 140 | // used as radians) while tolerating last-bit differences. |
| 141 | let cases = [ |
| 142 | // SF -> LA, ~559 km |
| 143 | ((377_749_000, -1_224_194_000), (340_522_000, -1_182_437_000), 559_000_000u32), |
| 144 | // SF -> San Luis Obispo, ~318.6 km |
| 145 | ((377_749_000, -1_224_194_000), (352_828_000, -1_206_596_000), 318_600_000), |
| 146 | // 1 degree of latitude at the equator, ~111.2 km |
| 147 | ((0, 0), (10_000_000, 0), 111_200_000), |
| 148 | ]; |
| 149 | for ((la1, lo1), (la2, lo2), expect) in cases { |
| 150 | let got = accurate_dist_mm(la1, lo1, la2, lo2); |
| 151 | assert!(got.abs_diff(expect) < expect / 200, "expected ~{expect} mm, got {got} mm"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn haversine_is_zero_and_symmetric() { |
| 157 | let sf = (377_749_000, -1_224_194_000); |
| 158 | let la = (340_522_000, -1_182_437_000); |
| 159 | assert_eq!(accurate_dist_mm(sf.0, sf.1, sf.0, sf.1), 0); |
| 160 | assert_eq!( |
| 161 | accurate_dist_mm(sf.0, sf.1, la.0, la.1), |
| 162 | accurate_dist_mm(la.0, la.1, sf.0, sf.1) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn haversine_spans_the_antimeridian_without_overflowing() { |
| 168 | // The coordinate delta here is ~3.6e9, well past i32::MAX. Going the short |
| 169 | // way round is ~2.2 km; haversine takes the long way (~40,000 km) because |
| 170 | // it has no wrap-around notion, but it must not overflow or panic. |
| 171 | let d = accurate_dist_mm(0, 1_799_900_000, 0, -1_799_900_000); |
| 172 | assert!(d > 0, "got {d}"); |
| 173 | assert_eq!(d, accurate_dist_mm(0, -1_799_900_000, 0, 1_799_900_000)); |
| 174 | // Same for latitude, pole to pole. |
| 175 | assert!(accurate_dist_mm(-900_000_000, 0, 900_000_000, 0) > 0); |
| 176 | } |
| 177 | } |