cityjson 0.7.1

Types and accessor methods for representing semantic 3D city models in Rust, implementing the CityJSON specifications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! City objects — the features in a `CityJSON` dataset.
//!
//! Each entry in the `CityObjects` map is a [`CityObject`] with a string ID, a type, optional
//! geometry references, optional attributes, and optional parent/child relationships.
//!
//! **1st-level types** can exist without a parent:
//!
//! `Bridge`, `Building`, `CityFurniture`, `CityObjectGroup`, `GenericCityObject`, `LandUse`,
//! `OtherConstruction`, `PlantCover`, `SolitaryVegetationObject`, `TINRelief`, `Road`,
//! `Railway`, `TransportSquare`, `Waterway`, `WaterBody`, `Tunnel`
//!
//! **2nd-level types** must reference at least one parent:
//!
//! `BridgeConstructiveElement`, `BridgeFurniture`, `BridgeInstallation`, `BridgePart`,
//! `BridgeRoom`, `BuildingConstructiveElement`, `BuildingFurniture`, `BuildingInstallation`,
//! `BuildingPart`, `BuildingRoom`, `BuildingStorey`, `BuildingUnit`,
//! `TunnelConstructiveElement`, `TunnelFurniture`, `TunnelHollowSpace`, `TunnelInstallation`,
//! `TunnelPart`
//!
//! ```rust
//! use cityjson::CityModelType;
//! use cityjson::v2_0::{
//!     CityObject, CityObjectIdentifier, CityObjectType, OwnedAttributeValue, OwnedCityModel,
//! };
//!
//! let mut model = OwnedCityModel::new(CityModelType::CityJSON);
//!
//! let mut building = CityObject::new(
//!     CityObjectIdentifier::new("building-001".to_string()),
//!     CityObjectType::Building,
//! );
//! building
//!     .attributes_mut()
//!     .insert("measuredHeight".to_string(), OwnedAttributeValue::Float(15.3));
//!
//! let handle = model.cityobjects_mut().add(building).unwrap();
//! assert!(model.cityobjects().get(handle).is_some());
//! ```

use crate::backend::default::cityobject::{CityObjectCore, CityObjectsCore};
use crate::error::{Error, Result};
use crate::resources::handles::{CityObjectHandle, GeometryHandle, cast_handle_slice};
use crate::resources::id::ResourceId32;
use crate::resources::storage::{BorrowedStringStorage, OwnedStringStorage, StringStorage};
use std::fmt::{Display, Formatter};
use std::str::FromStr;

pub use crate::cityjson::core::cityobject::CityObjectIdentifier;

/// The ordered collection of city objects in a [`CityModel`](super::citymodel::CityModel).
///
/// Wraps a resource pool keyed by [`CityObjectHandle`].
#[derive(Debug, Clone)]
pub struct CityObjects<SS: StringStorage> {
    inner: CityObjectsCore<SS, ResourceId32, CityObject<SS>>,
}

impl<SS: StringStorage> CityObjects<SS> {
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: CityObjectsCore::new(),
        }
    }

    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inner: CityObjectsCore::with_capacity(capacity),
        }
    }

    /// Reserve capacity for additional city objects.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error::ResourcePoolFull`] when reserving the requested capacity
    /// would exceed the representable pool size.
    pub fn reserve(&mut self, additional: usize) -> Result<()> {
        self.inner.reserve(additional)
    }

    /// Add a city object and return its handle.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error::ResourcePoolFull`] when the city-object pool cannot store
    /// additional entries for `ResourceId32`.
    pub fn add(&mut self, city_object: CityObject<SS>) -> Result<CityObjectHandle> {
        self.inner.add(city_object).map(CityObjectHandle::from_raw)
    }

    #[must_use]
    pub fn get(&self, id: CityObjectHandle) -> Option<&CityObject<SS>> {
        self.inner.get(id.to_raw())
    }

    pub fn get_mut(&mut self, id: CityObjectHandle) -> Option<&mut CityObject<SS>> {
        self.inner.get_mut(id.to_raw())
    }

    pub fn remove(&mut self, id: CityObjectHandle) -> Option<CityObject<SS>> {
        self.inner.remove(id.to_raw())
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn iter<'a>(&'a self) -> impl Iterator<Item = (CityObjectHandle, &'a CityObject<SS>)>
    where
        CityObject<SS>: 'a,
    {
        self.inner
            .iter()
            .map(|(id, value)| (CityObjectHandle::from_raw(id), value))
    }

    pub fn iter_mut<'a>(
        &'a mut self,
    ) -> impl Iterator<Item = (CityObjectHandle, &'a mut CityObject<SS>)>
    where
        CityObject<SS>: 'a,
    {
        self.inner
            .iter_mut()
            .map(|(id, value)| (CityObjectHandle::from_raw(id), value))
    }

    #[must_use]
    pub fn first(&self) -> Option<(CityObjectHandle, &CityObject<SS>)> {
        self.inner
            .first()
            .map(|(id, value)| (CityObjectHandle::from_raw(id), value))
    }

    #[must_use]
    pub fn last(&self) -> Option<(CityObjectHandle, &CityObject<SS>)> {
        self.inner
            .last()
            .map(|(id, value)| (CityObjectHandle::from_raw(id), value))
    }

    pub fn ids(&self) -> impl Iterator<Item = CityObjectHandle> + '_ {
        self.inner.ids().map(CityObjectHandle::from_raw)
    }

    /// Add many city objects and return their handles.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error::ResourcePoolFull`] when inserting one of the objects
    /// exceeds city-object pool capacity.
    pub fn add_many<I: IntoIterator<Item = CityObject<SS>>>(
        &mut self,
        objects: I,
    ) -> Result<Vec<CityObjectHandle>> {
        self.inner
            .add_many(objects)
            .map(|ids| ids.into_iter().map(CityObjectHandle::from_raw).collect())
    }

    pub fn clear(&mut self) {
        self.inner.clear();
    }

    pub fn filter<F>(
        &self,
        predicate: F,
    ) -> impl Iterator<Item = (CityObjectHandle, &CityObject<SS>)>
    where
        F: Fn(&CityObject<SS>) -> bool,
    {
        self.inner
            .filter(predicate)
            .map(|(id, value)| (CityObjectHandle::from_raw(id), value))
    }
}

impl<SS: StringStorage> Default for CityObjects<SS> {
    fn default() -> Self {
        Self::new()
    }
}

pub type OwnedCityObjects = CityObjects<OwnedStringStorage>;
pub type BorrowedCityObjects<'a> = CityObjects<BorrowedStringStorage<'a>>;

/// A single city object.
///
/// Corresponds to one entry in the `CityObjects` JSON map. Holds the object type, geometry
/// handle references, attributes, optional bounding box, and parent/child handle lists.
///
/// Geometry handles must reference geometries already stored in the [`CityModel`](super::citymodel::CityModel);
/// use [`CityObject::add_geometry`] to attach them after insertion.
#[derive(Debug, Default, Clone)]
pub struct CityObject<SS: StringStorage> {
    inner: CityObjectCore<SS, ResourceId32, CityObjectType<SS>>,
}

impl<SS: StringStorage> CityObject<SS> {
    pub fn new(id: CityObjectIdentifier<SS>, type_cityobject: CityObjectType<SS>) -> Self {
        Self {
            inner: CityObjectCore::new(id.into_inner(), type_cityobject),
        }
    }

    pub fn id(&self) -> &str
    where
        SS::String: AsRef<str>,
    {
        self.inner.id().as_ref()
    }

    pub fn type_cityobject(&self) -> &CityObjectType<SS> {
        self.inner.type_cityobject()
    }

    pub fn geometry(&self) -> Option<&[GeometryHandle]> {
        self.inner
            .geometry()
            .map(|items| cast_handle_slice::<GeometryHandle>(items.as_slice()))
    }

    pub fn add_geometry(&mut self, geometry_ref: GeometryHandle) {
        self.inner.geometry_mut().push(geometry_ref.to_raw());
    }

    pub fn clear_geometry(&mut self) {
        self.inner.geometry_mut().clear();
    }

    pub fn attributes(&self) -> Option<&crate::v2_0::attributes::Attributes<SS>> {
        self.inner.attributes()
    }

    pub fn attributes_mut(&mut self) -> &mut crate::v2_0::attributes::Attributes<SS> {
        self.inner.attributes_mut()
    }

    pub fn geographical_extent(&self) -> Option<&crate::v2_0::metadata::BBox> {
        self.inner.geographical_extent()
    }

    pub fn set_geographical_extent(&mut self, bbox: Option<crate::v2_0::metadata::BBox>) {
        self.inner.set_geographical_extent(bbox);
    }

    pub fn children(&self) -> Option<&[CityObjectHandle]> {
        self.inner
            .children()
            .map(|items| cast_handle_slice::<CityObjectHandle>(items.as_slice()))
    }

    pub fn add_child(&mut self, child: CityObjectHandle) {
        self.inner.children_mut().push(child.to_raw());
    }

    pub fn clear_children(&mut self) {
        self.inner.children_mut().clear();
    }

    pub fn parents(&self) -> Option<&[CityObjectHandle]> {
        self.inner
            .parents()
            .map(|items| cast_handle_slice::<CityObjectHandle>(items.as_slice()))
    }

    pub fn add_parent(&mut self, parent: CityObjectHandle) {
        self.inner.parents_mut().push(parent.to_raw());
    }

    pub fn clear_parents(&mut self) {
        self.inner.parents_mut().clear();
    }

    pub fn extra(&self) -> Option<&crate::v2_0::attributes::Attributes<SS>> {
        self.inner.extra()
    }

    pub fn extra_mut(&mut self) -> &mut crate::v2_0::attributes::Attributes<SS> {
        self.inner.extra_mut()
    }
}

impl<SS: StringStorage> Display for CityObject<SS> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:#?}")
    }
}

/// The type of a city object, as defined in the `CityJSON` specification.
///
/// 1st-level types (e.g. `Building`, `Road`) can exist independently.
/// 2nd-level types (e.g. `BuildingPart`, `BuildingRoom`) require a `parents` reference.
///
/// Extension types are represented by `Extension(name)` where `name` starts with `"+"`.
/// `FromStr` accepts all standard names plus `"+"` prefixed extension names.
#[derive(Debug, Default, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
#[non_exhaustive]
pub enum CityObjectType<SS: StringStorage> {
    Bridge,
    BridgePart,
    BridgeInstallation,
    BridgeConstructiveElement,
    BridgeRoom,
    BridgeFurniture,
    Building,
    BuildingPart,
    BuildingInstallation,
    BuildingConstructiveElement,
    BuildingFurniture,
    BuildingStorey,
    BuildingRoom,
    BuildingUnit,
    CityFurniture,
    CityObjectGroup,
    #[default]
    Default,
    GenericCityObject,
    LandUse,
    OtherConstruction,
    PlantCover,
    SolitaryVegetationObject,
    TINRelief,
    WaterBody,
    Road,
    Railway,
    Waterway,
    TransportSquare,
    Tunnel,
    TunnelPart,
    TunnelInstallation,
    TunnelConstructiveElement,
    TunnelHollowSpace,
    TunnelFurniture,
    Extension(SS::String),
}

impl<SS: StringStorage> Display for CityObjectType<SS> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if let CityObjectType::Extension(ext) = self {
            write!(f, "{ext}")
        } else {
            write!(f, "{self:#?}")
        }
    }
}

impl FromStr for CityObjectType<OwnedStringStorage> {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s {
            "Bridge" => Ok(CityObjectType::Bridge),
            "BridgePart" => Ok(CityObjectType::BridgePart),
            "BridgeInstallation" => Ok(CityObjectType::BridgeInstallation),
            "BridgeConstructiveElement" => Ok(CityObjectType::BridgeConstructiveElement),
            "BridgeRoom" => Ok(CityObjectType::BridgeRoom),
            "BridgeFurniture" => Ok(CityObjectType::BridgeFurniture),
            "Building" => Ok(CityObjectType::Building),
            "BuildingPart" => Ok(CityObjectType::BuildingPart),
            "BuildingInstallation" => Ok(CityObjectType::BuildingInstallation),
            "BuildingConstructiveElement" => Ok(CityObjectType::BuildingConstructiveElement),
            "BuildingFurniture" => Ok(CityObjectType::BuildingFurniture),
            "BuildingStorey" => Ok(CityObjectType::BuildingStorey),
            "BuildingRoom" => Ok(CityObjectType::BuildingRoom),
            "BuildingUnit" => Ok(CityObjectType::BuildingUnit),
            "CityFurniture" => Ok(CityObjectType::CityFurniture),
            "CityObjectGroup" => Ok(CityObjectType::CityObjectGroup),
            "Default" => Ok(CityObjectType::Default),
            "GenericCityObject" => Ok(CityObjectType::GenericCityObject),
            "LandUse" => Ok(CityObjectType::LandUse),
            "OtherConstruction" => Ok(CityObjectType::OtherConstruction),
            "PlantCover" => Ok(CityObjectType::PlantCover),
            "SolitaryVegetationObject" => Ok(CityObjectType::SolitaryVegetationObject),
            "TINRelief" => Ok(CityObjectType::TINRelief),
            "WaterBody" => Ok(CityObjectType::WaterBody),
            "Road" => Ok(CityObjectType::Road),
            "Railway" => Ok(CityObjectType::Railway),
            "Waterway" => Ok(CityObjectType::Waterway),
            "TransportSquare" => Ok(CityObjectType::TransportSquare),
            "Tunnel" => Ok(CityObjectType::Tunnel),
            "TunnelPart" => Ok(CityObjectType::TunnelPart),
            "TunnelInstallation" => Ok(CityObjectType::TunnelInstallation),
            "TunnelConstructiveElement" => Ok(CityObjectType::TunnelConstructiveElement),
            "TunnelHollowSpace" => Ok(CityObjectType::TunnelHollowSpace),
            "TunnelFurniture" => Ok(CityObjectType::TunnelFurniture),
            _ => {
                if s.chars().next().is_some_and(|first_char| first_char == '+') {
                    Ok(CityObjectType::Extension(s.to_string()))
                } else {
                    Err(Error::InvalidCityObjectType(s.to_string()))
                }
            }
        }
    }
}