#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
use std::f64::consts::PI;
use std::fmt::{Display, Formatter};
use std::io::Read;
use flate2::read::{MultiGzDecoder, ZlibDecoder};
use strum::EnumIter;
pub const EARTH_CIRCUMFERENCE: f64 = 40_075_016.685_578_5;
pub const EARTH_CIRCUMFERENCE_DEGREES: u32 = 360;
pub const EARTH_RADIUS: f64 = EARTH_CIRCUMFERENCE / 2.0 / PI;
pub const MAX_ZOOM: u8 = 30;
mod decoders;
pub use decoders::*;
mod grid;
pub use crate::grid::{
BUILT_IN_GRIDS, CrsAxes, EUROPEAN_ETRS89_LAEA_QUAD, NZTM2000_QUAD, SIMPLE_CRS, TileGrid, TileGridError,
UPS_ANTARCTIC_WGS84_QUAD, UPS_ARCTIC_WGS84_QUAD, WEB_MERCATOR_QUAD, WEB_MERCATOR_QUAD_ID, WGS1984_QUAD_ID,
WORLD_CRS84_QUAD, WORLD_CRS84_QUAD_ID, WORLD_MERCATOR_WGS84_QUAD,
};
mod rectangle;
pub use crate::rectangle::{TileRect, append_rect};
mod coordinate;
pub use crate::coordinate::TileCoord;
pub type TileData = bytes::Bytes;
pub type Tile = (TileCoord, Option<TileData>);
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, EnumIter)]
pub enum Format {
Gif,
Jpeg,
Json,
Mvt,
Mlt,
Png,
Webp,
Avif,
Jxl,
}
impl Format {
pub const IMAGE_FORMATS: &[Self] = &[Self::Gif, Self::Jpeg, Self::Png, Self::Webp, Self::Avif, Self::Jxl];
#[must_use]
pub fn parse(value: &str) -> Option<Self> {
Some(match value.to_ascii_lowercase().as_str() {
"gif" => Self::Gif,
"jpg" | "jpeg" => Self::Jpeg,
"json" => Self::Json,
"pbf" | "mvt" => Self::Mvt,
"mlt" => Self::Mlt,
"png" => Self::Png,
"webp" => Self::Webp,
"avif" => Self::Avif,
"jxl" => Self::Jxl,
_ => None?,
})
}
#[must_use]
pub const fn metadata_format_value(self) -> &'static str {
match self {
Self::Gif => "gif",
Self::Jpeg => "jpeg",
Self::Json => "json",
Self::Mvt => "pbf",
Self::Mlt => "mlt",
Self::Png => "png",
Self::Webp => "webp",
Self::Avif => "avif",
Self::Jxl => "jxl",
}
}
#[must_use]
pub const fn content_type(self) -> &'static str {
match self {
Self::Gif => "image/gif",
Self::Jpeg => "image/jpeg",
Self::Json => "application/json",
Self::Mvt => "application/x-protobuf",
Self::Mlt => "application/vnd.maplibre-tile",
Self::Png => "image/png",
Self::Webp => "image/webp",
Self::Avif => "image/avif",
Self::Jxl => "image/jxl",
}
}
#[must_use]
pub const fn ogc_content_type(self) -> &'static str {
match self {
Self::Mvt => "application/vnd.mapbox-vector-tile",
Self::Gif | Self::Jpeg | Self::Json | Self::Mlt | Self::Png | Self::Webp | Self::Avif | Self::Jxl => {
self.content_type()
}
}
}
#[must_use]
pub fn from_content_type(supertype: &str, subtype: &str) -> Option<Self> {
Some(match (supertype, subtype) {
("image", "gif") => Self::Gif,
("image", "jpeg" | "jpg") => Self::Jpeg,
("application", "json") => Self::Json,
("application", "x-protobuf" | "vnd.mapbox-vector-tile") => Self::Mvt,
("application", "vnd.maplibre-vector-tile" | "vnd.maplibre-tile") => Self::Mlt,
("image", "png") => Self::Png,
("image", "webp") => Self::Webp,
("image", "avif") => Self::Avif,
("image", "jxl") => Self::Jxl,
_ => None?,
})
}
#[must_use]
pub const fn is_detectable(self) -> bool {
match self {
Self::Png | Self::Jpeg | Self::Gif | Self::Webp | Self::Avif | Self::Jxl | Self::Json | Self::Mlt => true,
Self::Mvt => false,
}
}
}
impl Display for Format {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(match *self {
Self::Gif => "gif",
Self::Jpeg => "jpeg",
Self::Json => "json",
Self::Mvt => "mvt",
Self::Mlt => "mlt",
Self::Png => "png",
Self::Webp => "webp",
Self::Avif => "avif",
Self::Jxl => "jxl",
})
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Encoding {
Uncompressed = 0b0000_0000,
Internal = 0b0000_0001,
Gzip = 0b0000_0010,
Zlib = 0b0000_0100,
Brotli = 0b0000_1000,
Zstd = 0b0001_0000,
}
impl Encoding {
#[must_use]
pub fn parse(value: &str) -> Option<Self> {
Some(match value.to_ascii_lowercase().as_str() {
"none" | "identity" => Self::Uncompressed,
"gzip" => Self::Gzip,
"deflate" | "zlib" => Self::Zlib,
"br" | "brotli" => Self::Brotli,
"zstd" => Self::Zstd,
_ => None?,
})
}
#[must_use]
pub const fn compression(self) -> Option<&'static str> {
match self {
Self::Uncompressed | Self::Internal => None,
Self::Gzip => Some("gzip"),
Self::Zlib => Some("deflate"),
Self::Brotli => Some("br"),
Self::Zstd => Some("zstd"),
}
}
#[must_use]
pub const fn is_encoded(self) -> bool {
match self {
Self::Uncompressed | Self::Internal => false,
Self::Gzip | Self::Zlib | Self::Brotli | Self::Zstd => true,
}
}
#[must_use]
pub fn detect(value: &[u8]) -> Option<Self> {
match value {
[0x1f, 0x8b, ..] => Some(Self::Gzip),
[cmf, flg, ..] if is_zlib_header(*cmf, *flg) => Some(Self::Zlib),
_ => None,
}
}
}
fn is_zlib_header(cmf: u8, flg: u8) -> bool {
cmf & 0x0f == 8 && cmf >> 4 <= 7 && (u16::from(cmf) * 256 + u16::from(flg)) % 31 == 0
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TileInfo {
pub format: Format,
pub encoding: Encoding,
}
impl TileInfo {
#[must_use]
pub const fn new(format: Format, encoding: Encoding) -> Self {
Self { format, encoding }
}
#[must_use]
pub fn detect(value: &[u8]) -> Self {
match Encoding::detect(value) {
Some(encoding @ Encoding::Gzip) => Self::detect_inner(&mut MultiGzDecoder::new(value), encoding),
Some(encoding @ Encoding::Zlib) => Self::detect_inner(&mut ZlibDecoder::new(value), encoding),
_ => match Self::detect_raster_formats(value) {
Some(raster_format) => Self::new(raster_format, Encoding::Internal),
None => Self::detect_vectorish_format(value).into(),
},
}
}
fn detect_inner(decoder: &mut impl Read, encoding: Encoding) -> Self {
let mut decompressed = Vec::new();
let format = if decoder
.by_ref()
.take(DETECT_PREFIX_LEN)
.read_to_end(&mut decompressed)
.is_err()
|| rules_out_mlt_and_json(&decompressed)
|| decoder.read_to_end(&mut decompressed).is_err()
{
Format::Mvt
} else {
Self::detect_vectorish_format(&decompressed)
};
Self::new(format, encoding)
}
#[must_use]
fn detect_raster_formats(value: &[u8]) -> Option<Format> {
match value {
v if v.starts_with(b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") => Some(Format::Png),
v if v.starts_with(b"\x47\x49\x46\x38\x39\x61") => Some(Format::Gif),
v if v.starts_with(b"\xFF\xD8\xFF") => Some(Format::Jpeg),
v if v.starts_with(b"\xFF\x0A") && decode_7bit_length_and_tag(v, &[0x1]).is_err() => Some(Format::Jxl),
v if v.starts_with(b"\x00\x00\x00\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A") => Some(Format::Jxl),
v if v.starts_with(b"RIFF") && v.len() > 8 && v[8..].starts_with(b"WEBP") => Some(Format::Webp),
_ => None,
}
}
#[must_use]
fn detect_vectorish_format(value: &[u8]) -> Format {
match value {
v if decode_7bit_length_and_tag(v, &[0x1]).is_ok() => Format::Mlt,
v if is_valid_json(v) => Format::Json,
_ => Format::Mvt,
}
}
#[must_use]
pub const fn encoding(self, encoding: Encoding) -> Self {
Self { encoding, ..self }
}
}
impl From<Format> for TileInfo {
fn from(format: Format) -> Self {
Self::new(
format,
match format {
Format::Mlt | Format::Png | Format::Jpeg | Format::Webp | Format::Gif | Format::Avif | Format::Jxl => {
Encoding::Internal
}
Format::Mvt | Format::Json => Encoding::Uncompressed,
},
)
}
}
impl Display for TileInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.format.content_type())?;
if let Some(encoding) = self.encoding.compression() {
write!(f, "; encoding={encoding}")?;
} else if self.encoding != Encoding::Uncompressed {
f.write_str("; uncompressed")?;
}
Ok(())
}
}
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
enum SevenBitDecodingError {
#[error("Expected a tag, but got nothing")]
TruncatedTag,
#[error("The size of the tile is too large to be decoded")]
SizeOverflow,
#[error("The size of the tile is lower than the one byte for the tag")]
SizeUnderflow,
#[error("Expected a size, but got nothing")]
TruncatedSize,
#[error("Expected {expected} bytes of data in layer according to the size, but got only {actual}")]
TruncatedData { expected: u64, actual: u64 },
#[error("Got tag {0} instead of the expected")]
UnexpectedTag(u8),
}
fn decode_7bit_length_and_tag(tile: &[u8], versions: &[u8]) -> Result<(), SevenBitDecodingError> {
if tile.is_empty() {
return Err(SevenBitDecodingError::TruncatedSize);
}
let mut tile_iter = tile.iter().peekable();
while tile_iter.peek().is_some() {
let mut size = 0_u64;
let mut header_bit_count = 0_u64;
loop {
header_bit_count += 1;
let Some(b) = tile_iter.next() else {
return Err(SevenBitDecodingError::TruncatedSize);
};
if header_bit_count * 7 + 8 > 64 {
return Err(SevenBitDecodingError::SizeOverflow);
}
let seven_bit_mask = !0x80;
size |= u64::from(*b & seven_bit_mask) << ((header_bit_count - 1) * 7);
if b & 0x80 == 0 {
let Some(tag) = tile_iter.next() else {
return Err(SevenBitDecodingError::TruncatedTag);
};
if !versions.contains(tag) {
return Err(SevenBitDecodingError::UnexpectedTag(*tag));
}
let payload_len = size.checked_sub(1).ok_or(SevenBitDecodingError::SizeUnderflow)?;
for i in 0..payload_len {
if tile_iter.next().is_none() {
return Err(SevenBitDecodingError::TruncatedData {
expected: payload_len,
actual: i,
});
}
}
break;
}
}
}
Ok(())
}
fn is_valid_json(tile: &[u8]) -> bool {
tile.starts_with(b"{") && tile.ends_with(b"}") && serde_json::from_slice::<serde::de::IgnoredAny>(tile).is_ok()
}
const DETECT_PREFIX_LEN: u64 = 16;
fn rules_out_mlt_and_json(prefix: &[u8]) -> bool {
!prefix.starts_with(b"{")
&& matches!(
decode_7bit_length_and_tag(prefix, &[0x1]),
Err(SevenBitDecodingError::UnexpectedTag(_)
| SevenBitDecodingError::SizeOverflow
| SevenBitDecodingError::SizeUnderflow)
)
}
#[must_use]
#[expect(clippy::cast_possible_truncation)]
#[expect(clippy::cast_sign_loss)]
pub fn tile_index(lng: f64, lat: f64, zoom: u8) -> (u32, u32) {
let tile_size = EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom);
let (x, y) = wgs84_to_webmercator(lng, lat);
let col = ((EARTH_CIRCUMFERENCE.mul_add(0.5, x).abs() / tile_size) as u32).min((1 << zoom) - 1);
let row = ((EARTH_CIRCUMFERENCE.mul_add(0.5, -y).abs() / tile_size) as u32).min((1 << zoom) - 1);
(col, row)
}
#[must_use]
pub fn xyz_to_bbox(zoom: u8, min_x: u32, min_y: u32, max_x: u32, max_y: u32) -> [f64; 4] {
assert!(zoom <= MAX_ZOOM, "zoom {zoom} must be <= {MAX_ZOOM}");
let tile_length = EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom);
let left_down_bbox = tile_bbox(min_x, max_y, tile_length);
let right_top_bbox = tile_bbox(max_x, min_y, tile_length);
let (min_lng, min_lat) = webmercator_to_wgs84(left_down_bbox[0], left_down_bbox[1]);
let (max_lng, max_lat) = webmercator_to_wgs84(right_top_bbox[2], right_top_bbox[3]);
[min_lng, min_lat, max_lng, max_lat]
}
#[expect(clippy::cast_lossless)]
#[must_use]
pub fn tile_bbox(x: u32, y: u32, tile_length: f64) -> [f64; 4] {
let min_x = (x as f64).mul_add(tile_length, EARTH_CIRCUMFERENCE * -0.5);
let max_y = (y as f64).mul_add(-tile_length, EARTH_CIRCUMFERENCE * 0.5);
[min_x, max_y - tile_length, min_x + tile_length, max_y]
}
#[must_use]
pub fn bbox_to_xyz(left: f64, bottom: f64, right: f64, top: f64, zoom: u8) -> (u32, u32, u32, u32) {
let (min_col, min_row) = tile_index(left, top, zoom);
let (max_col, max_row) = tile_index(right, bottom, zoom);
(min_col, min_row, max_col, max_row)
}
#[must_use]
#[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn get_zoom_precision(zoom: u8) -> usize {
assert!(zoom <= MAX_ZOOM, "zoom {zoom} must be <= {MAX_ZOOM}");
let lng_delta = webmercator_to_wgs84(EARTH_CIRCUMFERENCE / f64::from(1_u32 << zoom), 0.0).0;
let log = lng_delta.log10() - 0.5;
if log > 0.0 { 0 } else { -log.ceil() as usize }
}
#[must_use]
pub fn webmercator_to_wgs84(x: f64, y: f64) -> (f64, f64) {
let lng = (x / EARTH_RADIUS).to_degrees();
let lat = f64::atan(f64::sinh(y / EARTH_RADIUS)).to_degrees();
(lng, lat)
}
#[must_use]
pub fn wgs84_to_webmercator(lon: f64, lat: f64) -> (f64, f64) {
let x = lon.to_radians() * EARTH_RADIUS;
let y_sin = lat.to_radians().sin();
let y = EARTH_RADIUS / 2.0 * ((1.0 + y_sin) / (1.0 - y_sin)).ln();
(x, y)
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case::minimal_tile(&[0x01, 0x01], Ok(()))]
#[case::one_byte_length(&[0x02, 0x01, 0xaa], Ok(()))]
#[case::two_byte_length(&[[0xac_u8, 0x02, 0x01].as_slice(), &[0xaa; 299]].concat(), Ok(()))]
#[case::multi_byte_length(&[[0x80_u8, 0x80, 0x01, 0x01].as_slice(), &[0xdd; 16383]].concat(), Ok(()))]
#[case::wrong_version(&[0x03, 0x02, 0xaa], Err(SevenBitDecodingError::UnexpectedTag(0x02)))]
#[case::empty_input(&[], Err(SevenBitDecodingError::TruncatedSize))]
#[case::size_overflow(&[0xFF; 64], Err(SevenBitDecodingError::SizeOverflow))]
#[case::size_underflow(&[0x00, 0x01], Err(SevenBitDecodingError::SizeUnderflow))]
#[case::unterminated_length(&[0x80], Err(SevenBitDecodingError::TruncatedSize))]
#[case::missing_version_byte(&[0x05], Err(SevenBitDecodingError::TruncatedTag))]
#[case::wrong_length(&[0x03, 0x01], Err(SevenBitDecodingError::TruncatedData { expected: 2, actual: 0 }))]
fn test_decode_7bit_length_and_tag(#[case] tile: &[u8], #[case] expected: Result<(), SevenBitDecodingError>) {
let allowed_versions = &[0x01_u8];
let decoded = decode_7bit_length_and_tag(tile, allowed_versions);
assert_eq!(decoded, expected, "can decode one layer correctly");
if tile.is_empty() {
return;
}
let mut tile_with_two_layers = vec![0x01, 0x01];
tile_with_two_layers.extend_from_slice(tile);
let decoded = decode_7bit_length_and_tag(&tile_with_two_layers, allowed_versions);
assert_eq!(decoded, expected, "can decode two layers correctly");
}
}