ifc_georef/crs/
geographic.rs1use ifc_model::value::Value;
15use ifc_model::{EntityId, Model};
16
17use crate::crs::projected::name_or_wkt;
18use crate::crs::unit::{resolve_angle_unit, resolve_length_unit, AngleUnit, LengthUnit};
19use crate::crs::Damage;
20use crate::error::{GeorefError, GeorefResult};
21use crate::slot::geographic_crs as slot;
22use crate::slot::projected_crs as base;
23
24#[derive(Debug, Clone, PartialEq)]
27#[non_exhaustive]
28pub struct GeographicCrs {
29 pub entity: EntityId,
31 pub name: Option<String>,
34 pub description: Option<String>,
36 pub geodetic_datum: Option<String>,
38 pub prime_meridian: Option<String>,
40 pub angle_unit: Option<AngleUnit>,
43 pub height_unit: Option<LengthUnit>,
46 pub well_known_text: Option<String>,
48}
49
50pub(crate) fn geographic_crs(
51 model: &Model,
52 id: EntityId,
53 damage: &Damage,
54) -> GeorefResult<GeographicCrs> {
55 damage.check(id)?;
56 let entity = model.get(id).ok_or(GeorefError::MissingEntity {
57 referrer: id,
58 missing: id,
59 })?;
60 if !entity.is_type("IFCGEOGRAPHICCRS") {
61 return Err(GeorefError::WrongType {
62 entity: id,
63 expected: "IFCGEOGRAPHICCRS",
64 actual: entity.type_name.to_string(),
65 });
66 }
67 let (name, well_known_text) = name_or_wkt(model, id, entity, damage)?;
68 let unit_ref = |index: usize, name: &'static str| match entity.attribute(index) {
69 None | Some(Value::Null) => Ok(None),
70 Some(value) => value
71 .as_ref_id()
72 .map(Some)
73 .ok_or(GeorefError::InvalidAttribute {
74 entity: id,
75 index,
76 name,
77 }),
78 };
79 let angle_unit = unit_ref(slot::ANGLE_UNIT, "AngleUnit")?
80 .map(|unit| resolve_angle_unit(model, unit, damage))
81 .transpose()?;
82 let height_unit = unit_ref(slot::HEIGHT_UNIT, "HeightUnit")?
83 .map(|unit| resolve_length_unit(model, unit, damage))
84 .transpose()?;
85 Ok(GeographicCrs {
86 entity: id,
87 name,
88 description: entity.text(base::DESCRIPTION).map(str::to_owned),
89 geodetic_datum: entity.text(base::GEODETIC_DATUM).map(str::to_owned),
90 prime_meridian: entity.text(slot::PRIME_MERIDIAN).map(str::to_owned),
91 angle_unit,
92 height_unit,
93 well_known_text,
94 })
95}