Skip to main content

ifc_georef/crs/
geographic.rs

1//! Borrowed IFC4X3 `IfcGeographicCRS` interpretation.
2//!
3//! IFC4X3 ADD2 declares
4//! `IfcGeographicCRS SUBTYPE OF (IfcCoordinateReferenceSystem)` with
5//! `PrimeMeridian : OPTIONAL IfcIdentifier`, `AngleUnit : OPTIONAL
6//! IfcNamedUnit` (`WHERE AngleUnitIsPlaneAngle`) and `HeightUnit : OPTIONAL
7//! IfcNamedUnit` (`WHERE HeightUnitIsLength`). IFC4 does not declare the
8//! entity; the schema-pinned readers refuse it there before this runs.
9//!
10//! The inherited `Name` is `OPTIONAL` under `NameOrWKT`, exactly as for
11//! `IfcProjectedCRS` in IFC4X3, so the same rule is applied. An absent
12//! unit stays `None`: nothing here assumes degrees or metres.
13
14use 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/// A geographic coordinate reference system read from IFC4X3
25/// `IfcGeographicCRS`.
26#[derive(Debug, Clone, PartialEq)]
27#[non_exhaustive]
28pub struct GeographicCrs {
29    /// The `IfcGeographicCRS` entity this was read from.
30    pub entity: EntityId,
31    /// CRS identifier, conventionally an EPSG code such as `EPSG:4326`.
32    /// `None` only when exactly one `IfcWellKnownText` defines the CRS.
33    pub name: Option<String>,
34    /// Optional human-readable description.
35    pub description: Option<String>,
36    /// Optional geodetic datum, such as `WGS84`.
37    pub geodetic_datum: Option<String>,
38    /// Optional prime meridian identifier, such as `Greenwich`.
39    pub prime_meridian: Option<String>,
40    /// The declared unit of latitude and longitude. `None` when the file
41    /// does not state one; no unit is assumed.
42    pub angle_unit: Option<AngleUnit>,
43    /// The declared unit of ellipsoidal height. `None` when the file does
44    /// not state one; no unit is assumed.
45    pub height_unit: Option<LengthUnit>,
46    /// The OGC WKT literal of the one `IfcWellKnownText` defining this CRS.
47    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}