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
use geo::Coord;
use geo::CoordFloat;
use num_traits::FloatConst;

use crate::math::EPSILON;
use crate::Transform;

use super::builder::types::BuilderAntimeridianResampleNoClip;
use super::builder_conic::Builder;
use super::builder_conic::PRConic;
use super::conic_equal_area::ConicEqualArea;
use super::cylindrical_equal_area::CylindricalEqualArea;
use super::BuilderTrait;
use super::CenterSet;
use super::RawBase;
use super::ScaleSet;

/// [`ConicEqualArea`] return type.
///
/// Depending construction parameters
/// one of two Projection types are returned.
#[derive(Clone, Debug, Default)]
pub enum EqualArea<T> {
    /// Parallels symetical around the Equator.
    Cyl(CylindricalEqualArea<T>),
    /// Conic
    Con(ConicEqualArea<T>),
    /// State before the parallels are set.
    #[default]
    Uninitialized,
}
impl<T> Transform for EqualArea<T>
where
    T: CoordFloat + FloatConst,
{
    type T = T;
    fn transform(&self, p: &Coord<T>) -> Coord<T> {
        match self {
            Self::Cyl(cyl) => cyl.transform(p),
            Self::Con(con) => con.transform(p),
            Self::Uninitialized => Coord {
                x: T::nan(),
                y: T::nan(),
            },
        }
    }

    #[inline]
    fn invert(&self, p: &Coord<T>) -> Coord<T> {
        match self {
            Self::Cyl(cyl) => cyl.invert(p),
            Self::Con(con) => con.invert(p),
            Self::Uninitialized => Coord {
                x: T::zero(),
                y: T::zero(),
            },
        }
    }
}

impl<T> PRConic for EqualArea<T>
where
    T: 'static + CoordFloat + Default + FloatConst,
{
    /// Inputs select either a conic or a cylindrical projection.
    ///
    /// # Panics
    /// `unwrap()` is used here but a panic will never happen as EPSILON will always be converted into T.
    fn generate(self, y0: T, y1: T) -> Self {
        let two = T::from(2_f64).unwrap();
        let sy0 = y0.sin();
        let n = (sy0 + y1.sin()) / two;

        // Are the parallels symmetrical around the Equator?
        if n.abs() < T::from(EPSILON).unwrap() {
            return Self::Cyl(CylindricalEqualArea::new(y0));
        }
        let c = T::one() + sy0 * (two * n - sy0);
        let r0 = c.sqrt() / n;
        let two = T::from(2_f64).unwrap();
        Self::Con(ConicEqualArea::new(c, n, r0, two))
    }
}

impl<T> RawBase for EqualArea<T>
where
    T: 'static + CoordFloat + Default + FloatConst,
{
    type Builder<DRAIN: Clone> =
        Builder<BuilderAntimeridianResampleNoClip<DRAIN, Self, T>, T>;

    fn builder<DRAIN: Clone>() -> Self::Builder<DRAIN> {
        let mut b = Builder::new(Self::default());
        b.scale_set(T::from(155.424).unwrap());
        b.center_set(&Coord {
            x: T::zero(),
            y: T::from(33.6442).unwrap(),
        });
        b
    }
}