Skip to content

Commit

Permalink
Add methods to manipulate map layer list (#43)
Browse files Browse the repository at this point in the history
Add `LayerCollection` struct with some methods to change the layers list of a map.

Also provides `as_any` method on layers to allow downcasting layers from the list into concrete types.

Fixes #39
  • Loading branch information
Maximkaaa authored Feb 6, 2024
1 parent ddec270 commit 74f3524
Show file tree
Hide file tree
Showing 10 changed files with 711 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Build
run: cargo build --verbose --all
- name: Tests
run: cargo test --verbose
run: cargo test --features _tests --verbose

fmt:
name: Rustfmt
Expand Down
3 changes: 3 additions & 0 deletions galileo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ default = ["wgpu", "serde", "winit"]
wgpu = ["dep:wgpu", "raw-window-handle"]
geojson = ["dep:geojson", "galileo-types/geojson"]

# Used to provide some fixtures for doctests
_tests = []

[dependencies]
cfg-if = "1"
async-trait = "0.1.68"
Expand Down
18 changes: 12 additions & 6 deletions galileo/src/galileo_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,25 @@ impl MapBuilder {
self
}

pub fn create_raster_tile_layer(
tile_source: impl UrlSource<TileIndex> + 'static,
tile_scheme: TileSchema,
) -> RasterTileLayer<UrlImageProvider<TileIndex, FileCacheController>> {
let cache_controller = Some(FileCacheController::new(".tile_cache"));

let tile_provider = UrlImageProvider::new(tile_source, cache_controller);
RasterTileLayer::new(tile_scheme, tile_provider, None)
}

#[cfg(not(target_arch = "wasm32"))]
pub fn with_raster_tiles(
mut self,
tile_source: impl UrlSource<TileIndex> + 'static,
tile_scheme: TileSchema,
) -> Self {
let cache_controller = Some(FileCacheController::new(".tile_cache"));

let tile_provider = UrlImageProvider::new(tile_source, cache_controller);
self.layers.push(Box::new(RasterTileLayer::new(
self.layers.push(Box::new(Self::create_raster_tile_layer(
tile_source,
tile_scheme,
tile_provider,
None,
)));
self
}
Expand Down
39 changes: 32 additions & 7 deletions galileo/src/layer/feature_layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use galileo_types::geometry::{CartesianGeometry2d, Geom, Geometry};
use galileo_types::geometry_type::{CartesianSpace2d, CartesianSpace3d, GeoSpace2d};
use maybe_sync::{MaybeSend, MaybeSync};
use num_traits::AsPrimitive;
use std::any::Any;
use std::marker::PhantomData;
use std::sync::{Arc, RwLock};

Expand Down Expand Up @@ -283,9 +284,9 @@ where
impl<P, F, S> Layer for FeatureLayer<P, F, S, GeoSpace2d>
where
P: NewGeoPoint + 'static,
F: Feature + MaybeSend + MaybeSync,
F: Feature + MaybeSend + MaybeSync + 'static,
F::Geom: Geometry<Point = P>,
S: Symbol<F> + MaybeSend + MaybeSync,
S: Symbol<F> + MaybeSend + MaybeSync + 'static,
{
fn render(&self, view: &MapView, canvas: &mut dyn Canvas) {
if self.features.is_empty() {
Expand Down Expand Up @@ -336,14 +337,22 @@ where
fn set_messenger(&mut self, messenger: Box<dyn Messenger>) {
*self.messenger.write().unwrap() = Some(messenger);
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}

impl<P, F, S> Layer for FeatureLayer<P, F, S, CartesianSpace2d>
where
P: NewCartesianPoint2d + Clone + 'static,
F: Feature + MaybeSend + MaybeSync,
F: Feature + MaybeSend + MaybeSync + 'static,
F::Geom: Geometry<Point = P>,
S: Symbol<F> + MaybeSend + MaybeSync,
S: Symbol<F> + MaybeSend + MaybeSync + 'static,
{
fn render(&self, view: &MapView, canvas: &mut dyn Canvas) {
let lod = self.select_lod(view.resolution());
Expand Down Expand Up @@ -401,15 +410,23 @@ where
fn set_messenger(&mut self, messenger: Box<dyn Messenger>) {
*self.messenger.write().unwrap() = Some(messenger);
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}

impl<P, F, S> Layer for FeatureLayer<P, F, S, CartesianSpace3d>
where
P: NewCartesianPoint3d,
P: NewCartesianPoint3d + 'static,
P::Num: AsPrimitive<f32>,
F: Feature + MaybeSend + MaybeSync,
F: Feature + MaybeSend + MaybeSync + 'static,
F::Geom: Geometry<Point = P>,
S: Symbol<F> + MaybeSend + MaybeSync,
S: Symbol<F> + MaybeSend + MaybeSync + 'static,
{
fn render(&self, view: &MapView, canvas: &mut dyn Canvas) {
if view.crs() != &self.crs {
Expand Down Expand Up @@ -461,4 +478,12 @@ where
fn set_messenger(&mut self, messenger: Box<dyn Messenger>) {
*self.messenger.write().unwrap() = Some(messenger);
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
40 changes: 39 additions & 1 deletion galileo/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::messenger::Messenger;
use crate::render::{Canvas, Renderer};
use crate::view::MapView;
use maybe_sync::{MaybeSend, MaybeSync};
use std::any::Any;
use std::sync::{Arc, RwLock};

pub mod data_provider;
Expand All @@ -18,9 +19,11 @@ pub trait Layer: MaybeSend + MaybeSync {
fn render(&self, view: &MapView, canvas: &mut dyn Canvas);
fn prepare(&self, view: &MapView, renderer: &Arc<RwLock<dyn Renderer>>);
fn set_messenger(&mut self, messenger: Box<dyn Messenger>);
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}

impl<T: Layer> Layer for Arc<RwLock<T>> {
impl<T: Layer + 'static> Layer for Arc<RwLock<T>> {
fn render(&self, position: &MapView, canvas: &mut dyn Canvas) {
self.read().unwrap().render(position, canvas)
}
Expand All @@ -32,4 +35,39 @@ impl<T: Layer> Layer for Arc<RwLock<T>> {
fn set_messenger(&mut self, messenger: Box<dyn Messenger>) {
self.write().unwrap().set_messenger(messenger)
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}

#[cfg(feature = "_tests")]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TestLayer(pub &'static str);

#[cfg(feature = "_tests")]
impl Layer for TestLayer {
fn render(&self, _view: &MapView, _canvas: &mut dyn Canvas) {
unimplemented!()
}

fn prepare(&self, _view: &MapView, _renderer: &Arc<RwLock<dyn Renderer>>) {
unimplemented!()
}

fn set_messenger(&mut self, _messenger: Box<dyn Messenger>) {
unimplemented!()
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
9 changes: 9 additions & 0 deletions galileo/src/layer/raster_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::tile_scheme::{TileIndex, TileSchema};
use crate::view::MapView;
use maybe_sync::{MaybeSend, MaybeSync, Mutex};
use quick_cache::sync::Cache;
use std::any::Any;
use std::collections::HashSet;
use std::sync::{Arc, RwLock};
use web_time::{Duration, SystemTime};
Expand Down Expand Up @@ -335,4 +336,12 @@ where
fn set_messenger(&mut self, messenger: Box<dyn Messenger>) {
self.messenger = Some(Arc::from(messenger));
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
9 changes: 9 additions & 0 deletions galileo/src/layer/vector_tile_layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::render::{Canvas, PackedBundle, RenderOptions, Renderer};
use crate::tile_scheme::TileSchema;
use crate::view::MapView;
use nalgebra::Point2;
use std::any::Any;
use std::collections::HashSet;
use std::sync::{Arc, RwLock};

Expand Down Expand Up @@ -46,6 +47,14 @@ impl<Provider: VectorTileProvider + 'static> Layer for VectorTileLayer<Provider>
fn set_messenger(&mut self, messenger: Box<dyn Messenger>) {
self.tile_provider.set_messenger(messenger);
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}

impl<Provider: VectorTileProvider> VectorTileLayer<Provider> {
Expand Down
Loading

0 comments on commit 74f3524

Please sign in to comment.