use serde::{Deserialize, Serialize};
use serde_with::DisplayFromStr;
use std::num::{NonZeroU16, NonZeroU64};
use crate::common::{Crs, Links};
use super::{BoundingBox2D, Point2D, TitleDescriptionKeywords};
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TileMatrixSets {
pub tile_matrix_sets: Vec<TileMatrixSetItem>,
}
#[serde_with::serde_as]
#[serde_with::skip_serializing_none]
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TileMatrixSetItem {
pub id: Option<String>,
pub title: Option<String>,
pub uri: Option<String>,
#[serde(default)]
#[serde_as(as = "Option<DisplayFromStr>")]
pub crs: Option<Crs>,
pub links: Links,
}
#[serde_with::serde_as]
#[serde_with::skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TileMatrixSet {
#[serde(flatten)]
pub title_description_keywords: TitleDescriptionKeywords,
pub id: String,
pub uri: Option<String>,
#[serde_as(as = "DisplayFromStr")]
pub crs: Crs,
pub ordered_axes: Option<Vec<String>>,
pub well_known_scale_set: Option<String>,
pub bounding_box: Option<BoundingBox2D>,
pub tile_matrices: Vec<TileMatrix>,
}
#[serde_with::serde_as]
#[serde_with::skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TileMatrix {
#[serde(flatten)]
pub title_description_keywords: TitleDescriptionKeywords,
pub id: String,
pub scale_denominator: f64,
pub cell_size: f64,
pub corner_of_origin: Option<CornerOfOrigin>,
pub point_of_origin: Point2D,
pub tile_width: NonZeroU16,
pub tile_height: NonZeroU16,
pub matrix_width: NonZeroU64,
pub matrix_height: NonZeroU64,
pub variable_matrix_widths: Option<Vec<VariableMatrixWidth>>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct VariableMatrixWidth {
pub coalesc: NonZeroU64,
pub min_tile_row: u64,
pub smax_tile_row: u64,
}
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum CornerOfOrigin {
#[default]
TopLeft,
BottomLeft,
}
#[cfg(test)]
mod test {
use super::TileMatrixSet;
#[test]
fn parse_tms_example() {
let content =
std::fs::read_to_string("../ogcapi-services/assets/tms/WebMercartorQuad.json").unwrap();
let tms: TileMatrixSet = serde_json::from_str(&content).unwrap();
dbg!(&tms);
println!("{}", serde_json::to_string_pretty(&tms).unwrap());
}
}