Skip to main content

cityjson_types/backend/default/
metadata.rs

1use crate::prelude::StringStorage;
2use std::fmt::{Display, Formatter};
3
4/// Bounding Box.
5///
6/// A wrapper around an array of 6 values: `[minx, miny, minz, maxx, maxy, maxz]`.
7///
8/// # Examples
9/// ```
10/// # use cityjson_types::v2_0::BBox;
11/// let bbox = BBox::new(84710.1, 446846.0, -5.3, 84757.1, 446944.0, 40.9);
12/// let bbox_height = bbox.height();
13/// ```
14#[repr(C)]
15#[derive(Clone, Copy, Debug, PartialEq)]
16pub struct BBox {
17    values: [f64; 6],
18}
19
20impl BBox {
21    #[must_use]
22    pub fn new(min_x: f64, min_y: f64, min_z: f64, max_x: f64, max_y: f64, max_z: f64) -> Self {
23        Self {
24            values: [min_x, min_y, min_z, max_x, max_y, max_z],
25        }
26    }
27
28    #[must_use]
29    pub fn as_slice(&self) -> &[f64] {
30        &self.values
31    }
32
33    #[must_use]
34    pub fn min_x(&self) -> f64 {
35        self.values[0]
36    }
37
38    #[must_use]
39    pub fn min_y(&self) -> f64 {
40        self.values[1]
41    }
42
43    #[must_use]
44    pub fn min_z(&self) -> f64 {
45        self.values[2]
46    }
47
48    #[must_use]
49    pub fn max_x(&self) -> f64 {
50        self.values[3]
51    }
52
53    #[must_use]
54    pub fn max_y(&self) -> f64 {
55        self.values[4]
56    }
57
58    #[must_use]
59    pub fn max_z(&self) -> f64 {
60        self.values[5]
61    }
62
63    /// Calculates the width (x-axis length) of the bounding box.
64    #[must_use]
65    pub fn width(&self) -> f64 {
66        self.max_x() - self.min_x()
67    }
68
69    /// Calculates the length (y-axis length) of the bounding box.
70    #[must_use]
71    pub fn length(&self) -> f64 {
72        self.max_y() - self.min_y()
73    }
74
75    /// Calculates the height (z-axis length) of the bounding box.
76    #[must_use]
77    pub fn height(&self) -> f64 {
78        self.max_z() - self.min_z()
79    }
80}
81
82impl Default for BBox {
83    fn default() -> Self {
84        Self {
85            values: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
86        }
87    }
88}
89
90impl From<[f64; 6]> for BBox {
91    fn from(values: [f64; 6]) -> Self {
92        Self { values }
93    }
94}
95
96impl From<BBox> for [f64; 6] {
97    fn from(bbox: BBox) -> Self {
98        bbox.values
99    }
100}
101
102impl Display for BBox {
103    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
104        write!(
105            f,
106            "[{}, {}, {}, {}, {}, {}]",
107            self.min_x(),
108            self.min_y(),
109            self.min_z(),
110            self.max_x(),
111            self.max_y(),
112            self.max_z()
113        )
114    }
115}
116
117/// An identifier for the dataset.
118///
119/// # Examples
120/// ```
121/// # use cityjson_types::resources::storage::BorrowedStringStorage;
122/// # use cityjson_types::v2_0::CityModelIdentifier;
123/// let city_id: CityModelIdentifier<BorrowedStringStorage> = CityModelIdentifier::new("44574905-d2d2-4f40-8e96-d39e1ae45f70");
124/// ```
125#[repr(C)]
126#[derive(Clone, Debug, Default, PartialEq, PartialOrd, Ord, Eq, Hash)]
127pub struct CityModelIdentifier<SS: StringStorage>(SS::String);
128
129impl<SS: StringStorage> CityModelIdentifier<SS> {
130    pub fn new(value: SS::String) -> Self {
131        Self(value)
132    }
133
134    pub fn into_inner(self) -> SS::String {
135        self.0
136    }
137}
138
139impl<SS: StringStorage> Display for CityModelIdentifier<SS> {
140    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
141        write!(f, "{}", self.0)
142    }
143}
144
145/// The date when the dataset was compiled.
146///
147/// The format is a `"full-date"` per the
148/// [RFC 3339, Section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6).
149///
150/// # Examples
151/// ```
152/// # use cityjson_types::resources::storage::BorrowedStringStorage;
153/// # use cityjson_types::v2_0::Date;
154/// let date: Date<BorrowedStringStorage> = Date::new("1977-02-28");
155/// ```
156#[repr(C)]
157#[derive(Clone, Debug, Default, PartialEq, PartialOrd, Ord, Eq, Hash)]
158pub struct Date<SS: StringStorage>(SS::String);
159
160impl<SS: StringStorage> Date<SS> {
161    pub fn new(value: SS::String) -> Self {
162        Self(value)
163    }
164
165    pub fn into_inner(self) -> SS::String {
166        self.0
167    }
168}
169
170impl<SS: StringStorage> Display for Date<SS> {
171    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
172        write!(f, "{}", self.0)
173    }
174}
175
176/// The coordinate reference system (CRS) of the city model.
177///
178/// Must be formatted as a URL, according to the
179/// [OGC Name Type Specification](https://docs.opengeospatial.org/pol/09-048r5.html#_production_rule_for_specification_element_names).
180///
181/// # Examples
182/// ```
183/// # use cityjson_types::resources::storage::BorrowedStringStorage;
184/// # use cityjson_types::v2_0::CRS;
185/// let crs: CRS<BorrowedStringStorage> = CRS::new("https://www.opengis.net/def/crs/EPSG/0/7415");
186/// ```
187#[repr(C)]
188#[derive(Clone, Debug, Default, PartialEq, PartialOrd, Ord, Eq, Hash)]
189pub struct CRS<SS: StringStorage>(SS::String);
190
191impl<SS: StringStorage> CRS<SS> {
192    pub fn new(value: SS::String) -> Self {
193        Self(value)
194    }
195
196    pub fn into_inner(self) -> SS::String {
197        self.0
198    }
199}
200
201impl<SS: StringStorage> Display for CRS<SS> {
202    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
203        write!(f, "{}", self.0)
204    }
205}