irox_carto/geo/
mod.rs

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
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

//!
//! Geodesy types and math, Ellipses, Ellipsoids, Elliptical Shapes

extern crate alloc;
use crate::error::ConvertError;
use crate::geo::ellipsoid::Ellipsoid;
use crate::geo::standards::wgs84::{WGS84_EPSG_SHAPE, WGS84_SHAPE};
use crate::geo::standards::StandardShapes;
use alloc::string::String;
use ellipse::Ellipse;
use irox_tools::{cfg_feature_std, format};

pub mod ellipse;
pub mod ellipsoid;
pub mod standards;

cfg_feature_std! {
    mod meridians;
    pub use meridians::*;
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum EllipticalShape {
    EpsgDatum(u32),
    Ellipse(Ellipse),
}

impl Default for EllipticalShape {
    fn default() -> Self {
        WGS84_SHAPE
    }
}

impl EllipticalShape {
    #[must_use]
    pub fn name(&self) -> String {
        match self {
            EllipticalShape::EpsgDatum(d) => {
                format!("EPSG({d})")
            }
            EllipticalShape::Ellipse(e) => String::from(e.name()),
        }
    }

    #[must_use]
    pub fn is_wgs84(&self) -> bool {
        *self == WGS84_SHAPE || *self == WGS84_EPSG_SHAPE
    }

    pub fn as_ellipse(&self) -> Result<Ellipse, ConvertError> {
        Ellipse::try_from(self)
    }

    pub fn as_ellipsoid(&self) -> Result<Ellipsoid, ConvertError> {
        self.as_ellipse().map(Into::into)
    }
}

impl TryFrom<&EllipticalShape> for Ellipse {
    type Error = ConvertError;

    fn try_from(value: &EllipticalShape) -> Result<Self, Self::Error> {
        match value {
            EllipticalShape::EpsgDatum(d) => StandardShapes::lookup_epsg(*d)
                .ok_or_else(|| ConvertError::MissingProjection(format!("Unknown EPSG code: {d}")))
                .map(|v| v.as_ellipse()),
            EllipticalShape::Ellipse(e) => Ok(*e),
        }
    }
}