tile_grid/
tile_matrix_set.rs

1use ogcapi_types::tiles::{OrderedAxes, TileMatrixSet};
2use std::path::PathBuf;
3
4#[derive(thiserror::Error, Debug)]
5pub enum TileMatrixSetError {
6    #[error(transparent)]
7    JsonError(#[from] serde_json::Error),
8    #[error("{0}: {1}")]
9    FileError(PathBuf, #[source] std::io::Error),
10}
11
12pub trait TileMatrixSetOps: Sized {
13    fn from_json_file(json_path: &str) -> Result<Self, TileMatrixSetError>;
14    fn from_json(json: &str) -> Result<Self, TileMatrixSetError>;
15    /// Check if CRS has inverted AXIS (lat,lon) instead of (lon,lat).
16    fn crs_axis_inverted(&self) -> bool;
17}
18
19impl TileMatrixSetOps for TileMatrixSet {
20    fn from_json_file(json_path: &str) -> Result<Self, TileMatrixSetError> {
21        let content = std::fs::read_to_string(json_path)
22            .map_err(|e| TileMatrixSetError::FileError(json_path.into(), e))?;
23        TileMatrixSet::from_json(&content)
24    }
25    fn from_json(json: &str) -> Result<Self, TileMatrixSetError> {
26        serde_json::from_str(json).map_err(Into::into)
27    }
28    /// Check if CRS has inverted AXIS (lat,lon) instead of (lon,lat).
29    fn crs_axis_inverted(&self) -> bool {
30        if let Some(axes) = &self.ordered_axes {
31            ordered_axes_inverted(axes)
32        } else {
33            false // TODO: Check CRS axis ordering
34        }
35    }
36}
37
38pub(crate) fn ordered_axes_inverted(axes: &OrderedAxes) -> bool {
39    first_axes_inverted(&axes[0].to_uppercase())
40}
41
42fn first_axes_inverted(first: &str) -> bool {
43    first == "Y" || first == "LAT" || first == "N"
44}
45
46#[cfg(test)]
47mod test {
48    use super::TileMatrixSetOps;
49    use ogcapi_types::tiles::TileMatrixSet;
50
51    #[test]
52    fn parse_tms_example() {
53        let tms = TileMatrixSet::from_json_file("./data/WebMercatorQuad.json").unwrap();
54        println!("{}", serde_json::to_string_pretty(&tms).unwrap());
55    }
56}