Skip to content

Commit

Permalink
Add Equidistant Cylindrical 'eqc' projection
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarteau committed Oct 14, 2024
1 parent 7127768 commit 85568fa
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
89 changes: 89 additions & 0 deletions proj4rs/src/projections/eqc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//!
//! From proj/eqc.cpp
//!
//! See also <https://proj.org/operations/projections/eqc.html>
//!
//! Simplest of all projections
//!
//! eqc: "Equidistant Cylindrical (Plate Carree)"
//!

use crate::ellps::Ellipsoid;
use crate::errors::{Error, Result};
use crate::parameters::ParamList;
use crate::proj::ProjData;

// Projection stub
super::projection! { eqc }

#[derive(Debug, Clone)]
pub(crate) struct Projection {
rc: f64,
phi0: f64,
}

impl Projection {
pub fn eqc(p: &mut ProjData, params: &ParamList) -> Result<Self> {
let rc = params.try_angular_value("lat_ts")?.unwrap_or(0.).cos();
if rc <= 0. {
return Err(Error::InvalidParameterValue("lat_ts should be <= 90°"));
}
p.ellps = Ellipsoid::sphere(p.ellps.a)?;
Ok(Self { rc, phi0: p.phi0 })
}

#[inline(always)]
pub fn forward(&self, lam: f64, phi: f64, z: f64) -> Result<(f64, f64, f64)> {
Ok((lam * self.rc, phi - self.phi0, z))
}

#[inline(always)]
pub fn inverse(&self, x: f64, y: f64, z: f64) -> Result<(f64, f64, f64)> {
Ok((x / self.rc, y + self.phi0, z))
}

pub const fn has_inverse() -> bool {
true
}

pub const fn has_forward() -> bool {
true
}
}


#[cfg(test)]
mod tests {
use crate::math::consts::EPS_10;
use crate::proj::Proj;
use crate::tests::utils::{test_proj_forward, test_proj_inverse};

#[test]
fn proj_eqc_wgs84() {
let p = Proj::from_proj_string("+proj=eqc +ellps=WGS84").unwrap();

println!("{:#?}", p.projection());

let inputs = [
((2., 47., 0.), (222638.98158654713, 5232016.06728385761, 0.)),
];

test_proj_forward(&p, &inputs, EPS_10);
test_proj_inverse(&p, &inputs, EPS_10);
}

#[test]
fn proj_eqc_lat_ts() {
let p = Proj::from_proj_string("+proj=eqc +lat_ts=30 +lon_0=-90").unwrap();

println!("{:#?}", p.projection());

let inputs = [
((-88., 30., 0.), (192811.01392664597, 3339584.72379820701, 0.)),
];

test_proj_forward(&p, &inputs, EPS_10);
test_proj_inverse(&p, &inputs, EPS_10);
}

}
4 changes: 3 additions & 1 deletion proj4rs/src/projections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ macro_rules! projection {
use downcast;
use projection;

const NUM_PROJECTIONS: usize = 21;
const NUM_PROJECTIONS: usize = 22;

macro_rules! declare_projections {
($(($name:ident $(,)? $($init:ident),*)),+ $(,)?) => {
Expand All @@ -161,6 +161,7 @@ macro_rules! declare_projections {
// ---------------------------

pub mod aea;
pub mod eqc;
pub mod estmerc;
pub mod etmerc;
pub mod geocent;
Expand Down Expand Up @@ -190,6 +191,7 @@ declare_projections! [
(laea),
(moll, wag4, wag5),
(geos),
(eqc),
];

///
Expand Down
3 changes: 2 additions & 1 deletion projections.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
(laea),
(moll, wag4, wag5),
(geos),
(eqc),
]
```

Expand All @@ -25,7 +26,7 @@
- [-] bonne
- [-] cass
- [-] cea
- [-] eqc
- [+] eqc
- [-] eqdc
- [-] eqearth
- [-] equi
Expand Down

0 comments on commit 85568fa

Please sign in to comment.