Skip to main content

ifc_lite_processing/
georeferencing.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Georeferencing extraction for the HTTP server response.
6//!
7//! The browser parser (`@ifc-lite/parse`) exposes `IfcMapConversion` /
8//! `IfcProjectedCRS` georeferencing via `extractGeoreferencing`. The server
9//! previously surfaced only a coarse `is_geo_referenced` boolean, so consumers
10//! couldn't recover the real-world CRS, false eastings/northings, or grid-north
11//! rotation. This module reuses the shared `ifc_lite_core::GeoRefExtractor`
12//! (the same extraction the desktop/native paths use) and maps it into a
13//! serializable, server-friendly shape carried inline on every geometry
14//! endpoint's `ModelMetadata` (issue #900 parity follow-up).
15
16use std::sync::Arc;
17
18use ifc_lite_core::{EntityDecoder, EntityIndex, EntityScanner, GeoRefExtractor, IfcType};
19use serde::{Deserialize, Serialize};
20
21/// Georeferencing metadata (`IfcMapConversion` + `IfcProjectedCRS`).
22///
23/// Mirrors `ifc_lite_core::GeoReference` with two derived conveniences
24/// (`rotation_degrees`, `transform_matrix`) so consumers don't have to
25/// recompute the rotation or the local→map matrix.
26#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
27pub struct Georeferencing {
28    /// Projected CRS name from `IfcProjectedCRS.Name` (e.g. `"EPSG:32632"`).
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub crs_name: Option<String>,
31    /// Geodetic datum (e.g. `"WGS84"`).
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub geodetic_datum: Option<String>,
34    /// Vertical datum (e.g. `"NAVD88"`).
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub vertical_datum: Option<String>,
37    /// Map projection (e.g. `"UTM"`).
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub map_projection: Option<String>,
40    /// False easting — X offset to the map CRS, in the project's length unit.
41    pub eastings: f64,
42    /// False northing — Y offset to the map CRS, in the project's length unit.
43    pub northings: f64,
44    /// Orthogonal height — Z offset to the map CRS.
45    pub orthogonal_height: f64,
46    /// X-axis abscissa: cosine of the rotation to grid north.
47    pub x_axis_abscissa: f64,
48    /// X-axis ordinate: sine of the rotation to grid north.
49    pub x_axis_ordinate: f64,
50    /// Scale factor applied during the local→map transform (default `1.0`).
51    pub scale: f64,
52    /// Rotation to grid north in degrees, derived from the X-axis direction.
53    pub rotation_degrees: f64,
54    /// Local→map transform as a column-major 4×4 matrix (16 values).
55    pub transform_matrix: [f64; 16],
56    /// CRS description from `IfcProjectedCRS.Description`.
57    #[serde(skip_serializing_if = "Option::is_none", default)]
58    pub crs_description: Option<String>,
59    /// Map zone (e.g. `"32N"`) from `IfcProjectedCRS.MapZone`.
60    #[serde(skip_serializing_if = "Option::is_none", default)]
61    pub map_zone: Option<String>,
62    /// Map unit name resolved from `IfcProjectedCRS.MapUnit` (e.g. `"METRE"`,
63    /// `"MILLIMETRE"`); absent when no MapUnit is authored.
64    #[serde(skip_serializing_if = "Option::is_none", default)]
65    pub map_unit: Option<String>,
66    /// Scale factor converting MapConversion values to metres (0.001 for
67    /// millimetres); absent when no MapUnit is authored.
68    #[serde(skip_serializing_if = "Option::is_none", default)]
69    pub map_unit_scale: Option<f64>,
70    /// Provenance: `"mapConversion"`, `"ePSetMapConversion"`, or
71    /// `"siteLocation"` — same labels as the TS parser's
72    /// `GeoreferenceInfo.source`.
73    #[serde(skip_serializing_if = "Option::is_none", default)]
74    pub source: Option<String>,
75}
76
77impl Georeferencing {
78    fn from_core(geo: &ifc_lite_core::GeoReference) -> Self {
79        Self {
80            crs_name: geo.crs_name.clone(),
81            geodetic_datum: geo.geodetic_datum.clone(),
82            vertical_datum: geo.vertical_datum.clone(),
83            map_projection: geo.map_projection.clone(),
84            eastings: geo.eastings,
85            northings: geo.northings,
86            orthogonal_height: geo.orthogonal_height,
87            x_axis_abscissa: geo.x_axis_abscissa,
88            x_axis_ordinate: geo.x_axis_ordinate,
89            scale: geo.scale,
90            rotation_degrees: geo.rotation().to_degrees(),
91            transform_matrix: geo.to_matrix(),
92            crs_description: geo.crs_description.clone(),
93            map_zone: geo.map_zone.clone(),
94            map_unit: geo.map_unit.clone(),
95            map_unit_scale: geo.map_unit_scale,
96            source: Some(geo.source.label().to_string()),
97        }
98    }
99}
100
101/// Extract georeferencing from an IFC file, returning `None` when the model
102/// carries no `IfcMapConversion` / `ePSet_MapConversion` data.
103///
104/// Only the entity types the extractor needs (`IfcMapConversion`,
105/// `IfcProjectedCRS`, and `IfcPropertySet` for the IFC2x3 `ePSet_MapConversion`
106/// fallback) are collected from the scan — their `IfcType` is known from the
107/// entity name, so no decoding happens while building the candidate list.
108pub fn extract_georeferencing<T>(content: &T) -> Option<Georeferencing>
109where
110    T: AsRef<[u8]> + ?Sized,
111{
112    let content = content.as_ref();
113    // Parallel on native (byte-identical to `build_entity_index`), serial on wasm.
114    let entity_index = Arc::new(crate::build_entity_index_parallel(content));
115    extract_georeferencing_with_index(content, &entity_index)
116}
117
118/// [`extract_georeferencing`], reusing an index the caller already holds.
119///
120/// The extractor resolves its references by id, so this pass genuinely needs an
121/// index; what it does not need is a second one. A caller that has already
122/// scanned the file (the geometry pipeline holds one for its whole run) was
123/// paying a full extra scan to build a map it had in hand.
124///
125/// `entity_index` must have been built from this same `content`. Given that, the
126/// result is identical to the wrapper's.
127pub fn extract_georeferencing_with_index(
128    content: &[u8],
129    entity_index: &Arc<EntityIndex>,
130) -> Option<Georeferencing> {
131    let mut entity_types = Vec::new();
132    let mut scanner = EntityScanner::new(content);
133    while let Some((id, type_name, _, _)) = scanner.next_entity() {
134        if let Some(ifc_type) = georeferencing_candidate_type(type_name) {
135            entity_types.push((id, ifc_type));
136        }
137    }
138    extract_georeferencing_from_candidates(
139        &mut EntityDecoder::with_arc_index(content, entity_index.clone()), &entity_types)
140}
141
142/// Candidate classification shared by standalone extraction and the native
143/// geometry scan. Preserves file order, including all sites.
144///
145/// `type_name` is the STEP keyword exactly as the scanner read it, and STEP
146/// keyword case is not significant (ISO 10303-21), so the comparison is
147/// case-insensitive. A case-sensitive match silently classified every entity in
148/// a lowercase- or CamelCase-keyword file as a non-candidate, and the model then
149/// reported no georeferencing at all despite carrying complete data (#4497).
150/// `eq_ignore_ascii_case` rather than an uppercase copy: this runs once per
151/// entity in the scan loop, and the comparison is against fixed literals, so
152/// there is nothing an allocated canonical form would be reused for — the same
153/// shape `processor::quick_metadata::is_quick_spatial_type_ci` uses.
154pub(crate) fn georeferencing_candidate_type(type_name: &str) -> Option<IfcType> {
155    // Scaled's first eight attributes have the base conversion layout.
156    const MAP_CONVERSION: [&str; 2] = ["IFCMAPCONVERSION", "IFCMAPCONVERSIONSCALED"];
157    if MAP_CONVERSION
158        .iter()
159        .any(|candidate| type_name.eq_ignore_ascii_case(candidate))
160    {
161        return Some(IfcType::IfcMapConversion);
162    }
163    if type_name.eq_ignore_ascii_case("IFCPROJECTEDCRS") {
164        return Some(IfcType::IfcProjectedCRS);
165    }
166    if type_name.eq_ignore_ascii_case("IFCPROPERTYSET") {
167        return Some(IfcType::IfcPropertySet);
168    }
169    if type_name.eq_ignore_ascii_case("IFCSITE") {
170        return Some(IfcType::IfcSite);
171    }
172    None
173}
174
175/// Reuse candidates from the geometry scan, avoiding the remaining whole-file
176/// scan after index reuse. A fresh decoder preserves the standalone extractor's
177/// lookup semantics, including duplicate ids, rather than inheriting scan caches.
178pub(crate) fn extract_georeferencing_from_candidates(
179    decoder: &mut EntityDecoder<'_>,
180    entity_types: &[(u32, IfcType)],
181) -> Option<Georeferencing> {
182    if entity_types.is_empty() {
183        return None;
184    }
185
186    match GeoRefExtractor::extract(decoder, entity_types) {
187        Ok(Some(geo)) => Some(Georeferencing::from_core(&geo)),
188        Ok(None) => None,
189        Err(e) => {
190            tracing::debug!(error = %e, "Georeferencing extraction failed");
191            None
192        }
193    }
194}
195
196#[cfg(test)]
197#[path = "georeferencing_tests.rs"]
198mod tests;