use geolith::geometry::mercator::TileCoord;
use geolith::terrain::tile_range_for_bounds;
#[derive(Debug, Clone, PartialEq)]
pub struct PyramidSpec {
pub min_zoom: u8,
pub max_zoom: u8,
pub bbox: Option<(f64, f64, f64, f64)>,
pub bbox_min_zoom: u8,
}
impl PyramidSpec {
#[must_use]
pub fn tiles(&self) -> Vec<TileCoord> {
let mut tiles = Vec::with_capacity(self.count() as usize);
for z in self.min_zoom..=self.max_zoom {
let (x0, y0, x1, y1) = self.range_at(z);
for y in y0..=y1 {
for x in x0..=x1 {
tiles.push(TileCoord { z, x, y });
}
}
}
tiles
}
#[must_use]
pub fn count(&self) -> u64 {
(self.min_zoom..=self.max_zoom)
.map(|z| {
let (x0, y0, x1, y1) = self.range_at(z);
u64::from(x1 - x0 + 1) * u64::from(y1 - y0 + 1)
})
.sum()
}
#[must_use]
pub const fn covered_bounds(&self) -> (f64, f64, f64, f64) {
const WORLD: (f64, f64, f64, f64) = (-180.0, -85.051_128_78, 180.0, 85.051_128_78);
match self.bbox {
Some(bbox) if self.bbox_min_zoom <= self.min_zoom => bbox,
Some(_) | None => WORLD,
}
}
fn range_at(&self, z: u8) -> (u32, u32, u32, u32) {
let max_index = (1_u32 << z) - 1;
match self.bbox {
Some(bounds) if z >= self.bbox_min_zoom => tile_range_for_bounds(bounds, z),
_ => (0, 0, max_index, max_index),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn planet_pyramid_counts_the_full_quadtree() {
let spec = PyramidSpec {
min_zoom: 0,
max_zoom: 3,
bbox: None,
bbox_min_zoom: 0,
};
assert_eq!(spec.count(), 1 + 4 + 16 + 64);
assert_eq!(spec.tiles().len(), 85);
}
#[test]
fn bbox_restricts_all_zooms_by_default() {
let india = (68.0, 6.5, 97.5, 37.2);
let spec = PyramidSpec {
min_zoom: 5,
max_zoom: 6,
bbox: Some(india),
bbox_min_zoom: 0,
};
let tiles = spec.tiles();
assert_eq!(tiles.len() as u64, spec.count());
assert!(
tiles.iter().all(|t| {
let n = 1_u32 << t.z;
t.x >= n * 2 / 3 && t.x < n
}),
"india sits in the eastern third of the world grid"
);
}
#[test]
fn hybrid_pyramid_switches_to_bbox_at_bbox_min_zoom() {
let india = (68.0, 6.5, 97.5, 37.2);
let spec = PyramidSpec {
min_zoom: 0,
max_zoom: 6,
bbox: Some(india),
bbox_min_zoom: 5,
};
let planet_below: u64 = (0..5_u32).map(|z| 4_u64.pow(z)).sum();
let clipped = PyramidSpec {
min_zoom: 5,
max_zoom: 6,
bbox: Some(india),
bbox_min_zoom: 0,
};
assert_eq!(spec.count(), planet_below + clipped.count());
}
#[test]
fn covered_bounds_is_the_world_without_a_bbox() {
let spec = PyramidSpec {
min_zoom: 0,
max_zoom: 3,
bbox: None,
bbox_min_zoom: 0,
};
let (min_lon, min_lat, max_lon, max_lat) = spec.covered_bounds();
assert!((min_lon + 180.0).abs() < 1e-9);
assert!((max_lon - 180.0).abs() < 1e-9);
assert!(min_lat < -85.0 && max_lat > 85.0);
}
#[test]
fn covered_bounds_is_the_bbox_when_it_applies_at_every_zoom() {
let india = (68.0, 6.5, 97.5, 37.2);
let spec = PyramidSpec {
min_zoom: 5,
max_zoom: 8,
bbox: Some(india),
bbox_min_zoom: 0,
};
assert_eq!(spec.covered_bounds(), india);
}
#[test]
fn covered_bounds_widens_to_the_world_for_a_hybrid_pyramid() {
let india = (68.0, 6.5, 97.5, 37.2);
let spec = PyramidSpec {
min_zoom: 0,
max_zoom: 12,
bbox: Some(india),
bbox_min_zoom: 9,
};
let bounds = spec.covered_bounds();
assert_ne!(
bounds, india,
"zooms 0-8 hold planet tiles, so the bbox understates coverage"
);
assert!((bounds.0 + 180.0).abs() < 1e-9 && (bounds.2 - 180.0).abs() < 1e-9);
}
#[test]
fn covered_bounds_is_the_bbox_when_the_switch_equals_min_zoom() {
let india = (68.0, 6.5, 97.5, 37.2);
let spec = PyramidSpec {
min_zoom: 9,
max_zoom: 12,
bbox: Some(india),
bbox_min_zoom: 9,
};
assert_eq!(
spec.covered_bounds(),
india,
"no zoom sits below the switch, so coverage really is bbox-only"
);
}
#[test]
fn tiles_iterate_shallowest_zoom_first() {
let spec = PyramidSpec {
min_zoom: 1,
max_zoom: 2,
bbox: None,
bbox_min_zoom: 0,
};
let tiles = spec.tiles();
assert_eq!(tiles.first().map(|t| t.z), Some(1));
assert_eq!(tiles.last().map(|t| t.z), Some(2));
let boundary = tiles.iter().position(|t| t.z == 2).expect("z2 present");
assert_eq!(boundary, 4, "all four z1 tiles precede z2");
}
}