Skip to content

Commit

Permalink
Merge pull request #169 from podusowski/sources-module
Browse files Browse the repository at this point in the history
Sources module
  • Loading branch information
podusowski committed Jun 28, 2024
2 parents e46f94e + f4b903f commit c818161
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 142 deletions.
142 changes: 0 additions & 142 deletions walkers/src/sources.rs

This file was deleted.

32 changes: 32 additions & 0 deletions walkers/src/sources/geoportal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::{Attribution, TileSource};
use crate::TileId;

/// Orthophotomap layer from Poland's Geoportal.
/// <https://www.geoportal.gov.pl/uslugi/usluga-przegladania-wms>
pub struct Geoportal;

impl TileSource for Geoportal {
fn tile_url(&self, tile_id: TileId) -> String {
format!(
"https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMTS/StandardResolution?\
&SERVICE=WMTS\
&REQUEST=GetTile\
&VERSION=1.0.0\
&LAYER=ORTOFOTOMAPA\
&TILEMATRIXSET=EPSG:3857\
&TILEMATRIX=EPSG:3857:{}\
&TILEROW={}\
&TILECOL={}",
tile_id.zoom, tile_id.y, tile_id.x
)
}

fn attribution(&self) -> Attribution {
Attribution {
text: "Główny Urząd Geodezji i Kartografii",
url: "https://www.geoportal.gov.pl/",
logo_light: None,
logo_dark: None,
}
}
}
73 changes: 73 additions & 0 deletions walkers/src/sources/mapbox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::TileId;

use super::{Attribution, TileSource};

/// Predefined Mapbox styles.
/// <https://docs.mapbox.com/api/maps/styles/#classic-mapbox-styles>
#[derive(Clone, Copy, Default)]
pub enum MapboxStyle {
#[default]
Streets,
Outdoors,
Light,
Dark,
Satellite,
SatelliteStreets,
NavigationDay,
NavigationNight,
}

impl MapboxStyle {
fn api_slug(&self) -> &'static str {
match self {
Self::Streets => "streets-v12",
Self::Outdoors => "outdoors-v12",
Self::Light => "light-v11",
Self::Dark => "dark-v11",
Self::Satellite => "satellite-v9",
Self::SatelliteStreets => "satellite-streets-v12",
Self::NavigationDay => "navigation-day-v1",
Self::NavigationNight => "navigation-night-v1",
}
}
}

/// Mapbox static tile source.
/// <https://docs.mapbox.com/api/maps/static-tiles/>
#[derive(Default)]
pub struct Mapbox {
/// Predefined style to use
pub style: MapboxStyle,
/// Render tiles at 1024x1024 instead of 512x512 (@2x)
pub high_resolution: bool,
/// Mapbox API key, required
pub access_token: String,
}

impl TileSource for Mapbox {
fn tile_url(&self, tile_id: TileId) -> String {
format!(
"https://api.mapbox.com/styles/v1/mapbox/{}/tiles/512/{}/{}/{}{}?access_token={}",
self.style.api_slug(),
tile_id.zoom,
tile_id.x,
tile_id.y,
if self.high_resolution { "@2x" } else { "" },
self.access_token
)
}

fn attribution(&self) -> Attribution {
// TODO: Proper linking (https://docs.mapbox.com/help/getting-started/attribution/))
Attribution {
text: "© Mapbox, © OpenStreetMap",
url: "https://www.mapbox.com/about/maps/",
logo_light: Some(egui::include_image!("../../assets/mapbox-logo-white.svg")),
logo_dark: Some(egui::include_image!("../../assets/mapbox-logo-black.svg")),
}
}

fn tile_size(&self) -> u32 {
512
}
}
28 changes: 28 additions & 0 deletions walkers/src/sources/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Some common HTTP tile sources. Make sure you follow terms of usage of the particular source.

mod geoportal;
mod mapbox;
mod openstreetmap;

use crate::mercator::TileId;
pub use geoportal::Geoportal;
pub use mapbox::{Mapbox, MapboxStyle};
pub use openstreetmap::OpenStreetMap;

#[derive(Clone)]
pub struct Attribution {
pub text: &'static str,
pub url: &'static str,
pub logo_light: Option<egui::ImageSource<'static>>,
pub logo_dark: Option<egui::ImageSource<'static>>,
}

pub trait TileSource {
fn tile_url(&self, tile_id: TileId) -> String;
fn attribution(&self) -> Attribution;

/// Size of each tile, should be a multiple of 256
fn tile_size(&self) -> u32 {
256
}
}
23 changes: 23 additions & 0 deletions walkers/src/sources/openstreetmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::{Attribution, TileSource};
use crate::TileId;

/// <https://www.openstreetmap.org/about>
pub struct OpenStreetMap;

impl TileSource for OpenStreetMap {
fn tile_url(&self, tile_id: TileId) -> String {
format!(
"https://tile.openstreetmap.org/{}/{}/{}.png",
tile_id.zoom, tile_id.x, tile_id.y
)
}

fn attribution(&self) -> Attribution {
Attribution {
text: "OpenStreetMap contributors",
url: "https://www.openstreetmap.org/copyright",
logo_light: None,
logo_dark: None,
}
}
}

0 comments on commit c818161

Please sign in to comment.