Skip to main content

mln_style/
tileset.rs

1#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
2pub enum Scheme {
3    #[default]
4    Xyz,
5    Tms,
6}
7
8/// How a raster DEM tileset encodes elevation in its pixels.
9#[derive(Clone, Copy, Debug, PartialEq, mln_wire::Wire)]
10pub enum RasterEncoding {
11    /// Mapbox DEM
12    Mapbox,
13    /// Terrarium DEM
14    Terrarium,
15    /// The style spec's `custom` DEM: a pixel's elevation is
16    /// `r * red_factor + g * green_factor + b * blue_factor - base_shift`,
17    /// with each channel from 0 to 255.
18    Custom { red_factor: f32, green_factor: f32, blue_factor: f32, base_shift: f32 },
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22pub enum VectorEncoding {
23    /// Mapbox Vector Tiles (MVT)
24    Mapbox,
25    /// MapLibre Tiles
26    Mlt,
27}
28
29/// A source's TileJSON. TileJSON also includes center and zoom, which the
30/// renderer doesn't use.
31#[derive(Clone, Debug, PartialEq)]
32pub struct Tileset {
33    pub tiles: Vec<String>,
34    pub zoom_range: crate::range::Range<u8>,
35    pub attribution: String,
36    pub scheme: Scheme,
37    pub raster_encoding: Option<RasterEncoding>,
38    pub vector_encoding: Option<VectorEncoding>,
39    pub bounds: Option<crate::geo::LatLngBounds>,
40    /// The source's `sparse`. When it's set, an empty tile
41    /// response (HTTP 204, or no tile data from an archive such as PMTiles) means the tileset has
42    /// no tile there at that zoom level, and a tile from a lower zoom level may be drawn in its
43    /// place. Otherwise it's a tile with nothing at that place.
44    pub sparse: bool,
45}
46
47impl Default for Tileset {
48    fn default() -> Self {
49        Tileset {
50            tiles: Vec::new(),
51            zoom_range: crate::range::Range::new(0, crate::constants::DEFAULT_MAX_ZOOM),
52            attribution: String::new(),
53            scheme: Scheme::Xyz,
54            raster_encoding: None,
55            vector_encoding: None,
56            bounds: None,
57            sparse: false,
58        }
59    }
60}
61
62/// A tileset's members as a TileJSON document or a style source lists them, each optional, so
63/// that a style source's members can win over those of the TileJSON at its URL.
64#[derive(Clone, Debug, Default, PartialEq)]
65pub struct PartialTileset {
66    pub tiles: Option<Vec<String>>,
67    pub min_zoom: Option<u8>,
68    pub max_zoom: Option<u8>,
69    pub attribution: Option<String>,
70    pub scheme: Option<Scheme>,
71    pub raster_encoding: Option<RasterEncoding>,
72    pub vector_encoding: Option<VectorEncoding>,
73    pub bounds: Option<crate::geo::LatLngBounds>,
74    pub sparse: Option<bool>,
75}
76
77impl PartialTileset {
78    /// `self`'s members, with `fallback`'s in place of those `self` doesn't have.
79    #[must_use]
80    pub fn or(self, fallback: PartialTileset) -> PartialTileset {
81        PartialTileset {
82            tiles: self.tiles.or(fallback.tiles),
83            min_zoom: self.min_zoom.or(fallback.min_zoom),
84            max_zoom: self.max_zoom.or(fallback.max_zoom),
85            attribution: self.attribution.or(fallback.attribution),
86            scheme: self.scheme.or(fallback.scheme),
87            raster_encoding: self.raster_encoding.or(fallback.raster_encoding),
88            vector_encoding: self.vector_encoding.or(fallback.vector_encoding),
89            bounds: self.bounds.or(fallback.bounds),
90            sparse: self.sparse.or(fallback.sparse),
91        }
92    }
93}
94
95/// The tileset, with [`Tileset::default`]'s members in place of those it doesn't have.
96impl From<PartialTileset> for Tileset {
97    fn from(partial: PartialTileset) -> Self {
98        let default = Tileset::default();
99        Tileset {
100            tiles: partial.tiles.unwrap_or_default(),
101            zoom_range: crate::range::Range::new(
102                partial.min_zoom.unwrap_or(default.zoom_range.min),
103                partial.max_zoom.unwrap_or(default.zoom_range.max),
104            ),
105            attribution: partial.attribution.unwrap_or_default(),
106            scheme: partial.scheme.unwrap_or_default(),
107            raster_encoding: partial.raster_encoding,
108            vector_encoding: partial.vector_encoding,
109            bounds: partial.bounds,
110            sparse: partial.sparse.unwrap_or_default(),
111        }
112    }
113}