1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// mapgrid.rs
//
// Copyright (c) 2019-2021  Minnesota Department of Transportation
//
//! TileId and MapGrid structs.
//!
use crate::error::{Error, Result};
use num_traits::FromPrimitive;
use pointy::{BBox, Float, Pt, Transform};
use std::fmt;

/// Web Mercator map constants
pub trait MapConst {
    /// Half size of map (meters)
    const HALF_SIZE_M: Self;
}

impl MapConst for f32 {
    const HALF_SIZE_M: Self = 20_037_508.342_789_248;
}

impl MapConst for f64 {
    const HALF_SIZE_M: Self = 20_037_508.342_789_248;
}

/// A tile ID identifies a tile on a map grid at a specific zoom level.
///
/// It uses XYZ addressing, with X increasing from west to east and Y increasing
/// from north to south.  The X and Y values can range from 0 to
/// 2<sup>Z</sup>-1.
#[derive(Clone, Copy, Debug)]
pub struct TileId {
    x: u32, // not public to prevent invalid values being created
    y: u32,
    z: u32,
}

/// A map grid is used to address [tile]s on a map.
///
/// The grid should be in projected coördinates.  Use `default()` for
/// [Web Mercator].
///
/// [tile]: struct.Tile.html
/// [Web Mercator]: https://en.wikipedia.org/wiki/Web_Mercator_projection
#[derive(Clone, Debug)]
pub struct MapGrid<F>
where
    F: Float,
{
    /// Spatial reference ID
    srid: i32,

    /// Bounding box
    bbox: BBox<F>,
}

impl TileId {
    /// Get the X value.
    pub fn x(&self) -> u32 {
        self.x
    }

    /// Get the Y value.
    pub fn y(&self) -> u32 {
        self.y
    }

    /// Get the Z (zoom) value.
    pub fn z(&self) -> u32 {
        self.z
    }
}

impl fmt::Display for TileId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}/{}/{}", self.z, self.x, self.y)
    }
}

impl TileId {
    /// Create a new TildId.
    ///
    /// If invalid, returns [Error::InvalidTid](enum.Error.html).
    pub fn new(x: u32, y: u32, z: u32) -> Result<Self> {
        TileId::check_valid(x, y, z)?;
        Ok(TileId { x, y, z })
    }

    /// Check whether a tile ID is valid.
    fn check_valid(x: u32, y: u32, z: u32) -> Result<()> {
        if z > 31 {
            return Err(Error::InvalidTid());
        }
        let s = 1 << z;
        if x < s && y < s {
            Ok(())
        } else {
            Err(Error::InvalidTid())
        }
    }
}

impl<F> Default for MapGrid<F>
where
    F: Float + MapConst,
{
    fn default() -> Self {
        const WEB_MERCATOR_SRID: i32 = 3857;
        let srid = WEB_MERCATOR_SRID;
        let p0 = Pt::new(-F::HALF_SIZE_M, -F::HALF_SIZE_M);
        let p1 = Pt::new(F::HALF_SIZE_M, F::HALF_SIZE_M);
        let bbox = BBox::from((p0, p1));
        Self { srid, bbox }
    }
}

impl<F> MapGrid<F>
where
    F: Float + FromPrimitive,
{
    /// Create a new map grid.
    ///
    /// * `srid` Spatial reference ID.
    /// * `bbox` Bounding box.
    pub fn new(srid: i32, bbox: BBox<F>) -> Self {
        MapGrid { srid, bbox }
    }

    /// Get the spatial reference ID.
    pub fn srid(&self) -> i32 {
        self.srid
    }

    /// Get the bounding box of the grid.
    pub fn bbox(&self) -> BBox<F> {
        self.bbox
    }

    /// Get the bounding box of a tile ID.
    pub fn tile_bbox(&self, tid: TileId) -> BBox<F> {
        let tx = self.bbox.x_min(); // west edge
        let ty = self.bbox.y_max(); // north edge
        let tz = zoom_scale(tid.z);
        let sx = self.bbox.x_span() * tz;
        let sy = self.bbox.y_span() * tz;
        let t = Transform::with_scale(sx, -sy).translate(tx, ty);
        let tidx = F::from_u32(tid.x).unwrap();
        let tidy = F::from_u32(tid.y).unwrap();
        let p0 = t * Pt::new(tidx, tidy);
        let p1 = t * Pt::new(tidx + F::one(), tidy + F::one());
        BBox::from((p0, p1))
    }

    /// Get the transform to coördinates in 0 to 1 range.
    pub fn tile_transform(&self, tid: TileId) -> Transform<F> {
        let tx = self.bbox.x_min(); // west edge
        let ty = self.bbox.y_max(); // north edge
        let tz = F::from_u32(1 << tid.z).unwrap();
        let sx = tz / self.bbox.x_span();
        let sy = tz / self.bbox.y_span();
        let tidx = F::from_u32(tid.x).unwrap();
        let tidy = F::from_u32(tid.y).unwrap();
        Transform::with_translate(-tx, -ty)
            .scale(sx, -sy)
            .translate(-tidx, -tidy)
    }
}

/// Calculate scales at one zoom level.
fn zoom_scale<F>(zoom: u32) -> F
where
    F: Float + FromPrimitive,
{
    F::one() / F::from_u32(1 << zoom).unwrap()
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_tile_bbox() {
        let g = MapGrid::<f64>::default();
        let tid = TileId::new(0, 0, 0).unwrap();
        let b = g.tile_bbox(tid);
        assert_eq!(b.x_min(), -20037508.3427892480);
        assert_eq!(b.x_max(), 20037508.3427892480);
        assert_eq!(b.y_min(), -20037508.3427892480);
        assert_eq!(b.y_max(), 20037508.3427892480);

        let tid = TileId::new(0, 0, 1).unwrap();
        let b = g.tile_bbox(tid);
        assert_eq!(b.x_min(), -20037508.3427892480);
        assert_eq!(b.x_max(), 0.0);
        assert_eq!(b.y_min(), 0.0);
        assert_eq!(b.y_max(), 20037508.3427892480);

        let tid = TileId::new(1, 1, 1).unwrap();
        let b = g.tile_bbox(tid);
        assert_eq!(b.x_min(), 0.0);
        assert_eq!(b.x_max(), 20037508.3427892480);
        assert_eq!(b.y_min(), -20037508.3427892480);
        assert_eq!(b.y_max(), 0.0);

        let tid = TileId::new(246, 368, 10).unwrap();
        let b = g.tile_bbox(tid);
        assert_eq!(b.x_min(), -10410111.756214727);
        assert_eq!(b.x_max(), -10370975.997732716);
        assert_eq!(b.y_min(), 5596413.462927466);
        assert_eq!(b.y_max(), 5635549.221409475);
    }

    #[test]
    fn test_tile_transform() {
        let g = MapGrid::default();
        let tid = TileId::new(0, 0, 0).unwrap();
        let t = g.tile_transform(tid);
        assert_eq!(
            Pt::new(0.0, 0.0),
            t * Pt::new(-20037508.3427892480, 20037508.3427892480)
        );
        assert_eq!(
            Pt::new(1.0, 1.0),
            t * Pt::new(20037508.3427892480, -20037508.3427892480)
        );

        let tid = TileId::new(0, 0, 1).unwrap();
        let t = g.tile_transform(tid);
        assert_eq!(
            Pt::new(0.0, 0.0),
            t * Pt::new(-20037508.3427892480, 20037508.3427892480)
        );
        assert_eq!(Pt::new(1.0, 1.0), t * Pt::new(0.0, 0.0));

        let tid = TileId::new(1, 1, 1).unwrap();
        let t = g.tile_transform(tid);
        assert_eq!(Pt::new(0.0, 0.0), t * Pt::new(0.0, 0.0));
        assert_eq!(
            Pt::new(1.0, 1.0),
            t * Pt::new(20037508.3427892480, -20037508.3427892480)
        );

        let tid = TileId::new(246, 368, 10).unwrap();
        let t = g.tile_transform(tid);
        assert_eq!(
            Pt::new(0.0, 0.0),
            t * Pt::new(-10410111.756214727, 5635549.221409475)
        );
        assert_eq!(
            Pt::new(1.0, 0.9999999999999716),
            t * Pt::new(-10370975.997732716, 5596413.462927466)
        );
    }
}