Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept: Allow custom earth radii #1220

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions geo/src/algorithm/rhumb/distance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{CoordFloat, Point, MEAN_EARTH_RADIUS};
use crate::{earth_radius::{MeanEarthRadius, Iugg}, CoordFloat, Point};
use num_traits::FromPrimitive;

use super::RhumbCalculations;
Expand All @@ -7,7 +7,7 @@ use super::RhumbCalculations;
///
/// [rhumb line]: https://en.wikipedia.org/wiki/Rhumb_line
///
/// *Note*: this implementation uses a mean earth radius of 6371.088 km, based on the [recommendation of
/// *Note*: this implementation defaults to a mean earth radius of 6371.088 km, based on the [recommendation of
/// the IUGG](ftp://athena.fsv.cvut.cz/ZFG/grs80-Moritz.pdf)
pub trait RhumbDistance<T, Rhs = Self> {
/// Determine the distance between along the [rhumb line] between two geometries.
Expand Down Expand Up @@ -37,16 +37,20 @@ pub trait RhumbDistance<T, Rhs = Self> {
/// ```
///
/// [rhumb line]: https://en.wikipedia.org/wiki/Rhumb_line
fn rhumb_distance(&self, rhs: &Rhs) -> T;
fn rhumb_distance(&self, rhs: &Rhs) -> T {
self.rhumb_distance_with_radius::<Iugg>(rhs)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly rust doesn't allow us to do something like this:

fn rhumb_distance<Radius: MeanEarthRadius = Iugg>(&self, rhs: &Rhs) -> T {

}

fn rhumb_distance_with_radius<Radius: MeanEarthRadius>(&self, rhs: &Rhs) -> T;
}

impl<T> RhumbDistance<T, Point<T>> for Point<T>
where
T: CoordFloat + FromPrimitive,
{
fn rhumb_distance(&self, rhs: &Point<T>) -> T {
fn rhumb_distance_with_radius<Radius: MeanEarthRadius>(&self, rhs: &Point<T>) -> T {
let calculations = RhumbCalculations::new(self, rhs);
calculations.delta() * T::from(MEAN_EARTH_RADIUS).unwrap()
calculations.delta() * Radius::value()
}
}

Expand Down
57 changes: 57 additions & 0 deletions geo/src/earth_radius.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::CoordNum;
use num_traits::FromPrimitive;

/// Trait representing different mean earth radii
pub trait MeanEarthRadius {
const VALUE: f64;

fn value<T: CoordNum + FromPrimitive>() -> T {
T::from(Self::VALUE).unwrap()
}
}

/// Represents the WGS84 mean earth radius.
///
/// The WGS84 (World Geodetic System 1984) is a standard for use in cartography, geodesy, and satellite navigation.
/// The mean earth radius for WGS84 is approximately 6378137.0 meters.
pub struct Wgs84;

/// Represents the GRS80 mean earth radius.
///
/// The GRS80 (Geodetic Reference System 1980) is a geodetic reference system consisting of a global reference ellipsoid and a gravity field model.
/// The mean earth radius for GRS80 is approximately 6378137.0 meters.
pub struct Grs80;

/// Represents the mean earth radius as recommended by the IUGG.
///
/// The mean earth radius for this model is approximately 6371008.8 meters.
/// This value is recommended by the IUGG:
/// Moritz, H. (2000). Geodetic Reference System 1980. Journal of Geodesy, 74(1), 128–133. doi:10.1007/s001900050278
/// "Derived Geometric Constants: mean radius" (p133)
/// https://link.springer.com/article/10.1007/s001900050278
/// https://sci-hub.se/https://doi.org/10.1007/s001900050278
/// https://en.wikipedia.org/wiki/Earth_radius#Mean_radius
pub struct Iugg;

/// Represents a spherical mean earth radius.
///
/// This is a simplified model assuming the Earth is a perfect sphere.
/// The mean earth radius for a sphere is approximately 6371000.0 meters.
/// This value is commonly used in various scientific, educational, and engineering contexts for simplicity.
pub struct Sphere;

impl MeanEarthRadius for Wgs84 {
const VALUE: f64 = 6378137.0;
}

impl MeanEarthRadius for Grs80 {
const VALUE: f64 = 6378137.0;
}

impl MeanEarthRadius for Iugg {
const VALUE: f64 = 6371008.8;
}

impl MeanEarthRadius for Sphere {
const VALUE: f64 = 6371000.0;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not at all attached to these particular struct names or this particular set of options

4 changes: 4 additions & 0 deletions geo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,17 @@ mod utils;
use crate::kernels::{RobustKernel, SimpleKernel};
pub(crate) use geometry_cow::GeometryCow;

pub mod earth_radius;

#[cfg(test)]
#[macro_use]
extern crate approx;

#[macro_use]
extern crate log;

// TODO: remove below?

/// Mean radius of Earth in meters
/// This is the value recommended by the IUGG:
/// Moritz, H. (2000). Geodetic Reference System 1980. Journal of Geodesy, 74(1), 128–133. doi:10.1007/s001900050278
Expand Down
Loading