galileo 0.2.1

Cross-platform general purpose map rendering engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use std::path::{Path, PathBuf};
use std::sync::Arc;

use bytes::Bytes;

use super::style::{
    StyleRule, VectorTileLineSymbol, VectorTilePolygonSymbol, VectorTileStyle, VectorTileSymbol,
};
use super::tile_provider::loader::WebVtLoader;
use super::tile_provider::processor::VectorTileProcessor;
use super::tile_provider::VectorTileProvider;
use super::VectorTileLayer;
use crate::error::GalileoError;
use crate::layer::attribution::Attribution;
use crate::layer::data_provider::{FileCacheController, PersistentCacheController, UrlSource};
use crate::layer::Layer;
use crate::tile_schema::TileIndex;
use crate::{Color, Messenger, TileSchema};

/// Constructor for a [`VectorTileLayer`].
///
/// ```
/// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
///
/// # fn load_tile_schema() -> galileo::TileSchema { galileo::TileSchema::web(10) }
/// # fn load_style() -> galileo::layer::vector_tile_layer::style::VectorTileStyle {
/// #     galileo::layer::vector_tile_layer::style::VectorTileStyle::default() }
///
/// let tile_schema = load_tile_schema();
/// let style = load_style();
///
/// let layer = VectorTileLayerBuilder::new_rest(
///     |index| {
///         format!(
///             "https://vector_tiles.example.com/{}/{}/{}.png",
///             index.z, index.x, index.y
///         )
///     })
///     .with_file_cache("target")
///     .with_tile_schema(tile_schema)
///     .with_style(style)
///     .build()?;
/// # Ok::<(), galileo::error::GalileoError>(())
/// ```
pub struct VectorTileLayerBuilder {
    provider_type: ProviderType,
    style: Option<VectorTileStyle>,
    tile_schema: Option<TileSchema>,
    messenger: Option<Box<dyn Messenger>>,
    cache: CacheType,
    offline_mode: bool,
    attribution: Option<Attribution>,
}

enum ProviderType {
    Rest(Box<dyn UrlSource<TileIndex>>),
    Custom(VectorTileProvider),
}

enum CacheType {
    None,
    File(PathBuf),
    Custom(Box<dyn PersistentCacheController<str, Bytes>>),
}

impl VectorTileLayerBuilder {
    /// Initializes a builder for a layer that requests tiles from the given url source.
    ///
    /// ```
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    ///
    /// let layer = VectorTileLayerBuilder::new_rest(
    ///     |index| {
    ///         format!(
    ///             "https://vector_tiles.example.com/{}/{}/{}.png",
    ///             index.z, index.x, index.y
    ///         )
    ///     }).build()?;
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn new_rest(tile_source: impl UrlSource<TileIndex> + 'static) -> Self {
        Self {
            provider_type: ProviderType::Rest(Box::new(tile_source)),
            style: None,
            tile_schema: None,
            messenger: None,
            cache: CacheType::None,
            offline_mode: false,
            attribution: None,
        }
    }

    /// Initializes a builder for a lyer with the given tile provider.
    ///
    /// ```
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    /// use galileo::layer::vector_tile_layer::tile_provider::VectorTileProvider;
    ///
    /// # fn get_tile_provider() -> VectorTileProvider {
    /// #     VectorTileLayerBuilder::new_rest(|_|
    /// #         unimplemented!()).build().unwrap().provider().clone()
    /// # }
    ///
    /// let provider = get_tile_provider();
    ///
    /// let layer = VectorTileLayerBuilder::new_with_provider(provider).build()?;
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn new_with_provider(provider: VectorTileProvider) -> Self {
        Self {
            provider_type: ProviderType::Custom(provider),
            style: None,
            tile_schema: None,
            messenger: None,
            cache: CacheType::None,
            offline_mode: false,
            attribution: None,
        }
    }

    /// Adds a file cache for the tiles in the given folder.
    ///
    /// The file cache controller will create folders under the given path based on the url of the
    /// layer, so different layers can use the same `path` for the tile cache.
    ///
    /// If the `path` folder doesn't exist it will be creating. In case the creation of the folder
    /// fails, building the tile layer will return an error.
    ///
    /// Cannot be used with custom tile provider given by
    /// [`VectorTileLayerBuilder::new_with_provider()`] method as the provider must have already be
    /// created with the cache configured. So in this case building will also return an error.
    ///
    /// Replaces the value set by the [`VectorTileLayerBuilder::with_cache_controller()`] method.
    ///
    /// # Platforms
    ///
    /// When compiling for the `wasm32` architecture, file system operations are not available, so
    /// using a file cache will result in a runtime error. If you want to use the same code to
    /// create a layer for all platforms and not worry about cache availability, you can use
    /// [`VectorTileLayerBuilder::with_file_cache_checked()`] method.
    ///
    /// ```
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    ///
    /// let layer = VectorTileLayerBuilder::new_rest(
    ///     |index| {
    ///         format!(
    ///             "https://vector_tiles.example.com/{}/{}/{}.png",
    ///             index.z, index.x, index.y
    ///         )
    ///     })
    ///     .with_file_cache("./target")
    ///     .build()?;
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn with_file_cache(mut self, path: impl AsRef<Path>) -> Self {
        // You would think that we don't need `with_file_cache_checked` method and can move its
        // logic here instead. But actually not all `wasm32` platforms don't have access to the FS,
        // and there is no simple way to detect if there is for the current target. So I'd rather
        // have both methods for future, when we want to add support for more platforms or have a
        // better way to check if the FS operations are available on the current target.
        self.cache = CacheType::File(path.as_ref().into());
        self
    }

    /// Sets the attribution for the vector tile layer with the given text and URL.
    ///
    /// This method allows specifying an attribution, typically used for citing sources
    /// or providing credit for the data being used. The attribution consists of a text
    /// description and an optional URL where more information or the source can be found.
    ///
    /// # Arguments
    ///
    /// * `text` - A `String` containing the text of the attribution.
    /// * `url` - A `String` containing the URL associated with the attribution.
    ///
    pub fn with_attribution(mut self, text: String, url: String) -> Self {
        self.attribution = Some(Attribution::new(text, Some(url)));
        self
    }

    /// Sets the file cache if available on the target platform, or skips it otherwise.
    ///
    /// Currently it only checks if the target architecture is "wasm32".
    ///
    /// ```
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    ///
    /// let layer = VectorTileLayerBuilder::new_rest(
    ///     |index| {
    ///         format!(
    ///             "https://vector_tiles.example.com/{}/{}/{}.png",
    ///             index.z, index.x, index.y
    ///         )
    ///     })
    ///     .with_file_cache_checked("./target")
    ///     .build()?;
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn with_file_cache_checked(self, _path: impl AsRef<Path>) -> Self {
        #[allow(unused_mut)]
        let mut this = self;
        #[cfg(not(target_arch = "wasm32"))]
        {
            this = this.with_file_cache(_path);
        }
        this
    }

    /// Adds the given persistent cache for the tiles.
    ///
    /// Cannot be used with custom tile provider given by
    /// [`VectorTileLayerBuilder::new_with_provider()`] method as the provider must have already be
    /// created with the cache configured. So in this case building will also return an error.
    ///
    /// Replaces the value set by the [`VectorTileLayerBuilder::with_file_cache()`] method.
    ///
    /// ```
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    /// use galileo::layer::data_provider::FileCacheController;
    ///
    /// let cache_controller = FileCacheController::new("target")?;
    /// let layer = VectorTileLayerBuilder::new_rest(
    ///     |index| {
    ///         format!(
    ///             "https://vector_tiles.example.com/{}/{}/{}.png",
    ///             index.z, index.x, index.y
    ///         )
    ///     })
    ///     .with_cache_controller(cache_controller)
    ///     .build()?;
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn with_cache_controller(
        mut self,
        cache: impl PersistentCacheController<str, Bytes> + 'static,
    ) -> Self {
        self.cache = CacheType::Custom(Box::new(cache));
        self
    }

    /// Sets the layer to only use cached tiles without requesting from the url source.
    ///
    /// Note that even in offline mode url source must be configured correctly as it will be used
    /// to identify tiles in the cache.
    ///
    /// Cannot be used with custom tile provider given by
    /// [`VectorTileLayerBuilder::new_with_provider()`] method as the provider must have already be
    /// created with the offline mode. So in this case building will also return an error.
    ///
    /// If the layer is set to offline mode but there is no cache configured, building it will
    /// return a configuration error.
    ///
    /// ```
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    ///
    /// let layer = VectorTileLayerBuilder::new_rest(
    ///     |index| {
    ///         format!(
    ///             "https://vector_tiles.example.com/{}/{}/{}.png",
    ///             index.z, index.x, index.y
    ///         )
    ///     })
    ///     .with_file_cache("./target")
    ///     .with_offline_mode()
    ///     .build()?;
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn with_offline_mode(mut self) -> Self {
        self.offline_mode = true;
        self
    }

    /// Sets the layer's tile schema.
    ///
    /// Defaults to `TileSchema::web(18)`. Note that for vector tiles you usually don't want to use
    /// the default schema as vector tiles usually are larger than 256 px.
    ///
    /// ```
    /// use galileo::layer::Layer;
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    /// use galileo::TileSchema;
    ///
    /// let layer = VectorTileLayerBuilder::new_rest(
    ///     |index| {
    ///         format!(
    ///             "https://vector_tiles.example.com/{}/{}/{}.png",
    ///             index.z, index.x, index.y
    ///         )
    ///     })
    ///     .with_tile_schema(TileSchema::web(10))
    ///     .build()?;
    ///
    /// assert_eq!(*layer.tile_schema().as_ref().unwrap(), TileSchema::web(10));
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn with_tile_schema(mut self, tile_schema: TileSchema) -> Self {
        self.tile_schema = Some(tile_schema);
        self
    }

    /// Sets the layer's messenger.
    ///
    /// Vector tile layer uses the messenger to notify application when a new tile is loaded and
    /// ready to be drawn. This is required since the tiles are loaded asynchronously.
    ///
    /// If the messenger is not set, after call to
    /// [`Layer::prepare()`](crate::layer::Layer::prepare()) drawing the layer will
    /// still not draw anything, since the tiles are not loaded yet.
    ///
    /// Setting the messenger separately for the layer is not required if the map is created with
    /// the [`MapBuilder`](crate::map::MapBuilder) and the messenger is set for the map.
    pub fn with_messenger(mut self, messenger: impl Messenger + 'static) -> Self {
        self.messenger = Some(Box::new(messenger));
        self
    }

    /// Sets the layer's style.
    ///
    /// ```
    /// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
    /// use galileo::layer::vector_tile_layer::style::VectorTileStyle;
    ///
    /// # fn load_style() -> VectorTileStyle { VectorTileStyle::default() }
    ///
    /// let style = load_style();
    ///
    /// let layer = VectorTileLayerBuilder::new_rest(
    ///     |index| {
    ///         format!(
    ///             "https://vector_tiles.example.com/{}/{}/{}.png",
    ///             index.z, index.x, index.y
    ///         )
    ///     })
    ///     .with_style(style)
    ///     .build()?;
    /// # Ok::<(), galileo::error::GalileoError>(())
    /// ```
    pub fn with_style(mut self, style: VectorTileStyle) -> Self {
        self.style = Some(style);
        self
    }

    /// Consumes the builder and constructs the vector tile layer.
    ///
    /// Will return an error if the layer is configured incorrectly or if the cache controller
    /// fails to initialize.
    pub fn build(self) -> Result<VectorTileLayer, GalileoError> {
        let Self {
            provider_type,
            style,
            tile_schema,
            messenger,
            cache,
            offline_mode,
            attribution,
        } = self;

        let tile_schema = tile_schema.unwrap_or_else(|| TileSchema::web(18));

        let cache_controller: Option<Box<dyn PersistentCacheController<str, Bytes>>> = match cache {
            CacheType::None => None,
            CacheType::File(path_buf) => Some(Box::new(FileCacheController::new(&path_buf)?)),
            CacheType::Custom(persistent_cache_controller) => Some(persistent_cache_controller),
        };

        if cache_controller.is_none() && offline_mode {
            return Err(GalileoError::Configuration(
                "offline mode cannot be used without cache".into(),
            ));
        }

        let processor = Self::create_processor(tile_schema.clone());

        let provider = match provider_type {
            ProviderType::Rest(url_source) => {
                let loader = WebVtLoader::new(cache_controller, url_source, offline_mode);

                VectorTileProvider::new(Arc::new(loader), Arc::new(processor))
            }
            ProviderType::Custom(raster_tile_provider) => {
                if cache_controller.is_some() {
                    return Err(GalileoError::Configuration(
                        "custom tile provider cannot be used together with a cache controller"
                            .into(),
                    ));
                }

                raster_tile_provider
            }
        };

        let style = style.unwrap_or_else(Self::default_style);

        let mut layer = VectorTileLayer::new(provider, style, tile_schema, attribution);
        if let Some(messenger) = messenger {
            layer.set_messenger(messenger);
        }

        Ok(layer)
    }

    fn create_processor(tile_schema: TileSchema) -> impl VectorTileProcessor {
        #[cfg(target_arch = "wasm32")]
        {
            crate::platform::web::vt_processor::WebWorkerVtProcessor::new(
                tile_schema.clone(),
                crate::platform::web::web_workers::WebWorkerService::instance(),
            )
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            crate::platform::native::vt_processor::ThreadVtProcessor::new(tile_schema.clone())
        }
    }

    fn default_style() -> VectorTileStyle {
        VectorTileStyle {
            rules: vec![
                StyleRule {
                    layer_name: None,
                    properties: Default::default(),
                    symbol: VectorTileSymbol::Line(VectorTileLineSymbol {
                        width: 1.0,
                        stroke_color: Color::BLACK,
                    }),
                },
                StyleRule {
                    layer_name: None,
                    properties: Default::default(),
                    symbol: VectorTileSymbol::Polygon(VectorTilePolygonSymbol {
                        fill_color: Color::GRAY,
                    }),
                },
            ],
            background: Color::WHITE,
        }
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_compact_debug_snapshot;

    use super::*;

    fn custom_provider() -> VectorTileProvider {
        VectorTileLayerBuilder::new_rest(|_| unimplemented!())
            .build()
            .unwrap()
            .provider()
            .clone()
    }

    #[test]
    fn with_file_cache_replaces_cache_controller() {
        let cache = FileCacheController::new("target").unwrap();
        let builder = VectorTileLayerBuilder::new_rest(|_| unimplemented!())
            .with_cache_controller(cache)
            .with_file_cache("target");

        assert!(matches!(builder.cache, CacheType::File(_)));
    }

    #[test]
    fn with_file_cache_fails_build_if_cannot_init_folder() {
        let result = VectorTileLayerBuilder::new_rest(|_| unimplemented!())
            .with_file_cache("Cargo.toml")
            .build();

        assert!(result.is_err());
        assert_compact_debug_snapshot!(result, @r#"Err(FsIo("failed to initialize file cache folder \"Cargo.toml\": File exists (os error 17)"))"#);
    }

    #[test]
    fn with_file_cache_fails_build_if_custom_provider() {
        let provider = custom_provider();
        let result = VectorTileLayerBuilder::new_with_provider(provider)
            .with_file_cache("target")
            .build();

        assert!(result.is_err());
        assert_compact_debug_snapshot!(result, @r#"Err(Configuration("custom tile provider cannot be used together with a cache controller"))"#);
    }

    #[test]
    fn with_cache_controller_replaces_file_cache() {
        let cache = FileCacheController::new("target").unwrap();
        let builder = VectorTileLayerBuilder::new_rest(|_| unimplemented!())
            .with_file_cache("target")
            .with_cache_controller(cache);

        assert!(matches!(builder.cache, CacheType::Custom(_)));
    }

    #[test]
    fn with_cache_controller_fails_build_if_custom_provider() {
        let provider = custom_provider();
        let cache = FileCacheController::new("target").unwrap();
        let result = VectorTileLayerBuilder::new_with_provider(provider)
            .with_cache_controller(cache)
            .build();

        assert!(result.is_err());
        assert_compact_debug_snapshot!(result, @r#"Err(Configuration("custom tile provider cannot be used together with a cache controller"))"#);
    }

    #[test]
    fn with_offline_mode_incompatible_with_custom_provider() {
        let provider = custom_provider();
        let result = VectorTileLayerBuilder::new_with_provider(provider)
            .with_file_cache("target")
            .with_offline_mode()
            .build();

        assert!(result.is_err());
        assert_compact_debug_snapshot!(result, @r#"Err(Configuration("custom tile provider cannot be used together with a cache controller"))"#);
    }

    #[test]
    fn with_offline_mode_does_not_work_without_cache() {
        let result = VectorTileLayerBuilder::new_rest(|_| unimplemented!())
            .with_offline_mode()
            .build();

        assert!(result.is_err());
        assert_compact_debug_snapshot!(result, @r#"Err(Configuration("offline mode cannot be used without cache"))"#);
    }

    #[test]
    fn default_tile_schema() {
        let layer = VectorTileLayerBuilder::new_rest(|_| unimplemented!())
            .build()
            .unwrap();

        assert_eq!(*layer.tile_schema().as_ref().unwrap(), TileSchema::web(18));
    }
}