maplibre/io/
source_type.rs

1use crate::{coords::WorldTileCoords, style::source::TileAddressingScheme};
2
3/// Represents a source from which the vector tile are fetched.
4#[derive(Clone)]
5pub struct TessellateSource {
6    pub url: String,
7    pub filetype: String,
8}
9
10impl TessellateSource {
11    pub fn new(url: &str, filetype: &str) -> Self {
12        Self {
13            url: url.to_string(),
14            filetype: filetype.to_string(),
15        }
16    }
17
18    pub fn format(&self, coords: &WorldTileCoords) -> String {
19        let tile_coords = coords.into_tile(TileAddressingScheme::XYZ).unwrap();
20        format!(
21            "{url}/{z}/{x}/{y}.{filetype}",
22            url = self.url,
23            z = tile_coords.z,
24            x = tile_coords.x,
25            y = tile_coords.y,
26            filetype = self.filetype,
27        )
28    }
29}
30
31impl Default for TessellateSource {
32    fn default() -> Self {
33        Self::new("https://maps.tuerantuer.org/europe_germany", "pbf")
34        // Self::new("https://demotiles.maplibre.org/tiles", "pbf")
35    }
36}
37
38/// Represents a source from which the raster tile are fetched.
39#[derive(Clone)]
40pub struct RasterSource {
41    pub url: String,
42    pub filetype: String,
43    pub key: String,
44}
45
46impl RasterSource {
47    pub fn new(url: &str, filetype: &str, key: &str) -> Self {
48        Self {
49            url: url.to_string(),
50            filetype: filetype.to_string(),
51            key: key.to_string(),
52        }
53    }
54
55    pub fn format(&self, coords: &WorldTileCoords) -> String {
56        let tile_coords = coords.into_tile(TileAddressingScheme::XYZ).unwrap();
57        format!(
58            "{url}/{z}/{x}/{y}.{filetype}?key={key}",
59            url = self.url,
60            z = tile_coords.z,
61            x = tile_coords.x,
62            y = tile_coords.y,
63            filetype = self.filetype,
64            key = self.key,
65        )
66    }
67}
68
69impl Default for RasterSource {
70    fn default() -> Self {
71        Self::new(
72            "https://api.maptiler.com/tiles/satellite-v2",
73            "jpg",
74            "qnePkfbGpMsLCi3KFBs3",
75        )
76    }
77}
78
79/// Represents the tiles' different types of source.
80#[derive(Clone)]
81pub enum SourceType {
82    Raster(RasterSource),
83    Tessellate(TessellateSource),
84}
85
86impl SourceType {
87    pub fn format(&self, coords: &WorldTileCoords) -> String {
88        match self {
89            SourceType::Raster(raster_source) => raster_source.format(coords),
90            SourceType::Tessellate(tessellate_source) => tessellate_source.format(coords),
91        }
92    }
93}