Skip to main content

martin_tile_utils/
lib.rs

1#![doc = include_str!("../README.md")]
2
3// This code was partially adapted from https://github.com/maplibre/mbtileserver-rs
4// project originally written by Kaveh Karimi and licensed under MIT OR Apache-2.0
5
6use std::f64::consts::PI;
7use std::fmt::{Display, Formatter};
8
9/// circumference of the earth in meters
10pub const EARTH_CIRCUMFERENCE: f64 = 40_075_016.685_578_5;
11/// circumference of the earth in degrees
12pub const EARTH_CIRCUMFERENCE_DEGREES: u32 = 360;
13
14/// radius of the earth in meters
15pub const EARTH_RADIUS: f64 = EARTH_CIRCUMFERENCE / 2.0 / PI;
16
17pub const MAX_ZOOM: u8 = 30;
18
19mod decoders;
20pub use decoders::*;
21mod rectangle;
22pub use rectangle::{TileRect, append_rect};
23
24#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
25pub struct TileCoord {
26    pub z: u8,
27    pub x: u32,
28    pub y: u32,
29}
30
31pub type TileData = Vec<u8>;
32pub type Tile = (TileCoord, Option<TileData>);
33
34impl Display for TileCoord {
35    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36        if f.alternate() {
37            write!(f, "{}/{}/{}", self.z, self.x, self.y)
38        } else {
39            write!(f, "{},{},{}", self.z, self.x, self.y)
40        }
41    }
42}
43
44impl TileCoord {
45    /// Checks provided coordinates for validity
46    /// before constructing [`TileCoord`] instance.
47    ///
48    /// Check [`Self::new_unchecked`] if you are sure that your inputs are possible.
49    #[must_use]
50    pub fn new_checked(z: u8, x: u32, y: u32) -> Option<Self> {
51        Self::is_possible_on_zoom_level(z, x, y).then_some(Self { z, x, y })
52    }
53
54    /// Constructs [`TileCoord`] instance from arguments without checking that the tiles can exist.
55    ///
56    /// Check [`Self::new_checked`] if you are unsure if your inputs are possible.
57    #[must_use]
58    pub fn new_unchecked(z: u8, x: u32, y: u32) -> Self {
59        Self { z, x, y }
60    }
61
62    /// Checks that zoom `z` is plausibily small and `x`/`y` is possible on said zoom level
63    #[must_use]
64    pub fn is_possible_on_zoom_level(z: u8, x: u32, y: u32) -> bool {
65        if z > MAX_ZOOM {
66            return false;
67        }
68
69        let side_len = 1_u32 << z;
70        x < side_len && y < side_len
71    }
72}
73
74#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
75pub enum Format {
76    Gif,
77    Jpeg,
78    Json,
79    Mvt,
80    Mlt,
81    Png,
82    Webp,
83    Avif,
84}
85
86impl Format {
87    /// All image formats.
88    pub const IMAGE_FORMATS: &[Self] = &[Self::Gif, Self::Jpeg, Self::Png, Self::Webp, Self::Avif];
89
90    #[must_use]
91    pub fn parse(value: &str) -> Option<Self> {
92        Some(match value.to_ascii_lowercase().as_str() {
93            "gif" => Self::Gif,
94            "jpg" | "jpeg" => Self::Jpeg,
95            "json" => Self::Json,
96            "pbf" | "mvt" => Self::Mvt,
97            "mlt" => Self::Mlt,
98            "png" => Self::Png,
99            "webp" => Self::Webp,
100            "avif" => Self::Avif,
101            _ => None?,
102        })
103    }
104
105    /// Get the `format` value as it should be stored in the `MBTiles` metadata table
106    #[must_use]
107    pub fn metadata_format_value(self) -> &'static str {
108        match self {
109            Self::Gif => "gif",
110            Self::Jpeg => "jpeg",
111            Self::Json => "json",
112            // QGIS uses `pbf` instead of `mvt` for some reason
113            Self::Mvt => "pbf",
114            Self::Mlt => "mlt",
115            Self::Png => "png",
116            Self::Webp => "webp",
117            Self::Avif => "avif",
118        }
119    }
120
121    #[must_use]
122    pub fn content_type(&self) -> &str {
123        match *self {
124            Self::Gif => "image/gif",
125            Self::Jpeg => "image/jpeg",
126            Self::Json => "application/json",
127            Self::Mvt => "application/x-protobuf",
128            Self::Mlt => "application/vnd.maplibre-tile",
129            Self::Png => "image/png",
130            Self::Webp => "image/webp",
131            Self::Avif => "image/avif",
132        }
133    }
134
135    /// Parse a content type string back to a `Format`.
136    #[must_use]
137    pub fn from_content_type(supertype: &str, subtype: &str) -> Option<Self> {
138        Some(match (supertype, subtype) {
139            ("image", "gif") => Self::Gif,
140            ("image", "jpeg") => Self::Jpeg,
141            ("application", "json") => Self::Json,
142            ("application", "x-protobuf" | "vnd.mapbox-vector-tile") => Self::Mvt,
143            ("application", "vnd.maplibre-vector-tile" | "vnd.maplibre-tile") => Self::Mlt,
144            ("image", "png") => Self::Png,
145            ("image", "webp") => Self::Webp,
146            ("image", "avif") => Self::Avif,
147            _ => None?,
148        })
149    }
150
151    #[must_use]
152    pub fn is_detectable(self) -> bool {
153        match self {
154            Self::Png
155            | Self::Jpeg
156            | Self::Gif
157            | Self::Webp
158            | Self::Avif
159            | Self::Json
160            | Self::Mlt => true,
161            Self::Mvt => false,
162        }
163    }
164}
165
166impl Display for Format {
167    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
168        f.write_str(match *self {
169            Self::Gif => "gif",
170            Self::Jpeg => "jpeg",
171            Self::Json => "json",
172            Self::Mvt => "mvt",
173            Self::Mlt => "mlt",
174            Self::Png => "png",
175            Self::Webp => "webp",
176            Self::Avif => "avif",
177        })
178    }
179}
180
181#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
182pub enum Encoding {
183    /// Data is not compressed, but it can be
184    Uncompressed = 0b0000_0000,
185    /// Some formats like JPEG and PNG are already compressed
186    Internal = 0b0000_0001,
187    Gzip = 0b0000_0010,
188    Zlib = 0b0000_0100,
189    Brotli = 0b0000_1000,
190    Zstd = 0b0001_0000,
191}
192
193impl Encoding {
194    /// Parse the encoding from common names if they match
195    #[must_use]
196    pub fn parse(value: &str) -> Option<Self> {
197        Some(match value.to_ascii_lowercase().as_str() {
198            "none" | "identity" => Self::Uncompressed,
199            "gzip" => Self::Gzip,
200            "deflate" | "zlib" => Self::Zlib,
201            "br" | "brotli" => Self::Brotli,
202            "zstd" => Self::Zstd,
203            _ => None?,
204        })
205    }
206
207    /// Returns `None` for [`Encoding::Uncompressed`] and [`Encoding::Internal`]:
208    /// absence of the `compression` key in the metadata table means no external encoding.
209    #[must_use]
210    pub fn compression(self) -> Option<&'static str> {
211        match self {
212            Self::Uncompressed | Self::Internal => None,
213            Self::Gzip => Some("gzip"),
214            Self::Zlib => Some("deflate"),
215            Self::Brotli => Some("br"),
216            Self::Zstd => Some("zstd"),
217        }
218    }
219
220    #[must_use]
221    pub fn is_encoded(self) -> bool {
222        match self {
223            Self::Uncompressed | Self::Internal => false,
224            Self::Gzip | Self::Zlib | Self::Brotli | Self::Zstd => true,
225        }
226    }
227}
228
229#[derive(Clone, Copy, Debug, PartialEq, Eq)]
230pub struct TileInfo {
231    pub format: Format,
232    pub encoding: Encoding,
233}
234
235impl TileInfo {
236    #[must_use]
237    pub fn new(format: Format, encoding: Encoding) -> Self {
238        Self { format, encoding }
239    }
240
241    /// Try to figure out the format and encoding of the raw tile data
242    #[must_use]
243    pub fn detect(value: &[u8]) -> Self {
244        // Try GZIP decompression
245        if value.starts_with(b"\x1f\x8b") {
246            if let Ok(decompressed) = decode_gzip(value) {
247                let inner_format = Self::detect_vectorish_format(&decompressed);
248                return Self::new(inner_format, Encoding::Gzip);
249            }
250            // If decompression fails or format is unknown, assume MVT
251            return Self::new(Format::Mvt, Encoding::Gzip);
252        }
253
254        // Try Zlib decompression
255        if value.starts_with(b"\x78\x9c") {
256            if let Ok(decompressed) = decode_zlib(value) {
257                let inner_format = Self::detect_vectorish_format(&decompressed);
258                return Self::new(inner_format, Encoding::Zlib);
259            }
260            // If decompression fails or format is unknown, assume MVT
261            return Self::new(Format::Mvt, Encoding::Zlib);
262        }
263        if let Some(raster_format) = Self::detect_raster_formats(value) {
264            Self::new(raster_format, Encoding::Internal)
265        } else {
266            let inner_format = Self::detect_vectorish_format(value);
267            Self::new(inner_format, Encoding::Uncompressed)
268        }
269    }
270
271    /// Fast-path detection without decompression
272    #[must_use]
273    fn detect_raster_formats(value: &[u8]) -> Option<Format> {
274        match value {
275            v if v.starts_with(b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") => Some(Format::Png),
276            v if v.starts_with(b"\x47\x49\x46\x38\x39\x61") => Some(Format::Gif),
277            v if v.starts_with(b"\xFF\xD8\xFF") => Some(Format::Jpeg),
278            v if v.starts_with(b"RIFF") && v.len() > 8 && v[8..].starts_with(b"WEBP") => {
279                Some(Format::Webp)
280            }
281            _ => None,
282        }
283    }
284
285    /// Detect the format of vector (or json) data after decompression
286    #[must_use]
287    fn detect_vectorish_format(value: &[u8]) -> Format {
288        match value {
289            v if decode_7bit_length_and_tag(v, &[0x1]).is_ok() => Format::Mlt,
290            v if is_valid_json(v) => Format::Json,
291            // If we can't detect the format, we assume MVT.
292            // Reasoning:
293            //- it's the most common format and
294            //- we don't have a detector for it
295            _ => Format::Mvt,
296        }
297    }
298
299    #[must_use]
300    pub fn encoding(self, encoding: Encoding) -> Self {
301        Self { encoding, ..self }
302    }
303}
304
305impl From<Format> for TileInfo {
306    fn from(format: Format) -> Self {
307        Self::new(
308            format,
309            match format {
310                Format::Mlt
311                | Format::Png
312                | Format::Jpeg
313                | Format::Webp
314                | Format::Gif
315                | Format::Avif => Encoding::Internal,
316                Format::Mvt | Format::Json => Encoding::Uncompressed,
317            },
318        )
319    }
320}
321
322impl Display for TileInfo {
323    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
324        write!(f, "{}", self.format.content_type())?;
325        if let Some(encoding) = self.encoding.compression() {
326            write!(f, "; encoding={encoding}")?;
327        } else if self.encoding != Encoding::Uncompressed {
328            f.write_str("; uncompressed")?;
329        }
330        Ok(())
331    }
332}
333
334#[derive(thiserror::Error, Debug, PartialEq, Eq)]
335enum SevenBitDecodingError {
336    /// Expected a tag, but got nothing
337    #[error("Expected a tag, but got nothing")]
338    TruncatedTag,
339    /// The size of the tile is too large to be decoded
340    #[error("The size of the tile is too large to be decoded")]
341    SizeOverflow,
342    /// The size of the tile is lower than the number of bytes for the size and tag
343    #[error("The size of the tile is lower than the number of bytes for the size and tag")]
344    SizeUnderflow,
345    /// Expected a size, but got nothing
346    #[error("Expected a size, but got nothing")]
347    TruncatedSize,
348    /// Expected data according to the size, but got nothing
349    #[error("Expected {0} bytes of data in layer according to the size, but got only {1}")]
350    TruncatedData(u64, u64),
351    /// Got unexpected tag
352    #[error("Got tag {0} instead of the expected")]
353    UnexpectedTag(u8),
354}
355
356/// Tries to validate that the tile consists of a valid concatenation of (`size_7_bit`, `one_of_expected_version`, `data`)
357fn decode_7bit_length_and_tag(tile: &[u8], versions: &[u8]) -> Result<(), SevenBitDecodingError> {
358    if tile.is_empty() {
359        return Err(SevenBitDecodingError::TruncatedSize);
360    }
361    let mut tile_iter = tile.iter().peekable();
362    while tile_iter.peek().is_some() {
363        // need to parse size
364        let mut size = 0_u64;
365        let mut header_bit_count = 0_u64;
366        loop {
367            header_bit_count += 1;
368            let Some(b) = tile_iter.next() else {
369                return Err(SevenBitDecodingError::TruncatedSize);
370            };
371            if header_bit_count * 7 + 8 > 64 {
372                return Err(SevenBitDecodingError::SizeOverflow);
373            }
374            // decode size
375            size <<= 7;
376            let seven_bit_mask = !0x80;
377            size |= u64::from(*b & seven_bit_mask);
378            // 0 => no further size
379            if b & 0x80 == 0 {
380                // need to check tag
381                header_bit_count += 1;
382                let Some(tag) = tile_iter.next() else {
383                    return Err(SevenBitDecodingError::TruncatedTag);
384                };
385                if !versions.contains(tag) {
386                    return Err(SevenBitDecodingError::UnexpectedTag(*tag));
387                }
388                // need to check data-length
389                let payload_len = size
390                    .checked_sub(header_bit_count)
391                    .ok_or(SevenBitDecodingError::SizeUnderflow)?;
392                for i in 0..payload_len {
393                    if tile_iter.next().is_none() {
394                        return Err(SevenBitDecodingError::TruncatedData(payload_len, i));
395                    }
396                }
397                break;
398            }
399        }
400    }
401    Ok(())
402}
403
404/// Detects if the given tile is a valid JSON tile.
405///
406/// The check for a dictionary is used to speed up the validation process.
407fn is_valid_json(tile: &[u8]) -> bool {
408    tile.starts_with(b"{")
409        && tile.ends_with(b"}")
410        && serde_json::from_slice::<serde::de::IgnoredAny>(tile).is_ok()
411}
412
413/// Convert longitude and latitude to a tile (x,y) coordinates for a given zoom
414#[must_use]
415#[expect(clippy::cast_possible_truncation)]
416#[expect(clippy::cast_sign_loss)]
417pub fn tile_index(lng: f64, lat: f64, zoom: u8) -> (u32, u32) {
418    let tile_size = EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom);
419    let (x, y) = wgs84_to_webmercator(lng, lat);
420    let col = (((x - (EARTH_CIRCUMFERENCE * -0.5)).abs() / tile_size) as u32).min((1 << zoom) - 1);
421    let row = ((((EARTH_CIRCUMFERENCE * 0.5) - y).abs() / tile_size) as u32).min((1 << zoom) - 1);
422    (col, row)
423}
424
425/// Convert min/max XYZ tile coordinates to a bounding box values.
426///
427/// The result is `[min_lng, min_lat, max_lng, max_lat]`
428///
429/// # Panics
430/// Panics if `zoom` is greater than [`MAX_ZOOM`].
431#[must_use]
432pub fn xyz_to_bbox(zoom: u8, min_x: u32, min_y: u32, max_x: u32, max_y: u32) -> [f64; 4] {
433    assert!(zoom <= MAX_ZOOM, "zoom {zoom} must be <= {MAX_ZOOM}");
434
435    let tile_length = EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom);
436
437    let left_down_bbox = tile_bbox(min_x, max_y, tile_length);
438    let right_top_bbox = tile_bbox(max_x, min_y, tile_length);
439
440    let (min_lng, min_lat) = webmercator_to_wgs84(left_down_bbox[0], left_down_bbox[1]);
441    let (max_lng, max_lat) = webmercator_to_wgs84(right_top_bbox[2], right_top_bbox[3]);
442    [min_lng, min_lat, max_lng, max_lat]
443}
444
445#[expect(clippy::cast_lossless)]
446fn tile_bbox(x: u32, y: u32, tile_length: f64) -> [f64; 4] {
447    let min_x = EARTH_CIRCUMFERENCE * -0.5 + x as f64 * tile_length;
448    let max_y = EARTH_CIRCUMFERENCE * 0.5 - y as f64 * tile_length;
449
450    [min_x, max_y - tile_length, min_x + tile_length, max_y]
451}
452
453/// Convert bounding box to a tile box `(min_x, min_y, max_x, max_y)` for a given zoom
454#[must_use]
455pub fn bbox_to_xyz(left: f64, bottom: f64, right: f64, top: f64, zoom: u8) -> (u32, u32, u32, u32) {
456    let (min_col, min_row) = tile_index(left, top, zoom);
457    let (max_col, max_row) = tile_index(right, bottom, zoom);
458    (min_col, min_row, max_col, max_row)
459}
460
461/// Compute precision of a zoom level, i.e. how many decimal digits of the longitude and latitude are relevant
462#[must_use]
463#[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
464pub fn get_zoom_precision(zoom: u8) -> usize {
465    assert!(zoom <= MAX_ZOOM, "zoom {zoom} must be <= {MAX_ZOOM}");
466    let lng_delta = webmercator_to_wgs84(EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom), 0.0).0;
467    let log = lng_delta.log10() - 0.5;
468    if log > 0.0 { 0 } else { -log.ceil() as usize }
469}
470
471/// transform [`WebMercator`](https://epsg.io/3857) to [WGS84](https://epsg.io/4326)
472// from https://github.com/Esri/arcgis-osm-editor/blob/e4b9905c264aa22f8eeb657efd52b12cdebea69a/src/OSMWeb10_1/Utils/WebMercator.cs
473#[must_use]
474pub fn webmercator_to_wgs84(x: f64, y: f64) -> (f64, f64) {
475    let lng = (x / EARTH_RADIUS).to_degrees();
476    let lat = f64::atan(f64::sinh(y / EARTH_RADIUS)).to_degrees();
477    (lng, lat)
478}
479
480/// transform [WGS84](https://epsg.io/4326) to [`WebMercator`](https://epsg.io/3857)
481// from https://github.com/Esri/arcgis-osm-editor/blob/e4b9905c264aa22f8eeb657efd52b12cdebea69a/src/OSMWeb10_1/Utils/WebMercator.cs
482#[must_use]
483pub fn wgs84_to_webmercator(lon: f64, lat: f64) -> (f64, f64) {
484    let x = lon * PI / 180.0 * EARTH_RADIUS;
485
486    let y_sin = lat.to_radians().sin();
487    let y = EARTH_RADIUS / 2.0 * ((1.0 + y_sin) / (1.0 - y_sin)).ln();
488
489    (x, y)
490}
491
492#[cfg(test)]
493mod tests {
494    use approx::assert_relative_eq;
495    use rstest::rstest;
496
497    use super::*;
498
499    #[rstest]
500    #[case::png(
501        include_bytes!("../fixtures/world.png"),
502        TileInfo::new(Format::Png, Encoding::Internal)
503    )]
504    #[case::jpg(
505        include_bytes!("../fixtures/world.jpg"),
506        TileInfo::new(Format::Jpeg, Encoding::Internal)
507    )]
508    #[case::webp(
509        include_bytes!("../fixtures/dc.webp"),
510        TileInfo::new(Format::Webp, Encoding::Internal)
511    )]
512    #[case::json(
513        br#"{"foo":"bar"}"#,
514        TileInfo::new(Format::Json, Encoding::Uncompressed)
515    )]
516    // we have no way of knowing what is an MVT -> we just say it is out of the
517    // fact that it is not something else
518    #[case::invalid_webp_header(b"RIFF", TileInfo::new(Format::Mvt, Encoding::Uncompressed))]
519    fn test_data_format_detect(#[case] data: &[u8], #[case] expected: TileInfo) {
520        assert_eq!(TileInfo::detect(data), expected);
521    }
522
523    /// Test detection of compressed content (JSON, MLT, MVT)
524    #[test]
525    fn test_compressed_json_gzip() {
526        let json_data = br#"{"type":"FeatureCollection","features":[]}"#;
527        let compressed = encode_gzip(json_data).unwrap();
528        let result = TileInfo::detect(&compressed);
529        assert_eq!(result, TileInfo::new(Format::Json, Encoding::Gzip));
530    }
531
532    #[test]
533    fn test_compressed_json_zlib() {
534        use std::io::Write as _;
535
536        use flate2::write::ZlibEncoder;
537
538        let json_data = br#"{"type":"FeatureCollection","features":[]}"#;
539        let mut encoder = ZlibEncoder::new(Vec::new(), flate2::Compression::default());
540        encoder.write_all(json_data).unwrap();
541        let compressed = encoder.finish().unwrap();
542
543        let result = TileInfo::detect(&compressed);
544        assert_eq!(result, TileInfo::new(Format::Json, Encoding::Zlib));
545    }
546
547    #[test]
548    fn test_compressed_mlt_gzip() {
549        // MLT tile: length=2 (0x02), version=1 (0x01)
550        let mlt_data = &[0x02, 0x01];
551        let compressed = encode_gzip(mlt_data).unwrap();
552        let result = TileInfo::detect(&compressed);
553        assert_eq!(result, TileInfo::new(Format::Mlt, Encoding::Gzip));
554    }
555
556    #[test]
557    fn test_compressed_mlt_zlib() {
558        use std::io::Write as _;
559
560        use flate2::write::ZlibEncoder;
561
562        // MLT tile: length=5 (0x05), version=1 (0x01), plus some data
563        let mlt_data = &[0x05, 0x01, 0xaa, 0xbb, 0xcc];
564        let mut encoder = ZlibEncoder::new(Vec::new(), flate2::Compression::default());
565        encoder.write_all(mlt_data).unwrap();
566        let compressed = encoder.finish().unwrap();
567
568        let result = TileInfo::detect(&compressed);
569        assert_eq!(result, TileInfo::new(Format::Mlt, Encoding::Zlib));
570    }
571
572    #[test]
573    fn test_compressed_mvt_gzip_fallback() {
574        // Random data that doesn't match any known format => should be detected as MVT
575        let random_data = &[0x1a, 0x2b, 0x3c, 0x4d];
576        let compressed = encode_gzip(random_data).unwrap();
577        let result = TileInfo::detect(&compressed);
578        assert_eq!(result, TileInfo::new(Format::Mvt, Encoding::Gzip));
579    }
580
581    #[test]
582    fn test_compressed_mvt_zlib_fallback() {
583        use std::io::Write as _;
584
585        use flate2::write::ZlibEncoder;
586
587        // Random data that doesn't match any known format => should be detected as MVT
588        let random_data = &[0xaa, 0xbb, 0xcc, 0xdd];
589        let mut encoder = ZlibEncoder::new(Vec::new(), flate2::Compression::default());
590        encoder.write_all(random_data).unwrap();
591        let compressed = encoder.finish().unwrap();
592
593        let result = TileInfo::detect(&compressed);
594        assert_eq!(result, TileInfo::new(Format::Mvt, Encoding::Zlib));
595    }
596
597    #[test]
598    fn test_invalid_json_in_gzip() {
599        // Data that looks like JSON but isn't valid => should fall back to MVT
600        let invalid_json = b"{this is not valid json}";
601        let compressed = encode_gzip(invalid_json).unwrap();
602        let result = TileInfo::detect(&compressed);
603        assert_eq!(result, TileInfo::new(Format::Mvt, Encoding::Gzip));
604    }
605
606    #[rstest]
607    #[case::minimal_tile(&[0x02, 0x01], Ok(()))]
608    #[case::one_byte_length(&[0x03, 0x01, 0xaa], Ok(()))]
609    #[case::two_byte_length(&[0x80, 0x04, 0x01, 0xaa], Ok(()))]
610    #[case::multi_byte_length(&[0x80, 0x80, 0x05, 0x01, 0xdd], Ok(()))]
611    #[case::wrong_version(&[0x03, 0x02, 0xaa], Err(SevenBitDecodingError::UnexpectedTag(0x02)))]
612    #[case::empty_input(&[], Err(SevenBitDecodingError::TruncatedSize))]
613    #[case::size_overflow(&[0xFF; 64], Err(SevenBitDecodingError::SizeOverflow))]
614    #[case::size_underflow(&[0x00, 0x01], Err(SevenBitDecodingError::SizeUnderflow))]
615    #[case::unterminated_length(&[0x80], Err(SevenBitDecodingError::TruncatedSize))]
616    #[case::missing_version_byte(&[0x05], Err(SevenBitDecodingError::TruncatedTag))]
617    #[case::wrong_length(&[0x03, 0x01], Err(SevenBitDecodingError::TruncatedData(1, 0)))]
618    fn test_decode_7bit_length_and_tag(
619        #[case] tile: &[u8],
620        #[case] expected: Result<(), SevenBitDecodingError>,
621    ) {
622        let allowed_versions = &[0x01_u8];
623        let decoded = decode_7bit_length_and_tag(tile, allowed_versions);
624        assert_eq!(decoded, expected, "can decode one layer correctly");
625
626        if tile.is_empty() {
627            return;
628        }
629        let mut tile_with_two_layers = vec![0x02, 0x01];
630        tile_with_two_layers.extend_from_slice(tile);
631        let decoded = decode_7bit_length_and_tag(&tile_with_two_layers, allowed_versions);
632        assert_eq!(decoded, expected, "can decode two layers correctly");
633    }
634
635    #[rstest]
636    #[case(-180.0, 85.0511, 0, (0,0))]
637    #[case(-180.0, 85.0511, 1, (0,0))]
638    #[case(-180.0, 85.0511, 2, (0,0))]
639    #[case(0.0, 0.0, 0, (0,0))]
640    #[case(0.0, 0.0, 1, (1,1))]
641    #[case(0.0, 0.0, 2, (2,2))]
642    #[case(0.0, 1.0, 0, (0,0))]
643    #[case(0.0, 1.0, 1, (1,0))]
644    #[case(0.0, 1.0, 2, (2,1))]
645    fn test_tile_colrow(
646        #[case] lng: f64,
647        #[case] lat: f64,
648        #[case] zoom: u8,
649        #[case] expected: (u32, u32),
650    ) {
651        assert_eq!(
652            expected,
653            tile_index(lng, lat, zoom),
654            "{lng},{lat}@z{zoom} should be {expected:?}"
655        );
656    }
657
658    #[rstest]
659    // you could easily get test cases from maptiler: https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/#4/-118.82/71.02
660    #[case(0, 0, 0, 0, 0, [-180.0,-85.051_128_779_806_6,180.0,85.051_128_779_806_6])]
661    #[case(1, 0, 0, 0, 0, [-180.0,0.0,0.0,85.051_128_779_806_6])]
662    #[case(5, 1, 1, 2, 2, [-168.75,81.093_213_852_608_37,-146.25,83.979_259_498_862_05])]
663    #[case(5, 1, 3, 2, 5, [-168.75,74.019_543_311_502_26,-146.25,81.093_213_852_608_37])]
664    fn test_xyz_to_bbox(
665        #[case] zoom: u8,
666        #[case] min_x: u32,
667        #[case] min_y: u32,
668        #[case] max_x: u32,
669        #[case] max_y: u32,
670        #[case] expected: [f64; 4],
671    ) {
672        let bbox = xyz_to_bbox(zoom, min_x, min_y, max_x, max_y);
673        assert_relative_eq!(bbox[0], expected[0], epsilon = f64::EPSILON * 2.0);
674        assert_relative_eq!(bbox[1], expected[1], epsilon = f64::EPSILON * 2.0);
675        assert_relative_eq!(bbox[2], expected[2], epsilon = f64::EPSILON * 2.0);
676        assert_relative_eq!(bbox[3], expected[3], epsilon = f64::EPSILON * 2.0);
677    }
678
679    #[rstest]
680    #[case(0, (0, 0, 0, 0))]
681    #[case(1, (0, 1, 0, 1))]
682    #[case(2, (0, 3, 0, 3))]
683    #[case(3, (0, 7, 0, 7))]
684    #[case(4, (0, 14, 1, 15))]
685    #[case(5, (0, 29, 2, 31))]
686    #[case(6, (0, 58, 5, 63))]
687    #[case(7, (0, 116, 11, 126))]
688    #[case(8, (0, 233, 23, 253))]
689    #[case(9, (0, 466, 47, 507))]
690    #[case(10, (1, 933, 94, 1_014))]
691    #[case(11, (3, 1_866, 188, 2_029))]
692    #[case(12, (6, 3_732, 377, 4_059))]
693    #[case(13, (12, 7_465, 755, 8_119))]
694    #[case(14, (25, 14_931, 1_510, 16_239))]
695    #[case(15, (51, 29_863, 3_020, 32_479))]
696    #[case(16, (102, 59_727, 6_041, 64_958))]
697    #[case(17, (204, 119_455, 12_083, 129_917))]
698    #[case(18, (409, 238_911, 24_166, 259_834))]
699    #[case(19, (819, 477_823, 48_332, 519_669))]
700    #[case(20, (1_638, 955_647, 96_665, 1_039_339))]
701    #[case(21, (3_276, 1_911_295, 193_331, 2_078_678))]
702    #[case(22, (6_553, 3_822_590, 386_662, 4_157_356))]
703    #[case(23, (13_107, 7_645_181, 773_324, 8_314_713))]
704    #[case(24, (26_214, 15_290_363, 1_546_649, 16_629_427))]
705    #[case(25, (52_428, 30_580_726, 3_093_299, 33_258_855))]
706    #[case(26, (104_857, 61_161_453, 6_186_598, 66_517_711))]
707    #[case(27, (209_715, 122_322_907, 12_373_196, 133_035_423))]
708    #[case(28, (419_430, 244_645_814, 24_746_393, 266_070_846))]
709    #[case(29, (838_860, 489_291_628, 49_492_787, 532_141_692))]
710    #[case(30, (1_677_721, 978_583_256, 98_985_574, 1_064_283_385))]
711    fn test_box_to_xyz(#[case] zoom: u8, #[case] expected_xyz: (u32, u32, u32, u32)) {
712        let actual_xyz = bbox_to_xyz(
713            -179.437_499_999_999_55,
714            -84.769_878_779_806_56,
715            -146.812_499_999_999_6,
716            -81.374_463_852_608_33,
717            zoom,
718        );
719        assert_eq!(
720            actual_xyz, expected_xyz,
721            "zoom {zoom} does not have the right xyz"
722        );
723    }
724
725    #[rstest]
726    // test data via https://epsg.io/transform#s_srs=4326&t_srs=3857
727    #[case((0.0,0.0), (0.0,0.0))]
728    #[case((30.0,0.0), (3_339_584.723_798_207,0.0))]
729    #[case((-30.0,0.0), (-3_339_584.723_798_207,0.0))]
730    #[case((0.0,30.0), (0.0,3_503_549.843_504_375_3))]
731    #[case((0.0,-30.0), (0.0,-3_503_549.843_504_375_3))]
732    #[case((38.897_957,-77.036_560), (4_330_100.766_138_651, -13_872_207.775_755_845))] // white house
733    #[case((-180.0,-85.0), (-20_037_508.342_789_244, -19_971_868.880_408_566))]
734    #[case((180.0,85.0), (20_037_508.342_789_244, 19_971_868.880_408_566))]
735    #[case((0.026_949_458_523_585_632,0.080_848_348_740_973_67), (3000.0, 9000.0))]
736    fn test_coordinate_syste_conversion(
737        #[case] wgs84: (f64, f64),
738        #[case] webmercator: (f64, f64),
739    ) {
740        // epsg produces the expected values with f32 precision, grrr..
741        let epsilon = f64::from(f32::EPSILON);
742
743        let actual_wgs84 = webmercator_to_wgs84(webmercator.0, webmercator.1);
744        assert_relative_eq!(actual_wgs84.0, wgs84.0, epsilon = epsilon);
745        assert_relative_eq!(actual_wgs84.1, wgs84.1, epsilon = epsilon);
746
747        let actual_webmercator = wgs84_to_webmercator(wgs84.0, wgs84.1);
748        assert_relative_eq!(actual_webmercator.0, webmercator.0, epsilon = epsilon);
749        assert_relative_eq!(actual_webmercator.1, webmercator.1, epsilon = epsilon);
750    }
751
752    #[rstest]
753    #[case(0..11, 0)]
754    #[case(11..14, 1)]
755    #[case(14..17, 2)]
756    #[case(17..21, 3)]
757    #[case(21..24, 4)]
758    #[case(24..27, 5)]
759    #[case(27..30, 6)]
760    fn test_get_zoom_precision(
761        #[case] zoom: std::ops::Range<u8>,
762        #[case] expected_precision: usize,
763    ) {
764        for z in zoom {
765            let actual_precision = get_zoom_precision(z);
766            assert_eq!(
767                actual_precision, expected_precision,
768                "Zoom level {z} should have precision {expected_precision}, but was {actual_precision}"
769            );
770        }
771    }
772
773    #[test]
774    fn test_tile_coord_zoom_range() {
775        for z in 0..=MAX_ZOOM {
776            assert!(TileCoord::is_possible_on_zoom_level(z, 0, 0));
777            assert_eq!(
778                TileCoord::new_checked(z, 0, 0),
779                Some(TileCoord { z, x: 0, y: 0 })
780            );
781        }
782        assert!(!TileCoord::is_possible_on_zoom_level(MAX_ZOOM + 1, 0, 0));
783        assert_eq!(TileCoord::new_checked(MAX_ZOOM + 1, 0, 0), None);
784    }
785
786    #[test]
787    fn test_tile_coord_new_checked_xy_for_zoom() {
788        assert!(TileCoord::is_possible_on_zoom_level(5, 0, 0));
789        assert_eq!(
790            TileCoord::new_checked(5, 0, 0),
791            Some(TileCoord { z: 5, x: 0, y: 0 })
792        );
793        assert!(TileCoord::is_possible_on_zoom_level(5, 31, 31));
794        assert_eq!(
795            TileCoord::new_checked(5, 31, 31),
796            Some(TileCoord { z: 5, x: 31, y: 31 })
797        );
798        assert!(!TileCoord::is_possible_on_zoom_level(5, 31, 32));
799        assert_eq!(TileCoord::new_checked(5, 31, 32), None);
800        assert!(!TileCoord::is_possible_on_zoom_level(5, 32, 31));
801        assert_eq!(TileCoord::new_checked(5, 32, 31), None);
802    }
803
804    #[test]
805    /// Any (u8, u32, u32) values can be put inside [`TileCoord`], of course, but some
806    /// functions may panic at runtime (e.g. [`mbtiles::invert_y_value`]) if they are impossible,
807    /// so let's not do that.
808    fn test_tile_coord_new_unchecked() {
809        assert_eq!(
810            TileCoord::new_unchecked(u8::MAX, u32::MAX, u32::MAX),
811            TileCoord {
812                z: u8::MAX,
813                x: u32::MAX,
814                y: u32::MAX
815            }
816        );
817    }
818
819    #[test]
820    fn xyz_format() {
821        let xyz = TileCoord { z: 1, x: 2, y: 3 };
822        assert_eq!(format!("{xyz}"), "1,2,3");
823        assert_eq!(format!("{xyz:#}"), "1/2/3");
824    }
825
826    #[rstest]
827    #[case("none", Some(Encoding::Uncompressed))]
828    #[case("identity", Some(Encoding::Uncompressed))]
829    #[case("IDENTITY", Some(Encoding::Uncompressed))]
830    #[case("gzip", Some(Encoding::Gzip))]
831    #[case("GZIP", Some(Encoding::Gzip))]
832    #[case("deflate", Some(Encoding::Zlib))]
833    #[case("zlib", Some(Encoding::Zlib))]
834    #[case("br", Some(Encoding::Brotli))]
835    #[case("brotli", Some(Encoding::Brotli))]
836    #[case("zstd", Some(Encoding::Zstd))]
837    #[case("unknown", None)]
838    #[case("", None)]
839    fn test_encoding_parse(#[case] input: &str, #[case] expected: Option<Encoding>) {
840        assert_eq!(Encoding::parse(input), expected);
841    }
842
843    #[rstest]
844    #[case(Encoding::Uncompressed, None)]
845    #[case(Encoding::Internal, None)]
846    #[case(Encoding::Gzip, Some("gzip"))]
847    #[case(Encoding::Zlib, Some("deflate"))]
848    #[case(Encoding::Brotli, Some("br"))]
849    #[case(Encoding::Zstd, Some("zstd"))]
850    fn test_compression(#[case] encoding: Encoding, #[case] expected: Option<&str>) {
851        assert_eq!(encoding.compression(), expected);
852    }
853}