Modern Apps git

← Modern-Apps.git

library/map: geometry queries order tiles by their centre, not their NW corner

Commit
bd53040f4fbf1116a9f25175d774c9926c708f25
Author
Vayun Mathur <[email protected]>
When
2026-12-01 09:52 UTC (5 days ago)
Parents
0d1d02e
tiles_for_bbox documented centre-first ordering (so capped
extracts keep the most relevant tiles), but its sort key
used the tile's north-west corner (extent 1, px=py=0)
instead of the tile centre. Corner distance is not centre
distance: for the San Francisco bbox at z10 the tile
containing the region centre sorted last
[(164,396),(164,395),(163,396),(163,395)] and the farthest
first, so under the MAX_FEATURES/MAX_POINTS caps the
central tile's features were the ones dropped.

The key is now the true centre (extent 2 midpoint). New
regression test failed pre-fix on exactly that order and
asserts [(163,395),(164,395),(163,396),(164,396)] now.
Projection, Mercator and camera math were audited and
cleared. Verified: cargo test 573 pass; :library:map:
testDebugUnitTest BUILD SUCCESSFUL; lint no issues.

Diff

library/map/src/main/rust/src/tile/geometry_query.rs
diff --git a/library/map/src/main/rust/src/tile/geometry_query.rs b/library/map/src/main/rust/src/tile/geometry_query.rsindex 007e7df699..e4f3762db7 100644--- a/library/map/src/main/rust/src/tile/geometry_query.rs+++ b/library/map/src/main/rust/src/tile/geometry_query.rs@@ -209,8 +209,14 @@ pub fn tiles_for_bbox(west: f64, south: f64, east: f64, north: f64, z: u8) -> Ve             let t = TileId { z, x, y };             // Distance from the bbox centre to this tile's centre, in             // tile units — only an ordering key, never a measurement.-            let tx = point_lon(t, 1, 0) - cx;-            let ty = point_lat(t, 1, 0) - cy;+            // The centre is the extent-2 midpoint (px=py=1), not the+            // north-west corner (extent 1, px=py=0): sorting by the+            // corner puts the south-east neighbour first whenever the+            // bbox centre sits in the north-west tile's south-east+            // quadrant, so a capped query would keep the farthest tile+            // and drop the one containing the centre.+            let tx = point_lon(t, 2, 1) - cx;+            let ty = point_lat(t, 2, 1) - cy;             tiles.push((tx * tx + ty * ty, t));         }     }
library/map/src/main/rust/src/tile/geometry_query/tests.rs
diff --git a/library/map/src/main/rust/src/tile/geometry_query/tests.rs b/library/map/src/main/rust/src/tile/geometry_query/tests.rsindex 78e1639572..b34ea64110 100644--- a/library/map/src/main/rust/src/tile/geometry_query/tests.rs+++ b/library/map/src/main/rust/src/tile/geometry_query/tests.rs@@ -159,6 +159,28 @@ fn wash_underlay_stitches_parent_water_into_a_gated_child() {     assert!(pairs[0].1.is_none()); } +#[test]+fn tiles_for_bbox_is_centre_first_not_northwest_corner_first() {+    // Bbox centre (-122.4, 37.8) lies in z10 tile (163, 395): that tile+    // spans lon -122.695312..-122.343750 and lat 37.718590..37.996163.+    // Its centre (-122.519531, 37.857507) is d2=0.017595 from the bbox+    // centre, the nearest of the four covering tiles; the south-east+    // tile (164, 396), centre (-122.167969, 37.579413) at d2=0.102497,+    // is the farthest. Sorting by each tile's north-west corner instead+    // of its centre puts (164, 396) first (corner d2=0.009792), so a+    // capped query would keep the farthest tile and drop the centre one.+    let tiles = tiles_for_bbox(-122.5, 37.7, -122.3, 37.9, 10);+    assert_eq!(+        tiles,+        vec![+            TileId { z: 10, x: 163, y: 395 },+            TileId { z: 10, x: 164, y: 395 },+            TileId { z: 10, x: 163, y: 396 },+            TileId { z: 10, x: 164, y: 396 },+        ]+    );+}+ #[test] fn tile_selection_and_zoom_are_bounded() {     // The whole world at z15 would be billions of tiles; the zoom