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" | "jpg") => 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            Self::detect_vectorish_format(value).into()
267        }
268    }
269
270    /// Fast-path detection without decompression
271    #[must_use]
272    fn detect_raster_formats(value: &[u8]) -> Option<Format> {
273        match value {
274            v if v.starts_with(b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") => Some(Format::Png),
275            v if v.starts_with(b"\x47\x49\x46\x38\x39\x61") => Some(Format::Gif),
276            v if v.starts_with(b"\xFF\xD8\xFF") => Some(Format::Jpeg),
277            v if v.starts_with(b"RIFF") && v.len() > 8 && v[8..].starts_with(b"WEBP") => {
278                Some(Format::Webp)
279            }
280            _ => None,
281        }
282    }
283
284    /// Detect the format of vector (or json) data after decompression
285    #[must_use]
286    fn detect_vectorish_format(value: &[u8]) -> Format {
287        match value {
288            v if decode_7bit_length_and_tag(v, &[0x1]).is_ok() => Format::Mlt,
289            v if is_valid_json(v) => Format::Json,
290            // If we can't detect the format, we assume MVT.
291            // Reasoning:
292            //- it's the most common format and
293            //- we don't have a detector for it
294            _ => Format::Mvt,
295        }
296    }
297
298    #[must_use]
299    pub fn encoding(self, encoding: Encoding) -> Self {
300        Self { encoding, ..self }
301    }
302}
303
304impl From<Format> for TileInfo {
305    fn from(format: Format) -> Self {
306        Self::new(
307            format,
308            match format {
309                Format::Mlt
310                | Format::Png
311                | Format::Jpeg
312                | Format::Webp
313                | Format::Gif
314                | Format::Avif => Encoding::Internal,
315                Format::Mvt | Format::Json => Encoding::Uncompressed,
316            },
317        )
318    }
319}
320
321impl Display for TileInfo {
322    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
323        write!(f, "{}", self.format.content_type())?;
324        if let Some(encoding) = self.encoding.compression() {
325            write!(f, "; encoding={encoding}")?;
326        } else if self.encoding != Encoding::Uncompressed {
327            f.write_str("; uncompressed")?;
328        }
329        Ok(())
330    }
331}
332
333#[derive(thiserror::Error, Debug, PartialEq, Eq)]
334enum SevenBitDecodingError {
335    /// Expected a tag, but got nothing
336    #[error("Expected a tag, but got nothing")]
337    TruncatedTag,
338    /// The size of the tile is too large to be decoded
339    #[error("The size of the tile is too large to be decoded")]
340    SizeOverflow,
341    /// The size of the tile is lower than the number of bytes for the size and tag
342    #[error("The size of the tile is lower than the number of bytes for the size and tag")]
343    SizeUnderflow,
344    /// Expected a size, but got nothing
345    #[error("Expected a size, but got nothing")]
346    TruncatedSize,
347    /// Expected data according to the size, but got nothing
348    #[error("Expected {0} bytes of data in layer according to the size, but got only {1}")]
349    TruncatedData(u64, u64),
350    /// Got unexpected tag
351    #[error("Got tag {0} instead of the expected")]
352    UnexpectedTag(u8),
353}
354
355/// Tries to validate that the tile consists of a valid concatenation of (`size_7_bit`, `one_of_expected_version`, `data`)
356fn decode_7bit_length_and_tag(tile: &[u8], versions: &[u8]) -> Result<(), SevenBitDecodingError> {
357    if tile.is_empty() {
358        return Err(SevenBitDecodingError::TruncatedSize);
359    }
360    let mut tile_iter = tile.iter().peekable();
361    while tile_iter.peek().is_some() {
362        // need to parse size
363        let mut size = 0_u64;
364        let mut header_bit_count = 0_u64;
365        loop {
366            header_bit_count += 1;
367            let Some(b) = tile_iter.next() else {
368                return Err(SevenBitDecodingError::TruncatedSize);
369            };
370            if header_bit_count * 7 + 8 > 64 {
371                return Err(SevenBitDecodingError::SizeOverflow);
372            }
373            // decode size
374            size <<= 7;
375            let seven_bit_mask = !0x80;
376            size |= u64::from(*b & seven_bit_mask);
377            // 0 => no further size
378            if b & 0x80 == 0 {
379                // need to check tag
380                header_bit_count += 1;
381                let Some(tag) = tile_iter.next() else {
382                    return Err(SevenBitDecodingError::TruncatedTag);
383                };
384                if !versions.contains(tag) {
385                    return Err(SevenBitDecodingError::UnexpectedTag(*tag));
386                }
387                // need to check data-length
388                let payload_len = size
389                    .checked_sub(header_bit_count)
390                    .ok_or(SevenBitDecodingError::SizeUnderflow)?;
391                for i in 0..payload_len {
392                    if tile_iter.next().is_none() {
393                        return Err(SevenBitDecodingError::TruncatedData(payload_len, i));
394                    }
395                }
396                break;
397            }
398        }
399    }
400    Ok(())
401}
402
403/// Detects if the given tile is a valid JSON tile.
404///
405/// The check for a dictionary is used to speed up the validation process.
406fn is_valid_json(tile: &[u8]) -> bool {
407    tile.starts_with(b"{")
408        && tile.ends_with(b"}")
409        && serde_json::from_slice::<serde::de::IgnoredAny>(tile).is_ok()
410}
411
412/// Convert longitude and latitude to a tile (x,y) coordinates for a given zoom
413#[must_use]
414#[expect(clippy::cast_possible_truncation)]
415#[expect(clippy::cast_sign_loss)]
416pub fn tile_index(lng: f64, lat: f64, zoom: u8) -> (u32, u32) {
417    let tile_size = EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom);
418    let (x, y) = wgs84_to_webmercator(lng, lat);
419    let col = (((x - (EARTH_CIRCUMFERENCE * -0.5)).abs() / tile_size) as u32).min((1 << zoom) - 1);
420    let row = ((((EARTH_CIRCUMFERENCE * 0.5) - y).abs() / tile_size) as u32).min((1 << zoom) - 1);
421    (col, row)
422}
423
424/// Convert min/max XYZ tile coordinates to a bounding box values.
425///
426/// The result is `[min_lng, min_lat, max_lng, max_lat]`
427///
428/// # Panics
429/// Panics if `zoom` is greater than [`MAX_ZOOM`].
430#[must_use]
431pub fn xyz_to_bbox(zoom: u8, min_x: u32, min_y: u32, max_x: u32, max_y: u32) -> [f64; 4] {
432    assert!(zoom <= MAX_ZOOM, "zoom {zoom} must be <= {MAX_ZOOM}");
433
434    let tile_length = EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom);
435
436    let left_down_bbox = tile_bbox(min_x, max_y, tile_length);
437    let right_top_bbox = tile_bbox(max_x, min_y, tile_length);
438
439    let (min_lng, min_lat) = webmercator_to_wgs84(left_down_bbox[0], left_down_bbox[1]);
440    let (max_lng, max_lat) = webmercator_to_wgs84(right_top_bbox[2], right_top_bbox[3]);
441    [min_lng, min_lat, max_lng, max_lat]
442}
443
444#[expect(clippy::cast_lossless)]
445fn tile_bbox(x: u32, y: u32, tile_length: f64) -> [f64; 4] {
446    let min_x = EARTH_CIRCUMFERENCE * -0.5 + x as f64 * tile_length;
447    let max_y = EARTH_CIRCUMFERENCE * 0.5 - y as f64 * tile_length;
448
449    [min_x, max_y - tile_length, min_x + tile_length, max_y]
450}
451
452/// Convert bounding box to a tile box `(min_x, min_y, max_x, max_y)` for a given zoom
453#[must_use]
454pub fn bbox_to_xyz(left: f64, bottom: f64, right: f64, top: f64, zoom: u8) -> (u32, u32, u32, u32) {
455    let (min_col, min_row) = tile_index(left, top, zoom);
456    let (max_col, max_row) = tile_index(right, bottom, zoom);
457    (min_col, min_row, max_col, max_row)
458}
459
460/// Compute precision of a zoom level, i.e. how many decimal digits of the longitude and latitude are relevant
461#[must_use]
462#[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
463pub fn get_zoom_precision(zoom: u8) -> usize {
464    assert!(zoom <= MAX_ZOOM, "zoom {zoom} must be <= {MAX_ZOOM}");
465    let lng_delta = webmercator_to_wgs84(EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom), 0.0).0;
466    let log = lng_delta.log10() - 0.5;
467    if log > 0.0 { 0 } else { -log.ceil() as usize }
468}
469
470/// transform [`WebMercator`](https://epsg.io/3857) to [WGS84](https://epsg.io/4326)
471// from https://github.com/Esri/arcgis-osm-editor/blob/e4b9905c264aa22f8eeb657efd52b12cdebea69a/src/OSMWeb10_1/Utils/WebMercator.cs
472#[must_use]
473pub fn webmercator_to_wgs84(x: f64, y: f64) -> (f64, f64) {
474    let lng = (x / EARTH_RADIUS).to_degrees();
475    let lat = f64::atan(f64::sinh(y / EARTH_RADIUS)).to_degrees();
476    (lng, lat)
477}
478
479/// transform [WGS84](https://epsg.io/4326) to [`WebMercator`](https://epsg.io/3857)
480// from https://github.com/Esri/arcgis-osm-editor/blob/e4b9905c264aa22f8eeb657efd52b12cdebea69a/src/OSMWeb10_1/Utils/WebMercator.cs
481#[must_use]
482pub fn wgs84_to_webmercator(lon: f64, lat: f64) -> (f64, f64) {
483    let x = lon * PI / 180.0 * EARTH_RADIUS;
484
485    let y_sin = lat.to_radians().sin();
486    let y = EARTH_RADIUS / 2.0 * ((1.0 + y_sin) / (1.0 - y_sin)).ln();
487
488    (x, y)
489}
490
491#[cfg(test)]
492mod tests {
493    use approx::assert_relative_eq;
494    use rstest::rstest;
495
496    use super::*;
497
498    #[rstest]
499    #[case::png(
500        include_bytes!("../fixtures/world.png"),
501        TileInfo::new(Format::Png, Encoding::Internal)
502    )]
503    #[case::jpg(
504        include_bytes!("../fixtures/world.jpg"),
505        TileInfo::new(Format::Jpeg, Encoding::Internal)
506    )]
507    #[case::webp(
508        include_bytes!("../fixtures/dc.webp"),
509        TileInfo::new(Format::Webp, Encoding::Internal)
510    )]
511    #[case::json(
512        br#"{"foo":"bar"}"#,
513        TileInfo::new(Format::Json, Encoding::Uncompressed)
514    )]
515    // we have no way of knowing what is an MVT -> we just say it is out of the
516    // fact that it is not something else
517    #[case::invalid_webp_header(b"RIFF", TileInfo::new(Format::Mvt, Encoding::Uncompressed))]
518    fn test_data_format_detect(#[case] data: &[u8], #[case] expected: TileInfo) {
519        assert_eq!(TileInfo::detect(data), expected);
520    }
521
522    /// Test detection of compressed content (JSON, MLT, MVT)
523    #[test]
524    fn test_compressed_json_gzip() {
525        let json_data = br#"{"type":"FeatureCollection","features":[]}"#;
526        let compressed = encode_gzip(json_data).unwrap();
527        let result = TileInfo::detect(&compressed);
528        assert_eq!(result, TileInfo::new(Format::Json, Encoding::Gzip));
529    }
530
531    #[test]
532    fn test_compressed_json_zlib() {
533        use std::io::Write as _;
534
535        use flate2::write::ZlibEncoder;
536
537        let json_data = br#"{"type":"FeatureCollection","features":[]}"#;
538        let mut encoder = ZlibEncoder::new(Vec::new(), flate2::Compression::default());
539        encoder.write_all(json_data).unwrap();
540        let compressed = encoder.finish().unwrap();
541
542        let result = TileInfo::detect(&compressed);
543        assert_eq!(result, TileInfo::new(Format::Json, Encoding::Zlib));
544    }
545
546    #[test]
547    fn test_raw_mlt_encoding_internal() {
548        // MLT has internal compression, so raw MLT bytes should be Encoding::Internal
549        // to prevent the serve path from applying heavyweight gzip/brotli on top.
550        let mlt_data = &[0x02, 0x01];
551        let result = TileInfo::detect(mlt_data);
552        assert_eq!(result, TileInfo::new(Format::Mlt, Encoding::Internal));
553    }
554
555    #[test]
556    fn test_compressed_mlt_gzip() {
557        // MLT tile: length=2 (0x02), version=1 (0x01)
558        let mlt_data = &[0x02, 0x01];
559        let compressed = encode_gzip(mlt_data).unwrap();
560        let result = TileInfo::detect(&compressed);
561        assert_eq!(result, TileInfo::new(Format::Mlt, Encoding::Gzip));
562    }
563
564    #[test]
565    fn test_compressed_mlt_zlib() {
566        use std::io::Write as _;
567
568        use flate2::write::ZlibEncoder;
569
570        // MLT tile: length=5 (0x05), version=1 (0x01), plus some data
571        let mlt_data = &[0x05, 0x01, 0xaa, 0xbb, 0xcc];
572        let mut encoder = ZlibEncoder::new(Vec::new(), flate2::Compression::default());
573        encoder.write_all(mlt_data).unwrap();
574        let compressed = encoder.finish().unwrap();
575
576        let result = TileInfo::detect(&compressed);
577        assert_eq!(result, TileInfo::new(Format::Mlt, Encoding::Zlib));
578    }
579
580    #[test]
581    fn test_compressed_mvt_gzip_fallback() {
582        // Random data that doesn't match any known format => should be detected as MVT
583        let random_data = &[0x1a, 0x2b, 0x3c, 0x4d];
584        let compressed = encode_gzip(random_data).unwrap();
585        let result = TileInfo::detect(&compressed);
586        assert_eq!(result, TileInfo::new(Format::Mvt, Encoding::Gzip));
587    }
588
589    #[test]
590    fn test_compressed_mvt_zlib_fallback() {
591        use std::io::Write as _;
592
593        use flate2::write::ZlibEncoder;
594
595        // Random data that doesn't match any known format => should be detected as MVT
596        let random_data = &[0xaa, 0xbb, 0xcc, 0xdd];
597        let mut encoder = ZlibEncoder::new(Vec::new(), flate2::Compression::default());
598        encoder.write_all(random_data).unwrap();
599        let compressed = encoder.finish().unwrap();
600
601        let result = TileInfo::detect(&compressed);
602        assert_eq!(result, TileInfo::new(Format::Mvt, Encoding::Zlib));
603    }
604
605    #[test]
606    fn test_invalid_json_in_gzip() {
607        // Data that looks like JSON but isn't valid => should fall back to MVT
608        let invalid_json = b"{this is not valid json}";
609        let compressed = encode_gzip(invalid_json).unwrap();
610        let result = TileInfo::detect(&compressed);
611        assert_eq!(result, TileInfo::new(Format::Mvt, Encoding::Gzip));
612    }
613
614    #[rstest]
615    #[case::minimal_tile(&[0x02, 0x01], Ok(()))]
616    #[case::one_byte_length(&[0x03, 0x01, 0xaa], Ok(()))]
617    #[case::two_byte_length(&[0x80, 0x04, 0x01, 0xaa], Ok(()))]
618    #[case::multi_byte_length(&[0x80, 0x80, 0x05, 0x01, 0xdd], Ok(()))]
619    #[case::wrong_version(&[0x03, 0x02, 0xaa], Err(SevenBitDecodingError::UnexpectedTag(0x02)))]
620    #[case::empty_input(&[], Err(SevenBitDecodingError::TruncatedSize))]
621    #[case::size_overflow(&[0xFF; 64], Err(SevenBitDecodingError::SizeOverflow))]
622    #[case::size_underflow(&[0x00, 0x01], Err(SevenBitDecodingError::SizeUnderflow))]
623    #[case::unterminated_length(&[0x80], Err(SevenBitDecodingError::TruncatedSize))]
624    #[case::missing_version_byte(&[0x05], Err(SevenBitDecodingError::TruncatedTag))]
625    #[case::wrong_length(&[0x03, 0x01], Err(SevenBitDecodingError::TruncatedData(1, 0)))]
626    fn test_decode_7bit_length_and_tag(
627        #[case] tile: &[u8],
628        #[case] expected: Result<(), SevenBitDecodingError>,
629    ) {
630        let allowed_versions = &[0x01_u8];
631        let decoded = decode_7bit_length_and_tag(tile, allowed_versions);
632        assert_eq!(decoded, expected, "can decode one layer correctly");
633
634        if tile.is_empty() {
635            return;
636        }
637        let mut tile_with_two_layers = vec![0x02, 0x01];
638        tile_with_two_layers.extend_from_slice(tile);
639        let decoded = decode_7bit_length_and_tag(&tile_with_two_layers, allowed_versions);
640        assert_eq!(decoded, expected, "can decode two layers correctly");
641    }
642
643    #[rstest]
644    #[case(-180.0, 85.0511, 0, (0,0))]
645    #[case(-180.0, 85.0511, 1, (0,0))]
646    #[case(-180.0, 85.0511, 2, (0,0))]
647    #[case(0.0, 0.0, 0, (0,0))]
648    #[case(0.0, 0.0, 1, (1,1))]
649    #[case(0.0, 0.0, 2, (2,2))]
650    #[case(0.0, 1.0, 0, (0,0))]
651    #[case(0.0, 1.0, 1, (1,0))]
652    #[case(0.0, 1.0, 2, (2,1))]
653    fn test_tile_colrow(
654        #[case] lng: f64,
655        #[case] lat: f64,
656        #[case] zoom: u8,
657        #[case] expected: (u32, u32),
658    ) {
659        assert_eq!(
660            expected,
661            tile_index(lng, lat, zoom),
662            "{lng},{lat}@z{zoom} should be {expected:?}"
663        );
664    }
665
666    #[rstest]
667    // you could easily get test cases from maptiler: https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/#4/-118.82/71.02
668    #[case(0, 0, 0, 0, 0, [-180.0,-85.051_128_779_806_6,180.0,85.051_128_779_806_6])]
669    #[case(1, 0, 0, 0, 0, [-180.0,0.0,0.0,85.051_128_779_806_6])]
670    #[case(5, 1, 1, 2, 2, [-168.75,81.093_213_852_608_37,-146.25,83.979_259_498_862_05])]
671    #[case(5, 1, 3, 2, 5, [-168.75,74.019_543_311_502_26,-146.25,81.093_213_852_608_37])]
672    fn test_xyz_to_bbox(
673        #[case] zoom: u8,
674        #[case] min_x: u32,
675        #[case] min_y: u32,
676        #[case] max_x: u32,
677        #[case] max_y: u32,
678        #[case] expected: [f64; 4],
679    ) {
680        let bbox = xyz_to_bbox(zoom, min_x, min_y, max_x, max_y);
681        assert_relative_eq!(bbox[0], expected[0], epsilon = f64::EPSILON * 2.0);
682        assert_relative_eq!(bbox[1], expected[1], epsilon = f64::EPSILON * 2.0);
683        assert_relative_eq!(bbox[2], expected[2], epsilon = f64::EPSILON * 2.0);
684        assert_relative_eq!(bbox[3], expected[3], epsilon = f64::EPSILON * 2.0);
685    }
686
687    #[rstest]
688    #[case(0, (0, 0, 0, 0))]
689    #[case(1, (0, 1, 0, 1))]
690    #[case(2, (0, 3, 0, 3))]
691    #[case(3, (0, 7, 0, 7))]
692    #[case(4, (0, 14, 1, 15))]
693    #[case(5, (0, 29, 2, 31))]
694    #[case(6, (0, 58, 5, 63))]
695    #[case(7, (0, 116, 11, 126))]
696    #[case(8, (0, 233, 23, 253))]
697    #[case(9, (0, 466, 47, 507))]
698    #[case(10, (1, 933, 94, 1_014))]
699    #[case(11, (3, 1_866, 188, 2_029))]
700    #[case(12, (6, 3_732, 377, 4_059))]
701    #[case(13, (12, 7_465, 755, 8_119))]
702    #[case(14, (25, 14_931, 1_510, 16_239))]
703    #[case(15, (51, 29_863, 3_020, 32_479))]
704    #[case(16, (102, 59_727, 6_041, 64_958))]
705    #[case(17, (204, 119_455, 12_083, 129_917))]
706    #[case(18, (409, 238_911, 24_166, 259_834))]
707    #[case(19, (819, 477_823, 48_332, 519_669))]
708    #[case(20, (1_638, 955_647, 96_665, 1_039_339))]
709    #[case(21, (3_276, 1_911_295, 193_331, 2_078_678))]
710    #[case(22, (6_553, 3_822_590, 386_662, 4_157_356))]
711    #[case(23, (13_107, 7_645_181, 773_324, 8_314_713))]
712    #[case(24, (26_214, 15_290_363, 1_546_649, 16_629_427))]
713    #[case(25, (52_428, 30_580_726, 3_093_299, 33_258_855))]
714    #[case(26, (104_857, 61_161_453, 6_186_598, 66_517_711))]
715    #[case(27, (209_715, 122_322_907, 12_373_196, 133_035_423))]
716    #[case(28, (419_430, 244_645_814, 24_746_393, 266_070_846))]
717    #[case(29, (838_860, 489_291_628, 49_492_787, 532_141_692))]
718    #[case(30, (1_677_721, 978_583_256, 98_985_574, 1_064_283_385))]
719    fn test_box_to_xyz(#[case] zoom: u8, #[case] expected_xyz: (u32, u32, u32, u32)) {
720        let actual_xyz = bbox_to_xyz(
721            -179.437_499_999_999_55,
722            -84.769_878_779_806_56,
723            -146.812_499_999_999_6,
724            -81.374_463_852_608_33,
725            zoom,
726        );
727        assert_eq!(
728            actual_xyz, expected_xyz,
729            "zoom {zoom} does not have the right xyz"
730        );
731    }
732
733    #[rstest]
734    // test data via https://epsg.io/transform#s_srs=4326&t_srs=3857
735    #[case((0.0,0.0), (0.0,0.0))]
736    #[case((30.0,0.0), (3_339_584.723_798_207,0.0))]
737    #[case((-30.0,0.0), (-3_339_584.723_798_207,0.0))]
738    #[case((0.0,30.0), (0.0,3_503_549.843_504_375_3))]
739    #[case((0.0,-30.0), (0.0,-3_503_549.843_504_375_3))]
740    #[case((38.897_957,-77.036_560), (4_330_100.766_138_651, -13_872_207.775_755_845))] // white house
741    #[case((-180.0,-85.0), (-20_037_508.342_789_244, -19_971_868.880_408_566))]
742    #[case((180.0,85.0), (20_037_508.342_789_244, 19_971_868.880_408_566))]
743    #[case((0.026_949_458_523_585_632,0.080_848_348_740_973_67), (3000.0, 9000.0))]
744    fn test_coordinate_syste_conversion(
745        #[case] wgs84: (f64, f64),
746        #[case] webmercator: (f64, f64),
747    ) {
748        // epsg produces the expected values with f32 precision, grrr..
749        let epsilon = f64::from(f32::EPSILON);
750
751        let actual_wgs84 = webmercator_to_wgs84(webmercator.0, webmercator.1);
752        assert_relative_eq!(actual_wgs84.0, wgs84.0, epsilon = epsilon);
753        assert_relative_eq!(actual_wgs84.1, wgs84.1, epsilon = epsilon);
754
755        let actual_webmercator = wgs84_to_webmercator(wgs84.0, wgs84.1);
756        assert_relative_eq!(actual_webmercator.0, webmercator.0, epsilon = epsilon);
757        assert_relative_eq!(actual_webmercator.1, webmercator.1, epsilon = epsilon);
758    }
759
760    #[rstest]
761    #[case(0..11, 0)]
762    #[case(11..14, 1)]
763    #[case(14..17, 2)]
764    #[case(17..21, 3)]
765    #[case(21..24, 4)]
766    #[case(24..27, 5)]
767    #[case(27..30, 6)]
768    fn test_get_zoom_precision(
769        #[case] zoom: std::ops::Range<u8>,
770        #[case] expected_precision: usize,
771    ) {
772        for z in zoom {
773            let actual_precision = get_zoom_precision(z);
774            assert_eq!(
775                actual_precision, expected_precision,
776                "Zoom level {z} should have precision {expected_precision}, but was {actual_precision}"
777            );
778        }
779    }
780
781    #[test]
782    fn test_tile_coord_zoom_range() {
783        for z in 0..=MAX_ZOOM {
784            assert!(TileCoord::is_possible_on_zoom_level(z, 0, 0));
785            assert_eq!(
786                TileCoord::new_checked(z, 0, 0),
787                Some(TileCoord { z, x: 0, y: 0 })
788            );
789        }
790        assert!(!TileCoord::is_possible_on_zoom_level(MAX_ZOOM + 1, 0, 0));
791        assert_eq!(TileCoord::new_checked(MAX_ZOOM + 1, 0, 0), None);
792    }
793
794    #[test]
795    fn test_tile_coord_new_checked_xy_for_zoom() {
796        assert!(TileCoord::is_possible_on_zoom_level(5, 0, 0));
797        assert_eq!(
798            TileCoord::new_checked(5, 0, 0),
799            Some(TileCoord { z: 5, x: 0, y: 0 })
800        );
801        assert!(TileCoord::is_possible_on_zoom_level(5, 31, 31));
802        assert_eq!(
803            TileCoord::new_checked(5, 31, 31),
804            Some(TileCoord { z: 5, x: 31, y: 31 })
805        );
806        assert!(!TileCoord::is_possible_on_zoom_level(5, 31, 32));
807        assert_eq!(TileCoord::new_checked(5, 31, 32), None);
808        assert!(!TileCoord::is_possible_on_zoom_level(5, 32, 31));
809        assert_eq!(TileCoord::new_checked(5, 32, 31), None);
810    }
811
812    #[test]
813    /// Any (u8, u32, u32) values can be put inside [`TileCoord`], of course, but some
814    /// functions may panic at runtime (e.g. [`mbtiles::invert_y_value`]) if they are impossible,
815    /// so let's not do that.
816    fn test_tile_coord_new_unchecked() {
817        assert_eq!(
818            TileCoord::new_unchecked(u8::MAX, u32::MAX, u32::MAX),
819            TileCoord {
820                z: u8::MAX,
821                x: u32::MAX,
822                y: u32::MAX
823            }
824        );
825    }
826
827    #[test]
828    fn xyz_format() {
829        let xyz = TileCoord { z: 1, x: 2, y: 3 };
830        assert_eq!(format!("{xyz}"), "1,2,3");
831        assert_eq!(format!("{xyz:#}"), "1/2/3");
832    }
833
834    #[rstest]
835    #[case("none", Some(Encoding::Uncompressed))]
836    #[case("identity", Some(Encoding::Uncompressed))]
837    #[case("IDENTITY", Some(Encoding::Uncompressed))]
838    #[case("gzip", Some(Encoding::Gzip))]
839    #[case("GZIP", Some(Encoding::Gzip))]
840    #[case("deflate", Some(Encoding::Zlib))]
841    #[case("zlib", Some(Encoding::Zlib))]
842    #[case("br", Some(Encoding::Brotli))]
843    #[case("brotli", Some(Encoding::Brotli))]
844    #[case("zstd", Some(Encoding::Zstd))]
845    #[case("unknown", None)]
846    #[case("", None)]
847    fn test_encoding_parse(#[case] input: &str, #[case] expected: Option<Encoding>) {
848        assert_eq!(Encoding::parse(input), expected);
849    }
850
851    #[rstest]
852    #[case(Encoding::Uncompressed, None)]
853    #[case(Encoding::Internal, None)]
854    #[case(Encoding::Gzip, Some("gzip"))]
855    #[case(Encoding::Zlib, Some("deflate"))]
856    #[case(Encoding::Brotli, Some("br"))]
857    #[case(Encoding::Zstd, Some("zstd"))]
858    fn test_compression(#[case] encoding: Encoding, #[case] expected: Option<&str>) {
859        assert_eq!(encoding.compression(), expected);
860    }
861}