1use crate::record::{Field, Record, ToRecord};
22use crate::{BindingError, IfcModel};
23
24#[derive(Debug, Clone, PartialEq)]
26pub struct MapConversion {
27 pub operation: u64,
30 pub kind: String,
32 pub source: u64,
35 pub source_kind: String,
37 pub target_crs: ProjectedCrs,
39 pub eastings: f64,
41 pub northings: f64,
43 pub orthogonal_height: f64,
46 pub x_axis: (f64, f64),
48 pub scale: f64,
50 pub factors: Option<(f64, f64, f64)>,
52 pub project_unit: LengthUnit,
54 pub map_unit: LengthUnit,
56 pub map_unit_declared: bool,
58 pub linear: [[f64; 3]; 3],
61 pub translation: [f64; 3],
63}
64
65#[derive(Debug, Clone, PartialEq)]
67pub struct ProjectedCrs {
68 pub id: u64,
70 pub name: Option<String>,
72 pub description: Option<String>,
74 pub geodetic_datum: Option<String>,
76 pub vertical_datum: Option<String>,
78 pub map_projection: Option<String>,
80 pub map_zone: Option<String>,
82 pub well_known_text: Option<String>,
84}
85
86#[derive(Debug, Clone, PartialEq)]
88pub struct LengthUnit {
89 pub name: String,
91 pub metres_per_unit: f64,
93}
94
95impl IfcModel {
96 pub fn georeferencing(&self) -> Result<Vec<MapConversion>, BindingError> {
106 #[cfg(feature = "georef")]
107 {
108 read::georeferencing(self)
109 }
110 #[cfg(not(feature = "georef"))]
111 {
112 Err(BindingError::FeatureDisabled("georef"))
113 }
114 }
115}
116
117#[cfg(feature = "georef")]
118mod read {
119 use ifc::georef::{GeorefError, OperationKind, OperationSource};
120 use ifc::GeoreferencingError;
121
122 use super::{LengthUnit, MapConversion, ProjectedCrs};
123 use crate::{BindingError, IfcModel};
124
125 pub(super) fn georeferencing(model: &IfcModel) -> Result<Vec<MapConversion>, BindingError> {
126 let operations = ifc::georeferencing(&model.inner).map_err(error)?;
127 Ok(operations
128 .into_iter()
129 .map(|map| {
130 let (kind, factors) = match map.kind {
131 OperationKind::MapConversion => ("map-conversion", None),
132 OperationKind::MapConversionScaled { factors } => {
133 ("map-conversion-scaled", Some(factors))
134 }
135 OperationKind::RigidOperation { .. } => ("rigid-operation", None),
136 _ => ("other", None),
137 };
138 let (source, source_kind) = match map.source {
139 OperationSource::Context(id) => (id.0, "context"),
140 OperationSource::CoordinateReferenceSystem(id) => (id.0, "crs"),
141 OperationSource::PropertySet(id) => (id.0, "property-set"),
142 _ => (map.source_crs.0, "other"),
143 };
144 let unit = |unit: &ifc::georef::LengthUnit| LengthUnit {
145 name: unit.name.clone(),
146 metres_per_unit: unit.metres_per_unit,
147 };
148 let crs = &map.target_crs;
149 MapConversion {
150 operation: map.operation.0,
151 kind: kind.to_owned(),
152 source,
153 source_kind: source_kind.to_owned(),
154 target_crs: ProjectedCrs {
155 id: crs.entity.0,
156 name: crs.name.clone(),
157 description: crs.description.clone(),
158 geodetic_datum: crs.geodetic_datum.clone(),
159 vertical_datum: crs.vertical_datum.clone(),
160 map_projection: crs.map_projection.clone(),
161 map_zone: crs.map_zone.clone(),
162 well_known_text: crs.well_known_text.clone(),
163 },
164 eastings: map.eastings,
165 northings: map.northings,
166 orthogonal_height: map.orthogonal_height,
167 x_axis: map.x_axis_direction,
168 scale: map.declared_scale,
169 factors,
170 project_unit: unit(&map.project_unit),
171 map_unit: unit(&map.map_unit),
172 map_unit_declared: map.declared_map_unit().is_some(),
173 linear: map.linear_part(),
174 translation: map.translation(),
175 }
176 })
177 .collect())
178 }
179
180 fn error(error: GeoreferencingError) -> BindingError {
181 let detail = error.to_string();
182 match error {
183 GeoreferencingError::Georef(error) => match error {
184 GeorefError::UnsupportedSchema { token } => BindingError::UnsupportedSchema(token),
185 GeorefError::MissingSchema => BindingError::UnsupportedSchema(String::new()),
186 GeorefError::AmbiguousSchema { .. } => BindingError::UnsupportedSchema(detail),
187 GeorefError::MissingEntity { .. } => BindingError::MissingReference(detail),
188 GeorefError::UnsupportedOperation { .. }
189 | GeorefError::CoordinateMeasureMismatch { .. } => {
190 BindingError::Unsupported(detail)
191 }
192 GeorefError::UnitCycle { .. } => BindingError::BudgetExceeded(detail),
193 _ => BindingError::InvalidModel(detail),
194 },
195 GeoreferencingError::ProjectLengthUnit(error) => {
196 crate::properties::read::unit_error(error)
197 }
198 GeoreferencingError::OffsetLengthUnit { .. } => BindingError::Unsupported(detail),
199 _ => BindingError::InvalidModel(detail),
200 }
201 }
202}
203
204impl ToRecord for MapConversion {
205 fn to_record(&self) -> Record {
206 let triple =
207 |[x, y, z]: [f64; 3]| Field::List(vec![Field::Real(x), Field::Real(y), Field::Real(z)]);
208 Record::new(
209 "MapConversion",
210 vec![
211 ("operation", Field::Id(self.operation)),
212 ("kind", Field::Text(self.kind.clone())),
213 ("source", Field::Id(self.source)),
214 ("source_kind", Field::Text(self.source_kind.clone())),
215 ("target_crs", Field::Record(self.target_crs.to_record())),
216 ("eastings", Field::Real(self.eastings)),
217 ("northings", Field::Real(self.northings)),
218 ("orthogonal_height", Field::Real(self.orthogonal_height)),
219 (
220 "x_axis",
221 Field::List(vec![Field::Real(self.x_axis.0), Field::Real(self.x_axis.1)]),
222 ),
223 ("scale", Field::Real(self.scale)),
224 (
225 "factors",
226 Field::optional(self.factors, |(x, y, z)| triple([x, y, z])),
227 ),
228 ("project_unit", Field::Record(self.project_unit.to_record())),
229 ("map_unit", Field::Record(self.map_unit.to_record())),
230 ("map_unit_declared", Field::Bool(self.map_unit_declared)),
231 (
232 "linear",
233 Field::List(self.linear.iter().map(|column| triple(*column)).collect()),
234 ),
235 ("translation", triple(self.translation)),
236 ],
237 )
238 }
239}
240
241impl ToRecord for ProjectedCrs {
242 fn to_record(&self) -> Record {
243 Record::new(
244 "ProjectedCrs",
245 vec![
246 ("id", Field::Id(self.id)),
247 ("name", Field::text(self.name.clone())),
248 ("description", Field::text(self.description.clone())),
249 ("geodetic_datum", Field::text(self.geodetic_datum.clone())),
250 ("vertical_datum", Field::text(self.vertical_datum.clone())),
251 ("map_projection", Field::text(self.map_projection.clone())),
252 ("map_zone", Field::text(self.map_zone.clone())),
253 ("well_known_text", Field::text(self.well_known_text.clone())),
254 ],
255 )
256 }
257}
258
259impl ToRecord for LengthUnit {
260 fn to_record(&self) -> Record {
261 Record::new(
262 "LengthUnit",
263 vec![
264 ("name", Field::Text(self.name.clone())),
265 ("metres_per_unit", Field::Real(self.metres_per_unit)),
266 ],
267 )
268 }
269}