Skip to main content

mln_style/
tile_id.rs

1//! The canonical tile id: a tile of the tile pyramid, by zoom level and
2//! column and row.
3
4// Arithmetic on the coordinates wraps, and shifts mask their count, so an id
5// outside the pyramid gives a wrong id rather than a panic; `new` checks the
6// id in debug builds. The derived `Ord` compares z, then x, then y.
7
8use std::fmt;
9
10/// Has integer z/x/y coordinates. All tiles must be derived from 0/0/0 (no
11/// tiles outside of the main tile pyramid). Used for requesting data;
12/// represents data tiles that exist out there. z is never larger than the
13/// source's maxzoom.
14#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, mln_wire::Wire)]
15pub struct CanonicalTileID {
16    pub z: u8,
17    pub x: u32,
18    pub y: u32,
19}
20
21impl CanonicalTileID {
22    #[must_use]
23    pub fn new(z: u8, x: u32, y: u32) -> Self {
24        debug_assert!(z <= 32);
25        debug_assert!(u64::from(x) < (1u64 << z));
26        debug_assert!(u64::from(y) < (1u64 << z));
27        CanonicalTileID { z, x, y }
28    }
29
30    #[must_use]
31    pub fn is_child_of(&self, parent: &CanonicalTileID) -> bool {
32        // Every tile is a child of 0/0/0, which a tile at z 32 couldn't shift
33        // its coordinates to.
34        parent.z == 0
35            || (parent.z < self.z
36                && parent.x == self.x.wrapping_shr(u32::from(self.z - parent.z))
37                && parent.y == self.y.wrapping_shr(u32::from(self.z - parent.z)))
38    }
39
40    /// The tile at zoom level `target_z` that holds this one, or the
41    /// top-left of the tiles this one holds there.
42    #[must_use]
43    pub fn scaled_to(&self, target_z: u8) -> CanonicalTileID {
44        if target_z <= self.z {
45            // parent or same
46            let shift = (i32::from(self.z) - i32::from(target_z)) as u32;
47            CanonicalTileID::new(target_z, self.x.wrapping_shr(shift), self.y.wrapping_shr(shift))
48        } else {
49            // child
50            let shift = (i32::from(target_z) - i32::from(self.z)) as u32;
51            CanonicalTileID::new(target_z, self.x.wrapping_shl(shift), self.y.wrapping_shl(shift))
52        }
53    }
54
55    #[must_use]
56    pub fn children(&self) -> [CanonicalTileID; 4] {
57        let child_z = self.z.wrapping_add(1);
58        let child_x = self.x.wrapping_mul(2);
59        let child_y = self.y.wrapping_mul(2);
60        [
61            CanonicalTileID::new(child_z, child_x, child_y),
62            CanonicalTileID::new(child_z, child_x, child_y.wrapping_add(1)),
63            CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y),
64            CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y.wrapping_add(1)),
65        ]
66    }
67}
68
69/// `z/x/y`.
70impl fmt::Display for CanonicalTileID {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        write!(f, "{}/{}/{}", self.z, self.x, self.y)
73    }
74}
75
76#[cfg(test)]
77#[path = "tile_id_test.rs"]
78mod tile_id_test;