irox_carto/
coordinate.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

//!
//! Latitude, Longitude, Elevation, and associated Coordinate types, Elliptical and Cartesian

extern crate alloc;
use crate::altitude::Altitude;
use crate::error::ConvertError;
use crate::geo::{standards, EllipticalShape};
use crate::position_type::ECEFPosition;
use core::fmt::{Display, Formatter};
use core::ops::Deref;
use irox_time::datetime::UTCDateTime;
use irox_time::epoch::UnixTimestamp;
use irox_tools::cfg_feature_std;
use irox_units::shapes::circular::CircularDimension;
use irox_units::shapes::Ellipse;
use irox_units::units::angle::{Angle, AngleUnits};
use irox_units::units::compass::Azimuth;
use irox_units::units::length::Length;

use alloc::string::{String, ToString};
use irox_tools::format;

cfg_feature_std! {
    use crate::ecef::{ECEF, WGS84ECEF};
}

/// A generic coordinate type that does not distinguish between a [RelativeCoordinateType] or an
/// [AbsoluteCoordinateType].
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum CoordinateType {
    Elliptical(EllipticalCoordinate),
    Cartesian(CartesianCoordinate),
    Horizontal(HorizontalCoordinate),
}

/// A "Absolute Coordinate" is a coordinate that has no variable/dependent element.  It
/// does not require a context to be meaningful.  Contrast with [RelativeCoordinateType]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum AbsoluteCoordinateType {
    Elliptical(EllipticalCoordinate),
    ECEF(ECEFPosition),
}
cfg_feature_std! {
    impl AbsoluteCoordinateType {
        #[must_use]
        pub fn as_elliptical(&self) -> EllipticalCoordinate {
            match self {
                AbsoluteCoordinateType::Elliptical(e) => *e,
                AbsoluteCoordinateType::ECEF(e) => WGS84ECEF::ecef_to_coord(e).0,
            }
        }
        pub fn as_ecef(&self) -> Result<ECEFPosition, ConvertError> {
            match self {
                AbsoluteCoordinateType::Elliptical(e) => ECEF::coord_to_ecef(e),
                AbsoluteCoordinateType::ECEF(e) => Ok(*e),
            }
        }
    }
}

/// A "Relative Coordinate" is a coordinate that has a variable or dependent element.  It's
/// absolute coordinate is dependent on another absolute coordinate as a reference point.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum RelativeCoordinateType {
    Cartesian(CartesianCoordinate),
    Horizontal(HorizontalCoordinate),
}

/// Forcing type for Latitude
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Latitude(pub Angle);
impl Deref for Latitude {
    type Target = Angle;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Forcing type for Longitude
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Longitude(pub Angle);
impl Deref for Longitude {
    type Target = Angle;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Forcing type for Elevation, the angle above the local horizontal
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Elevation(pub Angle);

/// Represents a Latitude, Longitude, and Altitude on a Elliptical Shape
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct EllipticalCoordinate {
    latitude: Latitude,
    longitude: Longitude,
    reference_frame: EllipticalShape,
    altitude: Option<Altitude>,
    altitude_uncertainty: Option<Length>,
    position_uncertainty: Option<PositionUncertainty>,
    timestamp: Option<UTCDateTime>,
}

impl Display for EllipticalCoordinate {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let alt = match self.altitude {
            Some(alt) => {
                format!(
                    " Alt: {}m {}",
                    alt.value().as_meters().value(),
                    alt.reference_frame().short_name()
                )
            }
            None => String::new(),
        };
        let alt_err = match self.altitude_uncertainty {
            Some(err) => format!("+/- {}m vert", err.as_meters().value()),
            None => String::new(),
        };
        let pos_err = match self.position_uncertainty {
            Some(err) => format!(" {err} horiz"),
            None => String::new(),
        };
        let asof = match self.timestamp {
            Some(ts) => format!(" as/of: {ts:?}"),
            None => String::new(),
        };
        write!(
            f,
            "Lat: {:0.5}\u{00B0} Lon: {:0.5}\u{00B0} {}{alt}{alt_err}{pos_err}{asof}",
            self.latitude.0.as_degrees().value(),
            self.longitude.0.as_degrees().value(),
            self.reference_frame.name()
        )
    }
}

impl EllipticalCoordinate {
    ///
    /// Constructs a new `EllipticalCoordinate` object
    #[must_use]
    pub const fn new(
        latitude: Latitude,
        longitude: Longitude,
        reference_frame: EllipticalShape,
    ) -> EllipticalCoordinate {
        EllipticalCoordinate {
            latitude,
            longitude,
            reference_frame,
            altitude: None,
            altitude_uncertainty: None,
            position_uncertainty: None,
            timestamp: None,
        }
    }

    ///
    /// Constructs a new `EllipticalCoordinate` object assuming [`AngleUnits::Degrees`] and [`standards::wgs84::WGS84_SHAPE`]
    #[must_use]
    pub const fn new_degrees_wgs84(latitude: f64, longitude: f64) -> EllipticalCoordinate {
        Self::new(
            Latitude(Angle::new(latitude, AngleUnits::Degrees)),
            Longitude(Angle::new(longitude, AngleUnits::Degrees)),
            standards::wgs84::WGS84_SHAPE,
        )
    }

    #[must_use]
    pub fn get_latitude(&self) -> &Latitude {
        &self.latitude
    }

    #[must_use]
    pub fn get_longitude(&self) -> &Longitude {
        &self.longitude
    }

    #[must_use]
    pub fn get_reference_frame(&self) -> &EllipticalShape {
        &self.reference_frame
    }

    #[must_use]
    pub fn get_altitude(&self) -> &Option<Altitude> {
        &self.altitude
    }

    #[must_use]
    pub fn get_altitude_uncertainty(&self) -> &Option<Length> {
        &self.altitude_uncertainty
    }

    #[must_use]
    pub fn get_timestamp(&self) -> &Option<UTCDateTime> {
        &self.timestamp
    }

    #[must_use]
    pub fn with_altitude(self, altitude: Altitude) -> EllipticalCoordinate {
        EllipticalCoordinate {
            altitude: Some(altitude),
            ..self
        }
    }

    #[must_use]
    pub fn with_timestamp(self, timestamp: UTCDateTime) -> EllipticalCoordinate {
        EllipticalCoordinate {
            timestamp: Some(timestamp),
            ..self
        }
    }

    #[must_use]
    pub fn position_uncertainty(&self) -> &Option<PositionUncertainty> {
        &self.position_uncertainty
    }
}

///
/// Allows the incremental building of an elliptical coordinate
#[derive(Debug, Default, Clone)]
pub struct EllipticalCoordinateBuilder {
    latitude: Option<Latitude>,
    longitude: Option<Longitude>,
    reference_frame: Option<EllipticalShape>,
    altitude: Option<Altitude>,
    altitude_uncertainty: Option<Length>,
    position_uncertainty: Option<PositionUncertainty>,
    timestamp: Option<UTCDateTime>,
}

impl EllipticalCoordinateBuilder {
    #[must_use]
    pub fn new() -> EllipticalCoordinateBuilder {
        Default::default()
    }

    pub fn with_latitude(&mut self, latitude: Latitude) -> &mut EllipticalCoordinateBuilder {
        self.latitude = Some(latitude);
        self
    }
    pub fn with_longitude(&mut self, longitude: Longitude) -> &mut EllipticalCoordinateBuilder {
        self.longitude = Some(longitude);
        self
    }
    pub fn with_reference_frame(
        &mut self,
        frame: EllipticalShape,
    ) -> &mut EllipticalCoordinateBuilder {
        self.reference_frame = Some(frame);
        self
    }
    pub fn with_altitude(&mut self, alt: Altitude) -> &mut EllipticalCoordinateBuilder {
        self.altitude = Some(alt);
        self
    }
    pub fn with_altitude_uncertainty(
        &mut self,
        alt_unk: Length,
    ) -> &mut EllipticalCoordinateBuilder {
        self.altitude_uncertainty = Some(alt_unk);
        self
    }
    pub fn with_position_uncertainty(
        &mut self,
        pos_unk: PositionUncertainty,
    ) -> &mut EllipticalCoordinateBuilder {
        self.position_uncertainty = Some(pos_unk);
        self
    }

    pub fn with_timestamp(&mut self, timestamp: UTCDateTime) -> &mut EllipticalCoordinateBuilder {
        self.timestamp = Some(timestamp);
        self
    }

    pub fn build(self) -> Result<EllipticalCoordinate, ConvertError> {
        let Some(latitude) = self.latitude else {
            return Err(ConvertError::MissingValue("Missing latitude".to_string()));
        };
        let Some(longitude) = self.longitude else {
            return Err(ConvertError::MissingValue("Missing longitude".to_string()));
        };
        let Some(reference_frame) = self.reference_frame else {
            return Err(ConvertError::MissingValue(
                "Missing reference frame".to_string(),
            ));
        };
        Ok(EllipticalCoordinate {
            latitude,
            longitude,
            reference_frame,
            altitude: self.altitude,
            altitude_uncertainty: self.altitude_uncertainty,
            position_uncertainty: self.position_uncertainty,
            timestamp: self.timestamp,
        })
    }
}

///
/// Represents a coordinate in 3D Cartesian Space (X, Y, Z)
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct CartesianCoordinate {
    x: Length,
    y: Length,
    z: Length,
    altitude: Option<Altitude>,
    altitude_uncertainty: Option<Length>,
    position_uncertainty: Option<PositionUncertainty>,
    timestamp: Option<UnixTimestamp>,
}

impl Display for CartesianCoordinate {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let alt = match self.altitude {
            Some(alt) => {
                format!(
                    " Alt: {}m {}",
                    alt.value().as_meters().value(),
                    alt.reference_frame().short_name()
                )
            }
            None => String::new(),
        };
        let alt_err = match self.altitude_uncertainty {
            Some(err) => format!("+/- {}m vert", err.as_meters().value()),
            None => String::new(),
        };
        let pos_err = match self.position_uncertainty {
            Some(err) => format!(" {err} horiz"),
            None => String::new(),
        };
        let asof = match self.timestamp {
            Some(ts) => format!(" as/of: {ts:?}"),
            None => String::new(),
        };
        write!(
            f,
            "X: {:0.5}m Y: {:0.5}m Z: {:0.5}m: {alt}{alt_err}{pos_err}{asof}",
            self.x.as_meters().value(),
            self.y.as_meters().value(),
            self.z.as_meters().value(),
        )
    }
}

impl CartesianCoordinate {
    #[must_use]
    pub fn new(x: Length, y: Length, z: Length) -> CartesianCoordinate {
        CartesianCoordinate {
            x,
            y,
            z,
            ..CartesianCoordinate::default()
        }
    }

    #[must_use]
    pub fn new_meters(x_meters: f64, y_meters: f64, z_meters: f64) -> CartesianCoordinate {
        Self::new(
            Length::new_meters(x_meters),
            Length::new_meters(y_meters),
            Length::new_meters(z_meters),
        )
    }

    #[must_use]
    pub fn get_x(&self) -> &Length {
        &self.x
    }

    #[must_use]
    pub fn get_y(&self) -> &Length {
        &self.y
    }

    #[must_use]
    pub fn get_z(&self) -> &Length {
        &self.z
    }

    #[must_use]
    pub fn get_altitude(&self) -> &Option<Altitude> {
        &self.altitude
    }

    #[must_use]
    pub fn get_altitude_uncertainty(&self) -> &Option<Length> {
        &self.altitude_uncertainty
    }

    #[must_use]
    pub fn get_timestamp(&self) -> &Option<UnixTimestamp> {
        &self.timestamp
    }

    #[must_use]
    pub fn with_altitude(self, altitude: Altitude) -> CartesianCoordinate {
        CartesianCoordinate {
            altitude: Some(altitude),
            ..self
        }
    }

    #[must_use]
    pub fn with_timestamp(self, timestamp: UnixTimestamp) -> CartesianCoordinate {
        CartesianCoordinate {
            timestamp: Some(timestamp),
            ..self
        }
    }

    #[must_use]
    pub fn position_uncertainty(&self) -> &Option<PositionUncertainty> {
        &self.position_uncertainty
    }
}

///
/// Allows the incremental building of an elliptical coordinate
#[derive(Debug, Default, Clone)]
pub struct CartesianCoordinateBuilder {
    x: Option<Length>,
    y: Option<Length>,
    z: Option<Length>,
    altitude: Option<Altitude>,
    altitude_uncertainty: Option<Length>,
    position_uncertainty: Option<PositionUncertainty>,
    timestamp: Option<UnixTimestamp>,
}

impl CartesianCoordinateBuilder {
    #[must_use]
    pub fn new() -> CartesianCoordinateBuilder {
        Default::default()
    }

    pub fn with_x(&mut self, x: Length) -> &mut CartesianCoordinateBuilder {
        self.x = Some(x);
        self
    }
    pub fn with_y(&mut self, y: Length) -> &mut CartesianCoordinateBuilder {
        self.y = Some(y);
        self
    }
    pub fn with_z(&mut self, z: Length) -> &mut CartesianCoordinateBuilder {
        self.z = Some(z);
        self
    }
    pub fn with_altitude(&mut self, alt: Altitude) -> &mut CartesianCoordinateBuilder {
        self.altitude = Some(alt);
        self
    }
    pub fn with_altitude_uncertainty(
        &mut self,
        alt_unk: Length,
    ) -> &mut CartesianCoordinateBuilder {
        self.altitude_uncertainty = Some(alt_unk);
        self
    }
    pub fn with_position_uncertainty(
        &mut self,
        pos_unk: PositionUncertainty,
    ) -> &mut CartesianCoordinateBuilder {
        self.position_uncertainty = Some(pos_unk);
        self
    }

    pub fn with_timestamp(&mut self, timestamp: UnixTimestamp) -> &mut CartesianCoordinateBuilder {
        self.timestamp = Some(timestamp);
        self
    }

    pub fn build(self) -> Result<CartesianCoordinate, ConvertError> {
        let Some(x) = self.x else {
            return Err(ConvertError::MissingValue("Missing x".to_string()));
        };
        let Some(y) = self.y else {
            return Err(ConvertError::MissingValue("Missing y".to_string()));
        };
        let Some(z) = self.z else {
            return Err(ConvertError::MissingValue("Missing z".to_string()));
        };
        Ok(CartesianCoordinate {
            x,
            y,
            z,
            altitude: self.altitude,
            altitude_uncertainty: self.altitude_uncertainty,
            position_uncertainty: self.position_uncertainty,
            timestamp: self.timestamp,
        })
    }
}

///
/// An uncertainty type for a position.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PositionUncertainty {
    /// Represents a uncertainty represented as a perfect circle, either radius or diameter.
    CircularUncertainty(CircularDimension),

    /// Represents an uncertainty represented as an ellipse, optionally oriented
    EllipticalUncertainty(Ellipse),
}

impl Display for PositionUncertainty {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            PositionUncertainty::CircularUncertainty(circ) => write!(f, "{circ}"),
            PositionUncertainty::EllipticalUncertainty(ell) => write!(f, "{ell}"),
        }
    }
}

///
/// A coordinate type that represents an Azimuth/Elevation look angle from a particular
/// refernece point.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HorizontalCoordinate {
    reference: AbsoluteCoordinateType,
    azimuth: Azimuth,
    elevation: Elevation,
}

#[cfg(all(target_os = "windows", feature = "windows"))]
pub mod windows_conv {
    extern crate alloc;
    use alloc::string::ToString;
    use windows::Devices::Geolocation::Geocoordinate;

    use irox_time::epoch::{FromTimestamp, UnixTimestamp, WindowsNTTimestamp};
    use irox_units::shapes::CircularDimension;
    use irox_units::units::angle::Angle;
    use irox_units::units::duration::{Duration, DurationUnit};
    use irox_units::units::length::Length;

    use crate::altitude::{Altitude, AltitudeReferenceFrame};
    use crate::coordinate::{
        EllipticalCoordinate, EllipticalCoordinateBuilder, Latitude, Longitude, PositionUncertainty,
    };
    use crate::error::ConvertError;
    use crate::geo::standards::wgs84::{WGS84_EPSG_CODE, WGS84_SHAPE};
    use crate::geo::EllipticalShape;

    pub const WINDOWS_2_NX_EPOCH_MICROS: i64 = 11_644_473_600_000_000;

    impl TryFrom<&Geocoordinate> for EllipticalCoordinate {
        type Error = ConvertError;

        fn try_from(value: &Geocoordinate) -> Result<Self, Self::Error> {
            let mut bld = EllipticalCoordinateBuilder::new();

            let Ok(point) = value.Point() else {
                return Err(ConvertError::MissingValue(
                    "Missing point value".to_string(),
                ));
            };
            let Ok(pos) = point.Position() else {
                return Err(ConvertError::MissingValue(
                    "Missing position value".to_string(),
                ));
            };
            bld.with_latitude(Latitude(Angle::new_degrees(pos.Latitude)));
            bld.with_longitude(Longitude(Angle::new_degrees(pos.Longitude)));

            let alt = Length::new_meters(pos.Altitude);
            let alt_frame = match point.AltitudeReferenceSystem() {
                Ok(frame) => match frame.0 {
                    1 => AltitudeReferenceFrame::Terrain,
                    2 => AltitudeReferenceFrame::Ellipsoid,
                    3 => AltitudeReferenceFrame::Geoid,
                    4 => AltitudeReferenceFrame::SurfaceFeatures,
                    _ => AltitudeReferenceFrame::Unspecified,
                },
                Err(_) => AltitudeReferenceFrame::Unspecified,
            };
            bld.with_altitude(Altitude::new(alt, alt_frame));

            bld.with_reference_frame(match point.SpatialReferenceId() {
                Ok(epsg) => match epsg {
                    WGS84_EPSG_CODE => WGS84_SHAPE,
                    e => EllipticalShape::EpsgDatum(e),
                },
                Err(_) => {
                    // assume wgs84.
                    WGS84_SHAPE
                }
            });

            if let Ok(acc) = value.Accuracy() {
                let length = Length::new_meters(acc);
                let rad = CircularDimension::new_radius(length);
                bld.with_position_uncertainty(PositionUncertainty::CircularUncertainty(rad));
            }
            if let Ok(acc) = value.AltitudeAccuracy() {
                if let Ok(acc) = acc.GetDouble() {
                    bld.with_altitude_uncertainty(Length::new_meters(acc));
                }
            }

            if let Ok(ts) = value.PositionSourceTimestamp() {
                if let Ok(ts) = ts.GetDateTime() {
                    let timestamp = WindowsNTTimestamp::from_offset(Duration::new(
                        ts.UniversalTime as f64 * 100.0_f64,
                        DurationUnit::Nanosecond,
                    ));
                    let timestamp: UnixTimestamp = UnixTimestamp::from_timestamp(&timestamp);
                    bld.with_timestamp(timestamp.into());
                }
            }

            bld.build()
        }
    }
}

impl Display for Latitude {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("Lat[{}]", self.0))
    }
}

impl Display for Longitude {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("Lon[{}]", self.0))
    }
}

impl Display for Elevation {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("Elv[{}]", self.0))
    }
}

#[macro_export]
macro_rules! assert_coordinate_eq_eps {
    ($left:expr, $right:expr, $eps:expr) => {
        match (&$left, &$right) {
            (left_val, right_val) => {
                irox_units::assert_length_eq_eps!(left_val.get_x(), right_val.get_x(), $eps);
                irox_units::assert_length_eq_eps!(left_val.get_y(), right_val.get_y(), $eps);
                irox_units::assert_length_eq_eps!(left_val.get_z(), right_val.get_z(), $eps);
            }
        }
    };
}