egui_map_view/
config.rs

1//! Configuration for different map providers.
2
3use crate::TileId;
4
5/// Configuration for a map provider.
6pub trait MapConfig {
7    /// Returns the URL for a given tile.
8    fn tile_url(&self, tile: &TileId) -> String;
9
10    /// Returns the attribution text to be displayed on the map. If returns `None`, no attribution is shown.
11    fn attribution(&self) -> Option<&String>;
12
13    /// Returns the attribution URL to be linked from the attribution text.
14    fn attribution_url(&self) -> Option<&String>;
15
16    /// The default geographical center of the map. (longitude, latitude)
17    fn default_center(&self) -> (f64, f64);
18
19    /// The default zoom level of the map.
20    fn default_zoom(&self) -> u8;
21}
22
23/// Configuration for the OpenStreetMap tile server.
24///
25/// # Example
26///
27/// ```
28/// use egui_map_view::config::OpenStreetMapConfig;
29/// let config = OpenStreetMapConfig::default();
30/// ```
31#[cfg(feature = "openstreetmap")]
32pub struct OpenStreetMapConfig {
33    base_url: String,
34    attribution: String,
35    attribution_url: String,
36    default_center: (f64, f64),
37    default_zoom: u8,
38}
39
40#[cfg(feature = "openstreetmap")]
41impl Default for OpenStreetMapConfig {
42    fn default() -> Self {
43        Self {
44            base_url: "https://tile.openstreetmap.org".to_string(),
45            attribution: "© OpenStreetMap contributors".to_string(),
46            attribution_url: "https://www.openstreetmap.org".to_string(),
47            default_center: (24.93545, 60.16952), // Helsinki, Finland
48            default_zoom: 5,
49        }
50    }
51}
52
53#[cfg(feature = "openstreetmap")]
54impl MapConfig for OpenStreetMapConfig {
55    fn tile_url(&self, tile: &TileId) -> String {
56        format!("{}/{}/{}/{}.png", self.base_url, tile.z, tile.x, tile.y)
57    }
58
59    fn attribution(&self) -> Option<&String> {
60        Some(&self.attribution)
61    }
62
63    fn attribution_url(&self) -> Option<&String> {
64        Some(&self.attribution_url)
65    }
66
67    fn default_center(&self) -> (f64, f64) {
68        self.default_center
69    }
70
71    fn default_zoom(&self) -> u8 {
72        self.default_zoom
73    }
74}
75
76/// Configuration for the Karttapaikka tile server.
77///
78/// # Example
79///
80/// ```
81/// use egui_map_view::config::KarttapaikkaMapConfig;
82/// let config = KarttapaikkaMapConfig::new("my-api-key".to_string());
83/// ```
84#[cfg(feature = "karttapaikka")]
85pub struct KarttapaikkaMapConfig {
86    base_url: String,
87    attribution: String,
88    attribution_url: String,
89    default_center: (f64, f64),
90    default_zoom: u8,
91    api_key: String,
92}
93
94#[cfg(feature = "karttapaikka")]
95impl Default for KarttapaikkaMapConfig {
96    fn default() -> Self {
97        Self {
98            base_url: "https://avoin-karttakuva.maanmittauslaitos.fi/avoin/wmts/1.0.0/maastokartta/default/WGS84_Pseudo-Mercator".to_string(),
99            attribution: "© Maanmittauslaitos".to_string(),
100            attribution_url: "https://www.maanmittauslaitos.fi/asioi-verkossa/karttapaikka".to_string(),
101            default_center: (24.93545, 60.16952), // Helsinki, Finland
102            default_zoom: 15,
103            api_key: "your-key-here".to_string(),
104        }
105    }
106}
107
108#[cfg(feature = "karttapaikka")]
109impl MapConfig for KarttapaikkaMapConfig {
110    fn tile_url(&self, tile: &TileId) -> String {
111        format!(
112            "{}/{}/{}/{}.png?api-key={}",
113            self.base_url, tile.z, tile.y, tile.x, self.api_key
114        )
115    }
116
117    fn attribution(&self) -> Option<&String> {
118        Some(&self.attribution)
119    }
120
121    fn attribution_url(&self) -> Option<&String> {
122        Some(&self.attribution_url)
123    }
124
125    fn default_center(&self) -> (f64, f64) {
126        self.default_center
127    }
128
129    fn default_zoom(&self) -> u8 {
130        self.default_zoom
131    }
132}
133
134#[cfg(feature = "karttapaikka")]
135impl KarttapaikkaMapConfig {
136    /// Creates a new `KarttapaikkaMapConfig` with the given API key.
137    pub fn new(api_key: String) -> Self {
138        let mut config = Self::default();
139        config.api_key = api_key;
140        config
141    }
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147    use crate::TileId;
148
149    #[test]
150    #[cfg(feature = "openstreetmap")]
151    fn openstreetmap_config_default() {
152        let config = OpenStreetMapConfig::default();
153        assert_eq!(config.base_url, "https://tile.openstreetmap.org");
154        assert_eq!(config.attribution, "© OpenStreetMap contributors");
155        assert_eq!(config.default_center, (24.93545, 60.16952));
156        assert_eq!(config.default_zoom, 5);
157    }
158
159    #[test]
160    #[cfg(feature = "openstreetmap")]
161    fn openstreetmap_config_tile_url() {
162        let config = OpenStreetMapConfig::default();
163        let tile_id = TileId { z: 10, x: 1, y: 2 };
164        let url = config.tile_url(&tile_id);
165        assert_eq!(url, "https://tile.openstreetmap.org/10/1/2.png");
166    }
167
168    #[test]
169    #[cfg(feature = "karttapaikka")]
170    fn karttapaikka_config_new() {
171        let api_key = "test-api-key".to_string();
172        let config = KarttapaikkaMapConfig::new(api_key.clone());
173        assert_eq!(config.api_key, api_key);
174        assert_eq!(
175            config.base_url,
176            "https://avoin-karttakuva.maanmittauslaitos.fi/avoin/wmts/1.0.0/maastokartta/default/WGS84_Pseudo-Mercator"
177        );
178        assert_eq!(config.attribution, "© Maanmittauslaitos");
179        assert_eq!(config.default_center, (24.93545, 60.16952));
180        assert_eq!(config.default_zoom, 15);
181    }
182
183    #[test]
184    #[cfg(feature = "karttapaikka")]
185    fn karttapaikka_config_tile_url() {
186        let api_key = "test-api-key".to_string();
187        let config = KarttapaikkaMapConfig::new(api_key.clone());
188        let tile_id = TileId { z: 10, x: 1, y: 2 };
189        let url = config.tile_url(&tile_id);
190        assert_eq!(
191            url,
192            "https://avoin-karttakuva.maanmittauslaitos.fi/avoin/wmts/1.0.0/maastokartta/default/WGS84_Pseudo-Mercator/10/2/1.png?api-key=test-api-key"
193        );
194    }
195}