Skip to main content

grib/
projection.rs

1//! Map projection functionality.
2
3pub use lcc::{Params as LccParams, Projection as Lcc};
4pub use merc::{Params as MercParams, Projection as Merc};
5pub use stere::{Params as StereParams, Projection as Stere};
6
7/// Map projection functionality.
8pub trait Project {
9    fn forward(&self, xy: &(f64, f64)) -> Result<(f64, f64), &'static str>;
10    fn inverse(&self, xy: &(f64, f64)) -> Result<(f64, f64), &'static str>;
11    fn a(&self) -> &f64;
12    fn lam0(&self) -> &f64;
13
14    /// Performs a projection.
15    ///
16    /// For forward transformation (`inverse = false`), `xy` is treated as
17    /// `(lambda, phi)`, where `lambda` represents longitude and `phi`
18    /// represents latitude, both expressed in degrees. The return value
19    /// should be considered to be `(x, y)`, and both `x` and `y` are values in
20    /// meters.
21    ///
22    /// For inverse transformation (`inverse = true`), `xy` is treated as `(x,
23    /// y)` and both `x` and `y` are values in meters.
24    /// The return value should be considered to be `(lambda, phi)`, where
25    /// `lambda` represents longitude and `phi` represents latitude, both
26    /// expressed in degrees.
27    fn project(&self, xy: &(f64, f64), inverse: bool) -> Result<(f64, f64), &'static str> {
28        if inverse {
29            let &(x, y) = xy;
30            let x = x / self.a();
31            let y = y / self.a();
32            let (lambda, phi) = self.inverse(&(x, y))?;
33            let lambda = helpers::normalize_longitude(lambda + self.lam0());
34            Ok((lambda, phi))
35        } else {
36            let &(lambda, phi) = xy;
37            let lambda = helpers::normalize_longitude(lambda - self.lam0());
38            let (x, y) = self.forward(&(lambda, phi))?;
39            let x = x * self.a();
40            let y = y * self.a();
41            Ok((x, y))
42        }
43    }
44}
45
46#[cfg(feature = "gridpoints-proj")]
47pub(crate) trait OsgeoProj {
48    fn proj_args(&self) -> String;
49}
50
51/// Ellipsoid definition.
52#[derive(Debug, PartialEq, Clone)]
53pub struct Ellipsoid {
54    /// Semimajor radius of the ellipsoid axis (in meters).
55    pub a: f64,
56    /// Semiminor radius of the ellipsoid axis (in meters).
57    pub b: f64,
58    /// Eccentricity.
59    pub e: f64,
60    /// Eccentricity squared.
61    pub e_sq: f64,
62}
63
64impl Ellipsoid {
65    pub fn from_a_and_b(a: f64, b: f64) -> Self {
66        let f = (a - b) / a;
67        let e_sq = 2. * f - f * f;
68        let e = e_sq.sqrt();
69        Self { a, b, e, e_sq }
70    }
71}
72
73mod helpers;
74mod lcc;
75mod merc;
76mod stere;