dted2 1.0.0

Tool for reading DTED files
Documentation
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//! Contains primitive items used through the crate.

use thiserror::Error;
use std::ops::{ Add, Div, Mul, Sub };
use num_traits::{ FromPrimitive, ToPrimitive };

/// Seconds -> Degrees
pub const SEC2DEG: f64 = 3600.0;
/// Seconds -> Minutes
pub const SEC2MIN: f64 = 60.0;
/// Minutes -> Degrees
pub const MIN2DEG: f64 = 60.0;

#[derive(Debug, Error)]
/// Errors that can occur when converting an angle
pub enum AngleError {
    #[error("Seconds must be less than 60")]
    SecondsUpperBoundBreached,
    #[error("Seconds must be non-negative. To set a negative `Angle`, please set the `negative` parameter to `true`.")]
    SecondsLowerBoundBreached,
    #[error("Minutes must be less than 60")]
    MinutesUpperBoundBreached,
    #[error("{0}s is too large to be an Angle")]
    TooLarge(f64),
}

#[derive(Debug, Copy, Clone)]
/// An angle in degrees, minutes, and seconds
///
/// See: [https://en.wikipedia.org/wiki/Geographic_coordinate_system](https://en.wikipedia.org/wiki/Geographic_coordinate_system)
///
/// Both `min` and `sec` must be less than 60, and, despite being an `f64`, `sec` must always be positive.
/// The fields are private to enforce these invariants, but can be accessed via methods.
///
/// # Example
///
/// ```
/// use dted2::primitives::Angle;
///
/// let angle = Angle::new(0, 1, 0.0, false);
/// assert_eq!(angle, Angle::from_secs(60.0));
///
/// let angle = Angle::new(1, 0, 0.0, true);
/// assert_eq!(angle, Angle::from_secs(-3600.0));
/// ```
pub struct Angle {
    deg: u16,
    min: u8,
    sec: f64,
    negative: bool,
}
impl Angle {
    /// Converts degrees, minutes, and seconds to an angle
    ///
    /// # Arguments
    ///
    /// * `deg` - The number of degrees
    /// * `min` - The number of minutes
    /// * `sec` - The number of seconds (floating point precision), must always be non-negative
    /// * `negative` - Whether or nor the angle is negative
    ///
    /// # Returns
    ///
    /// The [Angle] with the number of degrees, minutes, and seconds
    ///
    /// # Panics
    ///
    /// A panic will occur if either `min` or `sec` are at least 60, or if `sec` is negative.
    ///
    /// # Examples
    ///
    /// ```
    /// use dted2::primitives::Angle;
    ///
    /// let angle = Angle::new(0, 1, 1.0, false);
    /// assert_eq!(angle, Angle::from_secs(61.0));
    ///
    /// let angle = Angle::new(123, 45, 43.0, true);
    /// assert_eq!(angle, Angle::from_secs(-445543.0));
    /// ```
    ///
    /// ```should_panic
    /// # use dted2::primitives::Angle;
    /// let angle = Angle::new(45, 67, 4.0, false);
    /// ```
    ///
    /// ```should_panic
    /// # use dted2::primitives::Angle;
    /// let angle = Angle::new(45, 4, 60.0, false);
    /// ```
    ///
    /// ```should_panic
    /// # use dted2::primitives::Angle;
    /// let angle = Angle::new(45, 4, -4.0, false);
    /// ```
    pub fn new(deg: u16, min: u8, sec: f64, negative: bool) -> Self {
        if min >= 60 { panic!("{}", AngleError::MinutesUpperBoundBreached); }
        if sec >= 60.0 { panic!("{}", AngleError::SecondsUpperBoundBreached); }
        if sec < 0.0 { panic!("{}", AngleError::SecondsLowerBoundBreached); }
        Angle {
            deg,
            min,
            sec,
            negative,
        }
    }

    /// Returns whether or not the angle is negative.
    ///
    /// # Examples
    ///
    /// ```
    /// use dted2::primitives::Angle;
    ///
    /// assert!(!Angle::from_secs(4567.0).is_negative());
    /// assert!(Angle::from_secs(-4567.0).is_negative());
    /// ```
    pub fn is_negative(&self) -> bool {
        self.negative
    }

    /// Returns the positive integer number of degrees.
    ///
    /// # Examples
    ///
    /// ```
    /// use dted2::primitives::Angle;
    ///
    /// assert_eq!(Angle::new(123, 55, 28.2, false).deg(), 123);
    /// assert_eq!(Angle::new(47, 4, 32.0, true).deg(), 47)
    /// ```
    #[inline]
    pub fn deg(&self) -> u16 {
        self.deg
    }

    /// Returns the positive integer number of minutes.
    ///
    /// # Examples
    ///
    /// ```
    /// use dted2::primitives::Angle;
    ///
    /// assert_eq!(Angle::new(123, 55, 28.2, false).min(), 55);
    /// assert_eq!(Angle::new(47, 4, 32.0, true).min(), 4)
    /// ```
    #[inline]
    pub fn min(&self) -> u8 {
        self.min
    }

    /// Returns the positive number of seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use dted2::primitives::Angle;
    ///
    /// assert_eq!(Angle::new(123, 55, 28.2, false).sec(), 28.2);
    /// assert_eq!(Angle::new(47, 4, 32.0, true).sec(), 32.0)
    /// ```
    #[inline]
    pub fn sec(&self) -> f64 {
        self.sec
    }

    /// Converts seconds to degrees, minutes, and seconds
    /// AKA an [Angle]
    ///
    /// # Arguments
    ///
    /// * `sec` - The number of seconds, which can be negative
    ///
    ///
    /// # Returns
    ///
    /// The number of degrees, minutes, and seconds as an [Angle].
    ///
    /// # Panics
    ///
    /// A panic will occur if `total_secs` is too large to be represented as an [Angle].
    ///
    /// # Examples
    ///
    /// ```
    /// use dted2::primitives::Angle;
    ///
    /// assert_eq!(Angle::from_secs(61.0), Angle::new(0, 1, 1.0, false));
    /// assert_eq!(Angle::new(123, 45, 43.8, true).total_secs(), -445543.8);
    /// ```
    ///
    /// ```should_panic
    /// use dted2::primitives::Angle;
    ///
    /// Angle::from_secs(1e10);
    /// ```
    pub fn from_secs(total_sec: f64) -> Self {
        let sec_abs = total_sec.abs();

        if sec_abs > (u16::MAX as f64) * SEC2DEG {
            panic!("{}", AngleError::TooLarge(total_sec));
        }

        let sec_int = sec_abs as u32;

        let deg = sec_int / 3600;
        let min = (sec_int % 3600) / 60;
        let sec = sec_abs - (deg * 3600 + min * 60) as f64;

        Angle {
            deg: deg as u16,
            min: min as u8,
            sec,
            negative: total_sec < 0.0,
        }
    }

    /// Computes the signed total arc seconds of the angle.
    ///
    /// # Examples
    ///
    /// ```
    /// use dted2::primitives::Angle;
    ///
    /// assert_eq!(Angle::new(0, 1, 1.0, false).total_secs(), 61.0);
    /// assert_eq!(Angle::new(123, 45, 43.8, true).total_secs(), -445543.8);
    /// ```
    pub fn total_secs(&self) -> f64 {
        let secs_abs = (self.deg as u32 * 3600 + self.min as u32 * 60) as f64 + self.sec;
        (((self.negative as i8 * -2) + 1) as f64) * secs_abs
    }
}

/// Compares two [Angle]s, taking into account that positive zero is the same as negative zero.
///
/// # Examples
/// ```
/// use dted2::primitives::Angle;
///
/// assert_eq!(Angle::new(1, 1, 1.0, false), Angle::new(1, 1, 1.0, false));
/// assert_ne!(Angle::new(1, 1, 1.0, false), Angle::new(1, 1, 1.0, true));
/// assert_ne!(Angle::new(1, 1, 1.0, false), Angle::new(1, 1, 2.0, false));
/// assert_eq!(Angle::new(0, 0, 0.0, false), Angle::new(0, 0, 0.0, true));
/// ```
impl PartialEq for Angle {
    fn eq(&self, other: &Self) -> bool {
        let is_zero = self.deg == 0 || self.min == 0 || self.sec == 0.0;
        (is_zero || self.negative == other.negative)
            && self.deg == other.deg
            && self.min == other.min
            && self.sec == other.sec
    }
}

/// Add's an [Angle] to another [Angle]
///
/// # Returns
///
/// * [Angle]
///
impl Add for Angle {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Angle::from_secs(self.total_secs() + rhs.total_secs())
    }
}

/// Subtracts an [Angle] from another [Angle]
///
/// # Returns
///
/// * [Angle]
impl Sub for Angle {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Angle::from_secs(self.total_secs() - rhs.total_secs())
    }
}
/// Multiplies an [Angle] by another [Angle]
///
/// # Returns
///
/// * [Angle]
impl Mul for Angle {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Angle::from_secs(self.total_secs() * rhs.total_secs())
    }
}
/// Multiplies an [Angle] by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [Angle]
impl<T> Mul<T> for Angle
where
    T: ToPrimitive,
{
    type Output = Self;
    fn mul(self, rhs: T) -> Self::Output {
        Angle::from_secs(self.total_secs() * T::to_f64(&rhs).unwrap_or(0.0))
    }
}
/// Divides an [Angle] by another [Angle]
///
/// # Returns
///
/// * [Angle]
impl Div for Angle {
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        Angle::from_secs(self.total_secs() / rhs.total_secs())
    }
}
/// Divides an [Angle] by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [Angle]
impl<T> Div<T> for Angle
where
    T: ToPrimitive,
{
    type Output = Self;
    fn div(self, rhs: T) -> Self::Output {
        Angle::from_secs(self.total_secs() / T::to_f64(&rhs).unwrap_or(1.0))
    }
}

/// Converts an [Angle] to degrees of variable precision
macro_rules! impl_type_from_angle {
    ($($type:ty),*) => {
        $(
            #[doc = concat!(" Converts an [Angle] (degrees, minutes, seconds) to radians as ")]
            #[doc = concat!(" a specific numeric type (`", stringify!($type), "`).")]
            #[doc = concat!("")]
            #[doc = concat!(" # Example")]
            #[doc = concat!("")]
            #[doc = concat!(" ```")]
            #[doc = concat!(" use dted2::primitives::Angle;")]
            #[doc = concat!("")]
            #[doc = concat!(" let angle = Angle::new(0, 0, 0.0, false);")]
            #[doc = concat!(" let radians: ", stringify!($type), " = angle.into();")]
            #[doc = concat!(" assert_eq!(radians, 0.0 as ", stringify!($type), ");")]
            #[doc = concat!(" ```")]
            impl ::std::convert::From<Angle> for $type {
                fn from(value: Angle) -> Self {
                    let abs = value.deg as $type +
                        value.min as $type / (60.0 as $type) +
                        value.sec as $type / (3600.0 as $type);
                    abs * ((value.negative as i8 * -2 + 1) as $type)
                }
            }
            #[doc = concat!(" Converts an [`AxisElement<Angle>`] to [`AxisElement<", stringify!($type), ">`].")]
            impl ::std::convert::From<AxisElement<Angle>> for AxisElement<$type> {
                fn from(value: AxisElement<Angle>) -> Self {
                    AxisElement {
                        lat: value.lat.into(),
                        lon: value.lon.into(),
                    }
                }
            }
        )*
    };
}
impl_type_from_angle!(f32);
impl_type_from_angle!(f64);
impl_type_from_angle!(i16);
impl_type_from_angle!(i32);
impl_type_from_angle!(i64);
impl_type_from_angle!(i128);
impl_type_from_angle!(isize);

#[derive(Copy, Clone, Debug, PartialEq)]
/// An Axis element
///
/// # Fields
///
/// * `lat`: Latitude
/// * `lon`: Longitude
pub struct AxisElement<T> {
    pub lat: T,
    pub lon: T,
}
impl<T> AxisElement<T> {
    pub fn new(lat: T, lon: T) -> Self {
        Self { lat, lon }
    }
}
/// Adds a [AxisElement]<[Angle]> to another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(Angle::new(3, 2, 59.0, false), Angle::new(0, 0, 0.0, false));
/// let b = AxisElement::new(Angle::new(0, 1, 1.0, true), Angle::new(0, 2, 0.0, true));
/// let c = a + b;
/// assert_eq!(c, AxisElement::new(Angle::new(3, 1, 58.0, false), Angle::new(0, 2, 0.0, true)));
/// ```
impl Add for AxisElement<Angle> {
    type Output = AxisElement<Angle>;
    fn add(self, rhs: Self) -> Self::Output {
        AxisElement {
            lat: self.lat + rhs.lat,
            lon: self.lon + rhs.lon,
        }
    }
}
/// Adds a to a scalar `A` (max precision of f64) to a [AxisElement]`<T>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(-10, 20);
/// let b = 12.0;
/// let c = a + b;
/// assert_eq!(c, AxisElement::new(2, 32));
/// ```
impl<A, T> Add<A> for AxisElement<T>
where
    A: Copy + ToPrimitive,
    T: Copy + FromPrimitive + Add<Output = T>,
{
    type Output = AxisElement<T>;
    fn add(self, rhs: A) -> Self::Output {
        let rhs = A::to_f64(&rhs).expect("Failed to convert RHS to f64");
        AxisElement {
            lat: self.lat
                + T::from_f64(rhs).unwrap_or_else(|| {
                    panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
                }),
            lon: self.lon
                + T::from_f64(rhs).unwrap_or_else(|| {
                    panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
                }),
        }
    }
}
/// Adds a [AxisElement]`<A>` to a scalar [AxisElement]`<T>`,
/// using max precision of f64
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(10, 20);
/// let b = AxisElement::new(12, 32);
/// let c = a + b;
/// assert_eq!(c, AxisElement::new(22, 52));
/// ```
impl<A, T> Add<AxisElement<A>> for AxisElement<T>
where
    A: ToPrimitive,
    T: ToPrimitive + FromPrimitive + Add<Output = T>,
{
    type Output = AxisElement<T>;
    fn add(self, rhs: AxisElement<A>) -> Self::Output {
        let rhs_lat: f64 = A::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
        let rhs_lon: f64 = A::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
        let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
        let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
        AxisElement {
            lat: T::from_f64(lat + rhs_lat).unwrap_or_else(|| {
                panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
            }),
            lon: T::from_f64(lon + rhs_lon).unwrap_or_else(|| {
                panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
            }),
        }
    }
}
/// Subtracts a [AxisElement]<[Angle]> from another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(Angle::new(3, 1, 1.0, false), Angle::new(0, 2, 0.0, true));
/// let b = AxisElement::new(Angle::new(0, 10, 58.0, true), Angle::new(0, 1, 0.0, false));
/// let c = a - b;
/// assert_eq!(c, AxisElement::new(Angle::new(3, 11, 59.0, false), Angle::new(0, 3, 0.0, true)));
/// ```
impl Sub for AxisElement<Angle> {
    type Output = AxisElement<Angle>;
    fn sub(self, rhs: Self) -> Self::Output {
        AxisElement {
            lat: self.lat - rhs.lat,
            lon: self.lon - rhs.lon,
        }
    }
}
/// Subtracts a scalar `S` (max precision of f64) from a [AxisElement]`<T>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(-10, 20);
/// let b = 12.0;
/// let c = a - b;
/// assert_eq!(c, AxisElement::new(-22, 8));
/// ```
impl<S, T> Sub<S> for AxisElement<T>
where
    S: Copy + ToPrimitive,
    T: Copy + FromPrimitive + Sub<Output = T>,
{
    type Output = AxisElement<T>;
    fn sub(self, rhs: S) -> Self::Output {
        let rhs = S::to_f64(&rhs).expect("Failed to convert RHS to f64");
        AxisElement {
            lat: self.lat
                - T::from_f64(rhs).unwrap_or_else(|| {
                    panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
                }),
            lon: self.lon
                - T::from_f64(rhs).unwrap_or_else(|| {
                    panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
                }),
        }
    }
}
/// Subtracts a [AxisElement]`<S>` from a [AxisElement]`<T>`,
/// using max precision of f64
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(-10, 20);
/// let b = AxisElement::new(12, 32);
/// let c = a - b;
/// assert_eq!(c, AxisElement::new(-22, -12));
/// ```
impl<S, T> Sub<AxisElement<S>> for AxisElement<T>
where
    S: ToPrimitive,
    T: ToPrimitive + FromPrimitive + Sub<Output = T>,
{
    type Output = AxisElement<T>;
    fn sub(self, rhs: AxisElement<S>) -> Self::Output {
        let rhs_lat: f64 = S::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
        let rhs_lon: f64 = S::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
        let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
        let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
        AxisElement {
            lat: T::from_f64(lat - rhs_lat).unwrap_or_else(|| {
                panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
            }),
            lon: T::from_f64(lon - rhs_lon).unwrap_or_else(|| {
                panic!("Failed to convert f64 to {}", std::any::type_name::<T>())
            }),
        }
    }
}
/// Multiplies a [AxisElement]<[Angle]> by another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Mul for AxisElement<Angle> {
    type Output = AxisElement<Angle>;
    fn mul(self, rhs: Self) -> Self::Output {
        AxisElement {
            lat: self.lat * rhs.lat,
            lon: self.lon * rhs.lon,
        }
    }
}
/// Multiplies a [AxisElement]<[Angle]> by a [Angle]
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Mul<Angle> for AxisElement<Angle> {
    type Output = AxisElement<Angle>;
    fn mul(self, rhs: Angle) -> Self::Output {
        AxisElement {
            lat: self.lat * rhs,
            lon: self.lon * rhs,
        }
    }
}
/// Multiplies a [AxisElement]<[Angle]> by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<M> Mul<M> for AxisElement<Angle>
where
    M: Copy + ToPrimitive,
{
    type Output = AxisElement<Angle>;
    fn mul(self, rhs: M) -> Self::Output {
        AxisElement {
            lat: self.lat * rhs,
            lon: self.lon * rhs,
        }
    }
}
/// Multiplies a [AxisElement]<[Angle]> by a [AxisElement]`<M>`
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<M> Mul<AxisElement<M>> for AxisElement<Angle>
where
    M: Copy + ToPrimitive,
{
    type Output = AxisElement<Angle>;
    fn mul(self, rhs: AxisElement<M>) -> Self::Output {
        AxisElement {
            lat: self.lat * rhs.lat,
            lon: self.lon * rhs.lon,
        }
    }
}
/// Multiplies a [AxisElement]`<T>` by a [AxisElement]`<M>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<M, T> Mul<AxisElement<M>> for AxisElement<T>
where
    M: ToPrimitive + FromPrimitive,
    T: ToPrimitive,
{
    type Output = AxisElement<M>;
    fn mul(self, rhs: AxisElement<M>) -> Self::Output {
        let rhs_lat: f64 = M::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
        let rhs_lon: f64 = M::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
        let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
        let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
        AxisElement {
            lat: M::from_f64(lat * rhs_lat).expect("Failed to convert latitude from f64"),
            lon: M::from_f64(lon * rhs_lon).expect("Failed to convert longitude from f64"),
        }
    }
}
/// Multiplies a [AxisElement]`<T>` by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<M, T> Mul<M> for AxisElement<T>
where
    M: ToPrimitive + FromPrimitive,
    T: ToPrimitive,
{
    type Output = AxisElement<M>;
    fn mul(self, rhs: M) -> Self::Output {
        let rhs: f64 = M::to_f64(&rhs).expect("Failed to convert RHS to f64");
        let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
        let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
        AxisElement {
            lat: M::from_f64(lat * rhs).expect("Failed to convert latitude from f64"),
            lon: M::from_f64(lon * rhs).expect("Failed to convert longitude from f64"),
        }
    }
}
/// Divides a [AxisElement]<[Angle]> by another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Div for AxisElement<Angle> {
    type Output = AxisElement<Angle>;
    fn div(self, rhs: Self) -> Self::Output {
        AxisElement {
            lat: self.lat / rhs.lat,
            lon: self.lon / rhs.lon,
        }
    }
}
/// Divides a [AxisElement]<[Angle]> by a [Angle]
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Div<Angle> for AxisElement<Angle> {
    type Output = AxisElement<Angle>;
    fn div(self, rhs: Angle) -> Self::Output {
        AxisElement {
            lat: self.lat / rhs,
            lon: self.lon / rhs,
        }
    }
}
/// Divides a [AxisElement]<[Angle]> by a scalar `D` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<D> Div<D> for AxisElement<Angle>
where
    D: Copy + ToPrimitive,
{
    type Output = AxisElement<Angle>;
    fn div(self, rhs: D) -> Self::Output {
        AxisElement {
            lat: self.lat / rhs,
            lon: self.lon / rhs,
        }
    }
}
/// Divides a [AxisElement]<[Angle]> by a [AxisElement]`<D>`
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<D> Div<AxisElement<D>> for AxisElement<Angle>
where
    D: Copy + ToPrimitive,
{
    type Output = AxisElement<Angle>;
    fn div(self, rhs: AxisElement<D>) -> Self::Output {
        AxisElement {
            lat: self.lat / rhs.lat,
            lon: self.lon / rhs.lon,
        }
    }
}
/// Divides a [AxisElement]`<T>` by a [AxisElement]`<D>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<D, T> Div<AxisElement<D>> for AxisElement<T>
where
    D: Copy + ToPrimitive + FromPrimitive,
    T: Copy + ToPrimitive,
{
    type Output = AxisElement<D>;
    fn div(self, rhs: AxisElement<D>) -> Self::Output {
        let rhs_lat: f64 = D::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
        let rhs_lon: f64 = D::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
        let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
        let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
        AxisElement {
            lat: D::from_f64(lat / rhs_lat).expect("Failed to convert latitude from f64"),
            lon: D::from_f64(lon / rhs_lon).expect("Failed to convert longitude from f64"),
        }
    }
}
/// Divides a [AxisElement]`<T>` by a scalar `D` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<D, T> Div<D> for AxisElement<T>
where
    D: Copy + ToPrimitive + FromPrimitive,
    T: Copy + ToPrimitive,
{
    type Output = AxisElement<D>;
    fn div(self, rhs: D) -> Self::Output {
        let rhs: f64 = D::to_f64(&rhs).expect("Failed to convert RHS to f64");
        let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
        let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
        AxisElement {
            lat: D::from_f64(lat / rhs).expect("Failed to convert latitude from f64"),
            lon: D::from_f64(lon / rhs).expect("Failed to convert longitude from f64"),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    /// Test [Angle] conversions to various primitive types
    fn angle_conversions() {
        let angle = Angle::new(123, 45, 43.8, true);

        assert_eq!(f64::from(angle), -123.76216666666667f64);
        assert_eq!(i16::from(angle), -123);
        assert_eq!(i32::from(angle), -123);
        assert_eq!(i64::from(angle), -123);
        assert_eq!(i128::from(angle), -123);
        assert_eq!(isize::from(angle), -123);
    }
}