1use crate::TileId;
4
5pub trait MapConfig {
7 fn tile_url(&self, tile: &TileId) -> String;
9
10 fn attribution(&self) -> Option<&String>;
12
13 fn attribution_url(&self) -> Option<&String>;
15
16 fn default_center(&self) -> (f64, f64);
18
19 fn default_zoom(&self) -> u8;
21}
22
23#[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), 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#[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), 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 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}
196
197pub struct DynMapConfig {
206 tile_url: Box<dyn Fn(&TileId) -> String>,
207}
208
209impl DynMapConfig {
210 pub fn new(tile_url: impl Fn(&TileId) -> String + 'static) -> Self {
212 Self {
213 tile_url: Box::new(tile_url),
214 }
215 }
216}
217
218impl MapConfig for DynMapConfig {
219 fn tile_url(&self, tile: &TileId) -> String {
220 (self.tile_url)(tile)
221 }
222
223 fn attribution(&self) -> Option<&String> {
224 None
225 }
226
227 fn attribution_url(&self) -> Option<&String> {
228 None
229 }
230
231 fn default_center(&self) -> (f64, f64) {
232 (24.93545, 60.16952)
233 }
234
235 fn default_zoom(&self) -> u8 {
236 2
237 }
238}