geolith_tiles_builder/raster/
pyramid.rs1use geolith::geometry::mercator::TileCoord;
8
9use geolith::terrain::tile_range_for_bounds;
10
11#[derive(Debug, Clone, PartialEq)]
13pub struct PyramidSpec {
14 pub min_zoom: u8,
16 pub max_zoom: u8,
18 pub bbox: Option<(f64, f64, f64, f64)>,
20 pub bbox_min_zoom: u8,
23}
24
25impl PyramidSpec {
26 #[must_use]
28 pub fn tiles(&self) -> Vec<TileCoord> {
29 let mut tiles = Vec::with_capacity(self.count() as usize);
30 for z in self.min_zoom..=self.max_zoom {
31 let (x0, y0, x1, y1) = self.range_at(z);
32 for y in y0..=y1 {
33 for x in x0..=x1 {
34 tiles.push(TileCoord { z, x, y });
35 }
36 }
37 }
38 tiles
39 }
40
41 #[must_use]
43 pub fn count(&self) -> u64 {
44 (self.min_zoom..=self.max_zoom)
45 .map(|z| {
46 let (x0, y0, x1, y1) = self.range_at(z);
47 u64::from(x1 - x0 + 1) * u64::from(y1 - y0 + 1)
48 })
49 .sum()
50 }
51
52 #[must_use]
58 pub const fn covered_bounds(&self) -> (f64, f64, f64, f64) {
59 const WORLD: (f64, f64, f64, f64) = (-180.0, -85.051_128_78, 180.0, 85.051_128_78);
60 match self.bbox {
61 Some(bbox) if self.bbox_min_zoom <= self.min_zoom => bbox,
62 Some(_) | None => WORLD,
63 }
64 }
65
66 fn range_at(&self, z: u8) -> (u32, u32, u32, u32) {
68 let max_index = (1_u32 << z) - 1;
69 match self.bbox {
70 Some(bounds) if z >= self.bbox_min_zoom => tile_range_for_bounds(bounds, z),
71 _ => (0, 0, max_index, max_index),
72 }
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn planet_pyramid_counts_the_full_quadtree() {
82 let spec = PyramidSpec {
83 min_zoom: 0,
84 max_zoom: 3,
85 bbox: None,
86 bbox_min_zoom: 0,
87 };
88 assert_eq!(spec.count(), 1 + 4 + 16 + 64);
89 assert_eq!(spec.tiles().len(), 85);
90 }
91
92 #[test]
93 fn bbox_restricts_all_zooms_by_default() {
94 let india = (68.0, 6.5, 97.5, 37.2);
95 let spec = PyramidSpec {
96 min_zoom: 5,
97 max_zoom: 6,
98 bbox: Some(india),
99 bbox_min_zoom: 0,
100 };
101 let tiles = spec.tiles();
102 assert_eq!(tiles.len() as u64, spec.count());
103 assert!(
104 tiles.iter().all(|t| {
105 let n = 1_u32 << t.z;
106 t.x >= n * 2 / 3 && t.x < n
107 }),
108 "india sits in the eastern third of the world grid"
109 );
110 }
111
112 #[test]
113 fn hybrid_pyramid_switches_to_bbox_at_bbox_min_zoom() {
114 let india = (68.0, 6.5, 97.5, 37.2);
115 let spec = PyramidSpec {
116 min_zoom: 0,
117 max_zoom: 6,
118 bbox: Some(india),
119 bbox_min_zoom: 5,
120 };
121 let planet_below: u64 = (0..5_u32).map(|z| 4_u64.pow(z)).sum();
122 let clipped = PyramidSpec {
123 min_zoom: 5,
124 max_zoom: 6,
125 bbox: Some(india),
126 bbox_min_zoom: 0,
127 };
128 assert_eq!(spec.count(), planet_below + clipped.count());
129 }
130
131 #[test]
132 fn covered_bounds_is_the_world_without_a_bbox() {
133 let spec = PyramidSpec {
134 min_zoom: 0,
135 max_zoom: 3,
136 bbox: None,
137 bbox_min_zoom: 0,
138 };
139 let (min_lon, min_lat, max_lon, max_lat) = spec.covered_bounds();
140 assert!((min_lon + 180.0).abs() < 1e-9);
141 assert!((max_lon - 180.0).abs() < 1e-9);
142 assert!(min_lat < -85.0 && max_lat > 85.0);
143 }
144
145 #[test]
146 fn covered_bounds_is_the_bbox_when_it_applies_at_every_zoom() {
147 let india = (68.0, 6.5, 97.5, 37.2);
148 let spec = PyramidSpec {
149 min_zoom: 5,
150 max_zoom: 8,
151 bbox: Some(india),
152 bbox_min_zoom: 0,
153 };
154 assert_eq!(spec.covered_bounds(), india);
155 }
156
157 #[test]
158 fn covered_bounds_widens_to_the_world_for_a_hybrid_pyramid() {
159 let india = (68.0, 6.5, 97.5, 37.2);
160 let spec = PyramidSpec {
161 min_zoom: 0,
162 max_zoom: 12,
163 bbox: Some(india),
164 bbox_min_zoom: 9,
165 };
166 let bounds = spec.covered_bounds();
167 assert_ne!(
168 bounds, india,
169 "zooms 0-8 hold planet tiles, so the bbox understates coverage"
170 );
171 assert!((bounds.0 + 180.0).abs() < 1e-9 && (bounds.2 - 180.0).abs() < 1e-9);
172 }
173
174 #[test]
175 fn covered_bounds_is_the_bbox_when_the_switch_equals_min_zoom() {
176 let india = (68.0, 6.5, 97.5, 37.2);
177 let spec = PyramidSpec {
178 min_zoom: 9,
179 max_zoom: 12,
180 bbox: Some(india),
181 bbox_min_zoom: 9,
182 };
183 assert_eq!(
184 spec.covered_bounds(),
185 india,
186 "no zoom sits below the switch, so coverage really is bbox-only"
187 );
188 }
189
190 #[test]
191 fn tiles_iterate_shallowest_zoom_first() {
192 let spec = PyramidSpec {
193 min_zoom: 1,
194 max_zoom: 2,
195 bbox: None,
196 bbox_min_zoom: 0,
197 };
198 let tiles = spec.tiles();
199 assert_eq!(tiles.first().map(|t| t.z), Some(1));
200 assert_eq!(tiles.last().map(|t| t.z), Some(2));
201 let boundary = tiles.iter().position(|t| t.z == 2).expect("z2 present");
202 assert_eq!(boundary, 4, "all four z1 tiles precede z2");
203 }
204}