use crate::tile_matrix_set::TileMatrixSet;
use crate::tms::Tms;
use once_cell::sync::OnceCell;
use std::collections::HashMap;
#[derive(Clone)]
pub struct TileMatrixSets {
coll: HashMap<String, TileMatrixSet>,
}
#[derive(thiserror::Error, Debug)]
pub enum RegistryError {
#[error("Tile Matrix set not found: `{0}`")]
TmsNotFound(String),
#[error("`{0}` is already a registered TMS")]
TmsAlreadyRegistered(String),
#[error(transparent)]
TmsError(#[from] crate::tms::TmsError),
}
impl TileMatrixSets {
pub fn new() -> Self {
Self {
coll: HashMap::new(),
}
}
pub fn get(&self, id: &str) -> Result<&TileMatrixSet, RegistryError> {
self.coll
.get(id)
.ok_or(RegistryError::TmsNotFound(id.to_string()))
}
pub fn lookup(&self, id: &str) -> Result<Tms, RegistryError> {
self.get(id)?.into_tms().map_err(Into::into)
}
pub fn list(&self) -> impl Iterator<Item = &String> {
self.coll.keys().into_iter()
}
pub fn register(
&mut self,
custom_tms: Vec<TileMatrixSet>,
overwrite: bool,
) -> Result<(), RegistryError> {
for tms in custom_tms {
if self.coll.contains_key(&tms.id) {
if overwrite {
self.coll.insert(tms.id.clone(), tms);
} else {
return Err(RegistryError::TmsAlreadyRegistered(tms.id));
}
} else {
self.coll.insert(tms.id.clone(), tms);
}
}
Ok(())
}
}
pub fn tms() -> &'static TileMatrixSets {
static TMS: OnceCell<TileMatrixSets> = OnceCell::new();
&TMS.get_or_init(|| {
let mut sets = TileMatrixSets::new();
let tms = vec![
#[cfg(feature = "projtransform")]
include_str!("../data/CanadianNAD83_LCC.json"),
#[cfg(feature = "projtransform")]
include_str!("../data/EuropeanETRS89_LAEAQuad.json"),
#[cfg(feature = "projtransform")]
include_str!("../data/UPSAntarcticWGS84Quad.json"),
#[cfg(feature = "projtransform")]
include_str!("../data/UPSArcticWGS84Quad.json"),
#[cfg(feature = "projtransform")]
include_str!("../data/UTM31WGS84Quad.json"),
include_str!("../data/WebMercatorQuad.json"),
include_str!("../data/WGS1984Quad.json"),
include_str!("../data/WorldMercatorWGS84Quad.json"),
]
.into_iter()
.map(|data| TileMatrixSet::from_json(&data).unwrap())
.collect::<Vec<_>>();
sets.register(tms, false).unwrap();
sets
})
}