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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// Copyright (c) Pirmin Kalberer. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//

//!Tile grids

use std::f64::consts;

/// Geographic extent
#[derive(PartialEq, Clone, Debug)]
pub struct Extent {
    pub minx: f64,
    pub miny: f64,
    pub maxx: f64,
    pub maxy: f64,
}

/// Min and max grid cell numbers
#[derive(PartialEq, Clone, Debug)]
pub struct ExtentInt {
    pub minx: u32,
    pub miny: u32,
    pub maxx: u32,
    pub maxy: u32,
}

// Max grid cell numbers
type CellIndex = (u32, u32);

/// Grid origin
#[derive(PartialEq, Debug)]
pub enum Origin {
    TopLeft,
    BottomLeft, //TopRight, BottomRight
}

/// Grid units
#[derive(PartialEq, Debug)]
pub enum Unit {
    Meters,
    Degrees,
    Feet,
}

/// Tile grid
#[derive(Debug)]
pub struct Grid {
    /// The width of an individual tile, in pixels.
    width: u16,
    /// The height of an individual tile, in pixels.
    height: u16,
    /// The geographical extent covered by the grid, in ground units (e.g. meters, degrees, feet, etc.).
    /// Must be specified as 4 floating point numbers ordered as minx, miny, maxx, maxy.
    /// The (minx,miny) point defines the origin of the grid, i.e. the pixel at the bottom left of the
    /// bottom-left most tile is always placed on the (minx,miny) geographical point.
    /// The (maxx,maxy) point is used to determine how many tiles there are for each zoom level.
    pub extent: Extent,
    /// Spatial reference system (PostGIS SRID).
    pub srid: i32,
    /// Grid units
    pub units: Unit,
    /// This is a list of resolutions for each of the zoom levels defined by the grid.
    /// This must be supplied as a list of positive floating point values, ordered from largest to smallest.
    /// The largest value will correspond to the grid’s zoom level 0. Resolutions
    /// are expressed in “units-per-pixel”,
    /// depending on the unit used by the grid (e.g. resolutions are in meters per
    /// pixel for most grids used in webmapping).
    resolutions: Vec<f64>,
    /// maxx/maxy for each resolution
    level_max: Vec<CellIndex>,
    /// Grid origin
    pub origin: Origin,
}

impl Grid {
    /// WGS84 grid
    pub fn wgs84() -> Grid {
        Grid::new(
            256,
            256,
            Extent {
                minx: -180.0,
                miny: -90.0,
                maxx: 180.0,
                maxy: 90.0,
            },
            4326,
            Unit::Degrees,
            vec![
                0.703125000000000,
                0.351562500000000,
                0.175781250000000,
                8.78906250000000e-2,
                4.39453125000000e-2,
                2.19726562500000e-2,
                1.09863281250000e-2,
                5.49316406250000e-3,
                2.74658203125000e-3,
                1.37329101562500e-3,
                6.86645507812500e-4,
                3.43322753906250e-4,
                1.71661376953125e-4,
                8.58306884765625e-5,
                4.29153442382812e-5,
                2.14576721191406e-5,
                1.07288360595703e-5,
                5.36441802978516e-6,
            ],
            Origin::BottomLeft,
        )
    }

    /// Web Mercator grid (Google maps compatible)
    pub fn web_mercator() -> Grid {
        Grid::new(
            256,
            256,
            Extent {
                minx: -20037508.3427892480,
                miny: -20037508.3427892480,
                maxx: 20037508.3427892480,
                maxy: 20037508.3427892480,
            },
            3857,
            Unit::Meters,
            // for calculation see fn test_resolutions
            vec![
                156543.0339280410,
                78271.5169640205,
                39135.75848201025,
                19567.879241005125,
                9783.939620502562,
                4891.969810251281,
                2445.9849051256406,
                1222.9924525628203,
                611.4962262814101,
                305.7481131407051,
                152.87405657035254,
                76.43702828517627,
                38.218514142588134,
                19.109257071294067,
                9.554628535647034,
                4.777314267823517,
                2.3886571339117584,
                1.1943285669558792,
                0.5971642834779396,
                0.2985821417389698,
                0.1492910708694849,
                0.07464553543474245,
                0.037322767717371225,
            ],
            Origin::BottomLeft,
        )
    }

    pub fn new(
        width: u16,
        height: u16,
        extent: Extent,
        srid: i32,
        units: Unit,
        resolutions: Vec<f64>,
        origin: Origin,
    ) -> Grid {
        let mut grid = Grid {
            width,
            height,
            extent,
            srid,
            units,
            resolutions,
            origin,
            level_max: Vec::new(),
        };
        grid.level_max = grid.level_max();
        grid
    }
    pub fn nlevels(&self) -> u8 {
        self.resolutions.len() as u8
    }
    pub fn maxzoom(&self) -> u8 {
        self.nlevels() - 1
    }
    pub fn pixel_width(&self, zoom: u8) -> f64 {
        const METERS_PER_DEGREE: f64 = 6378137.0 * 2.0 * consts::PI / 360.0;
        match self.units {
            Unit::Meters => self.resolutions[zoom as usize],
            Unit::Degrees => self.resolutions[zoom as usize] * METERS_PER_DEGREE,
            Unit::Feet => self.resolutions[zoom as usize] * 0.3048,
        }
    }
    pub fn scale_denominator(&self, zoom: u8) -> f64 {
        const PIXEL_SCREEN_WIDTH: f64 = 0.00028;
        // https://github.com/mapnik/mapnik/wiki/ScaleAndPpi#scale-denominator
        // Mapnik calculates it's default at about 90.7 PPI, which originates from an assumed standard pixel size
        // of 0.28 millimeters as defined by the OGC (Open Geospatial Consortium) SLD (Styled Layer Descriptor) Specification.
        self.pixel_width(zoom) / PIXEL_SCREEN_WIDTH
    }
    /// Extent of a given tile in the grid given its x, y, and z in TMS adressing scheme
    pub fn tile_extent(&self, xtile: u32, ytile: u32, zoom: u8) -> Extent {
        // based on mapcache_grid_get_tile_extent
        let res = self.resolutions[zoom as usize];
        let tile_sx = self.width as f64;
        let tile_sy = self.height as f64;
        match self.origin {
            Origin::BottomLeft => Extent {
                minx: self.extent.minx + (res * xtile as f64 * tile_sx),
                miny: self.extent.miny + (res * ytile as f64 * tile_sy),
                maxx: self.extent.minx + (res * (xtile + 1) as f64 * tile_sx),
                maxy: self.extent.miny + (res * (ytile + 1) as f64 * tile_sy),
            },
            Origin::TopLeft => Extent {
                minx: self.extent.minx + (res * xtile as f64 * tile_sx),
                miny: self.extent.maxy - (res * (ytile + 1) as f64 * tile_sy),
                maxx: self.extent.minx + (res * (xtile + 1) as f64 * tile_sx),
                maxy: self.extent.maxy - (res * ytile as f64 * tile_sy),
            },
        }
    }
    /// reverse y tile for XYZ adressing scheme
    pub fn ytile_from_xyz(&self, ytile: u32, zoom: u8) -> u32 {
        // y = maxy-ytile-1
        let maxy = self.level_max[zoom as usize].1;
        let y = maxy.saturating_sub(ytile).saturating_sub(1);
        y
    }
    /// Extent of a given tile in XYZ adressing scheme
    pub fn tile_extent_xyz(&self, xtile: u32, ytile: u32, zoom: u8) -> Extent {
        let y = self.ytile_from_xyz(ytile, zoom);
        self.tile_extent(xtile, y, zoom)
    }
    /// (maxx, maxy) of grid level
    pub(crate) fn level_limit(&self, zoom: u8) -> CellIndex {
        let res = self.resolutions[zoom as usize];
        let unitheight = self.height as f64 * res;
        let unitwidth = self.width as f64 * res;

        let maxy =
            ((self.extent.maxy - self.extent.miny - 0.01 * unitheight) / unitheight).ceil() as u32;
        let maxx =
            ((self.extent.maxx - self.extent.minx - 0.01 * unitwidth) / unitwidth).ceil() as u32;
        (maxx, maxy)
    }
    /// (maxx, maxy) of all grid levels
    fn level_max(&self) -> Vec<CellIndex> {
        (0..self.nlevels())
            .map(|zoom| self.level_limit(zoom))
            .collect()
    }
    /// Tile index limits covering extent
    pub fn tile_limits(&self, extent: Extent, tolerance: i32) -> Vec<ExtentInt> {
        // Based on mapcache_grid_compute_limits
        const EPSILON: f64 = 0.0000001;
        (0..self.nlevels())
            .map(|i| {
                let res = self.resolutions[i as usize];
                let unitheight = self.height as f64 * res;
                let unitwidth = self.width as f64 * res;
                let (level_maxx, level_maxy) = self.level_max[i as usize];

                let (mut minx, mut maxx, mut miny, mut maxy) = match self.origin {
                    Origin::BottomLeft => (
                        (((extent.minx - self.extent.minx) / unitwidth + EPSILON).floor() as i32)
                            - tolerance,
                        (((extent.maxx - self.extent.minx) / unitwidth - EPSILON).ceil() as i32)
                            + tolerance,
                        (((extent.miny - self.extent.miny) / unitheight + EPSILON).floor() as i32)
                            - tolerance,
                        (((extent.maxy - self.extent.miny) / unitheight - EPSILON).ceil() as i32)
                            + tolerance,
                    ),
                    Origin::TopLeft => (
                        (((extent.minx - self.extent.minx) / unitwidth + EPSILON).floor() as i32)
                            - tolerance,
                        (((extent.maxx - self.extent.minx) / unitwidth - EPSILON).ceil() as i32)
                            + tolerance,
                        (((self.extent.maxy - extent.maxy) / unitheight + EPSILON).floor() as i32)
                            - tolerance,
                        (((self.extent.maxy - extent.miny) / unitheight - EPSILON).ceil() as i32)
                            + tolerance,
                    ),
                };

                // to avoid requesting out-of-range tiles
                if minx < 0 {
                    minx = 0;
                }
                if maxx > level_maxx as i32 {
                    maxx = level_maxx as i32
                };
                if miny < 0 {
                    miny = 0
                };
                if maxy > level_maxy as i32 {
                    maxy = level_maxy as i32
                };

                ExtentInt {
                    minx: minx as u32,
                    maxx: maxx as u32,
                    miny: miny as u32,
                    maxy: maxy as u32,
                }
            })
            .collect()
    }
}

/// Returns the Spherical Mercator (x, y) in meters
fn lonlat_to_merc(lon: f64, lat: f64) -> (f64, f64) {
    // from mod web_mercator in grid_test
    //lng, lat = truncate_lnglat(lng, lat)
    let x = 6378137.0 * lon.to_radians();
    let y = 6378137.0 * ((consts::PI * 0.25) + (0.5 * lat.to_radians())).tan().ln();
    (x, y)
}

/// Projected extent
pub fn extent_to_merc(extent: &Extent) -> Extent {
    let (minx, miny) = lonlat_to_merc(extent.minx, extent.miny);
    let (maxx, maxy) = lonlat_to_merc(extent.maxx, extent.maxy);
    Extent {
        minx,
        miny,
        maxx,
        maxy,
    }
}