Skip to content

Commit

Permalink
Fix multi-dimension coord support
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Mar 15, 2022
1 parent 85b0b49 commit b0a42e6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
38 changes: 33 additions & 5 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{coord, CoordNum, Point};
use crate::{coord, CoordNum, Measure, Point, ZCoord};
use std::fmt::Debug;

#[cfg(any(feature = "approx", test))]
Expand All @@ -8,12 +8,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NoValue;

pub trait Measure: Default + Copy + PartialEq + Debug {}
pub trait ZCoord: Default + Copy + PartialEq + Debug {}
pub trait Srid: Default + Copy + PartialEq + Debug {}

impl<Z: Default + Copy + PartialEq + Debug> ZCoord for Z {}
impl<M: Default + Copy + PartialEq + Debug> Measure for M {}
impl<S: Default + Copy + PartialEq + Debug> Srid for S {}

/// A lightweight struct used to store coordinates on the 2-dimensional
Expand Down Expand Up @@ -376,3 +372,35 @@ where
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_coordinates() {
let p = coord! { x: 1.0, y: 2.0 };
assert_relative_eq!(p.x, 1.0);
assert_relative_eq!(p.y, 2.0);
assert_eq!(p.z, NoValue);
assert_eq!(p.m, NoValue);

let p = coord! { x: 1.0, y: 2.0, z: 3.0 };
assert_relative_eq!(p.x, 1.0);
assert_relative_eq!(p.y, 2.0);
assert_relative_eq!(p.z, 3.0);
assert_eq!(p.m, NoValue);

let p = coord! { x: 1.0, y: 2.0, m: 4_u8 };
assert_relative_eq!(p.x, 1.0);
assert_relative_eq!(p.y, 2.0);
assert_eq!(p.z, NoValue);
assert_eq!(p.m, 4_u8);

let p = coord! { x: 1_i32, y: 2_i32, z: 3_i32, m: 4.0_f64 };
assert_eq!(p.x, 1);
assert_eq!(p.y, 2);
assert_eq!(p.z, 3);
assert_relative_eq!(p.m, 4.0);
}
}
7 changes: 6 additions & 1 deletion geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ pub trait CoordNum: CoordinateType + Debug {}
#[allow(deprecated)]
impl<T: CoordinateType + Debug> CoordNum for T {}

pub trait Measure: Default + Copy + PartialEq + Debug {}
pub trait ZCoord: Default + Copy + PartialEq + Debug {}
impl<Z: Default + Copy + PartialEq + Debug> ZCoord for Z {}
impl<M: Default + Copy + PartialEq + Debug> Measure for M {}

pub trait CoordFloat: CoordNum + Float {}
impl<T: CoordNum + Float> CoordFloat for T {}

mod coordinate;
pub use crate::coordinate::{Coordinate, NoValue};
pub use crate::coordinate::{Coordinate, CoordinateM, CoordinateZ, CoordinateZM, NoValue};

mod point;
pub use crate::point::Point;
Expand Down
10 changes: 5 additions & 5 deletions geo-types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ macro_rules! coord {
)
};
(x: $x:expr, y: $y:expr, z: $z:expr $(,)* ) => {
$crate::Coordinate::new($x, $y, $z, $crate::NoValue::default())
$crate::CoordinateZ::new($x, $y, $z, $crate::NoValue::default())
};
(x: $x:expr, y: $y:expr, m: $m:expr, $(,)* ) => {
$crate::Coordinate::new($x, $y, $crate::NoValue::default(), $m)
(x: $x:expr, y: $y:expr, m: $m:expr $(,)* ) => {
$crate::CoordinateM::new($x, $y, $crate::NoValue::default(), $m)
};
(x: $x:expr, y: $y:expr, z: $z:expr, m: $m:expr, $(,)* ) => {
$crate::Coordinate::new($x, $y, $z, $m)
(x: $x:expr, y: $y:expr, z: $z:expr, m: $m:expr $(,)* ) => {
$crate::CoordinateZM::new($x, $y, $z, $m)
};
}

Expand Down

0 comments on commit b0a42e6

Please sign in to comment.