|
| 1 | +//! |
| 2 | +//! From proj/eqc.cpp |
| 3 | +//! |
| 4 | +//! See also <https://proj.org/operations/projections/eqc.html> |
| 5 | +//! |
| 6 | +//! Simplest of all projections |
| 7 | +//! |
| 8 | +//! eqc: "Equidistant Cylindrical (Plate Carree)" |
| 9 | +//! |
| 10 | +
|
| 11 | +use crate::ellps::Ellipsoid; |
| 12 | +use crate::errors::{Error, Result}; |
| 13 | +use crate::parameters::ParamList; |
| 14 | +use crate::proj::ProjData; |
| 15 | + |
| 16 | +// Projection stub |
| 17 | +super::projection! { eqc } |
| 18 | + |
| 19 | +#[derive(Debug, Clone)] |
| 20 | +pub(crate) struct Projection { |
| 21 | + rc: f64, |
| 22 | + phi0: f64, |
| 23 | +} |
| 24 | + |
| 25 | +impl Projection { |
| 26 | + pub fn eqc(p: &mut ProjData, params: &ParamList) -> Result<Self> { |
| 27 | + let rc = params.try_angular_value("lat_ts")?.unwrap_or(0.).cos(); |
| 28 | + if rc <= 0. { |
| 29 | + return Err(Error::InvalidParameterValue("lat_ts should be <= 90°")); |
| 30 | + } |
| 31 | + p.ellps = Ellipsoid::sphere(p.ellps.a)?; |
| 32 | + Ok(Self { rc, phi0: p.phi0 }) |
| 33 | + } |
| 34 | + |
| 35 | + #[inline(always)] |
| 36 | + pub fn forward(&self, lam: f64, phi: f64, z: f64) -> Result<(f64, f64, f64)> { |
| 37 | + Ok((lam * self.rc, phi - self.phi0, z)) |
| 38 | + } |
| 39 | + |
| 40 | + #[inline(always)] |
| 41 | + pub fn inverse(&self, x: f64, y: f64, z: f64) -> Result<(f64, f64, f64)> { |
| 42 | + Ok((x / self.rc, y + self.phi0, z)) |
| 43 | + } |
| 44 | + |
| 45 | + pub const fn has_inverse() -> bool { |
| 46 | + true |
| 47 | + } |
| 48 | + |
| 49 | + pub const fn has_forward() -> bool { |
| 50 | + true |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use crate::math::consts::EPS_10; |
| 58 | + use crate::proj::Proj; |
| 59 | + use crate::tests::utils::{test_proj_forward, test_proj_inverse}; |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn proj_eqc_wgs84() { |
| 63 | + let p = Proj::from_proj_string("+proj=eqc +ellps=WGS84").unwrap(); |
| 64 | + |
| 65 | + println!("{:#?}", p.projection()); |
| 66 | + |
| 67 | + let inputs = [ |
| 68 | + ((2., 47., 0.), (222638.98158654713, 5232016.06728385761, 0.)), |
| 69 | + ]; |
| 70 | + |
| 71 | + test_proj_forward(&p, &inputs, EPS_10); |
| 72 | + test_proj_inverse(&p, &inputs, EPS_10); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn proj_eqc_lat_ts() { |
| 77 | + let p = Proj::from_proj_string("+proj=eqc +lat_ts=30 +lon_0=-90").unwrap(); |
| 78 | + |
| 79 | + println!("{:#?}", p.projection()); |
| 80 | + |
| 81 | + let inputs = [ |
| 82 | + ((-88., 30., 0.), (192811.01392664597, 3339584.72379820701, 0.)), |
| 83 | + ]; |
| 84 | + |
| 85 | + test_proj_forward(&p, &inputs, EPS_10); |
| 86 | + test_proj_inverse(&p, &inputs, EPS_10); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments