Skip to content

Commit

Permalink
Output coordinates as f64 instead of u32
Browse files Browse the repository at this point in the history
Change in api to allow better interoperability with other geo crates.
  • Loading branch information
BezBIS committed Aug 28, 2024
1 parent 3a74d30 commit 19f4739
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 68 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gridish"
version = "0.1.2"
version = "1.0.0"
edition = "2021"
description = "Library for working with British and Irish national grid strings."
authors = ["Berwyn Powell"]
Expand All @@ -12,11 +12,11 @@ tetrads = []

[dependencies]
geo-types = "0.7.13"
serde = { version = "1", optional = true }
serde = { version = "1.0", optional = true }

[dev-dependencies]
criterion = "0.5.1"
serde_json = "1"
serde_json = "1.0"

[[bench]]
name = "parsing"
Expand Down
6 changes: 6 additions & 0 deletions src/coordinates/metres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ impl Into<u32> for Metres {
}
}

impl Into<f64> for Metres {
fn into(self) -> f64 {
f64::from(self.0)
}
}

#[cfg(test)]
mod test {
use crate::constants::_500KM;
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//! assert_eq!("SO84".to_string(), gridref_10k.to_string());
//!
//! // Get the eastings / northings at the gridref's south west corner
//! assert_eq!(gridref_100m.sw(), coord! {x: 389_200, y: 243_700 }.into());
//! assert_eq!(gridref_10k.sw(), coord! {x: 380_000, y: 240_000 }.into());
//! assert_eq!(gridref_100m.sw(), coord! {x: 389_200.0, y: 243_700.0 }.into());
//! assert_eq!(gridref_10k.sw(), coord! {x: 380_000.0, y: 240_000.0 }.into());
//! ```
//!
//! ## Features
Expand All @@ -33,7 +33,7 @@
//! let gridref_2k: OSGB = "SN24R".parse().unwrap();
//!
//! // Get the eastings / northings at the gridref's south west corner
//! assert_eq!(gridref_2k.sw(), coord! {x: 226_000, y: 242_000 }.into());
//! assert_eq!(gridref_2k.sw(), coord! {x: 226_000.0, y: 242_000.0 }.into());
//! # }
//! ```

Expand Down
58 changes: 29 additions & 29 deletions src/osgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl OSGB {
///
/// let gridref: OSGB = "SO892437".parse().unwrap();
///
/// assert_eq!(gridref.sw(), coord! {x: 389_200, y: 243_700 }.into());
/// assert_eq!(gridref.sw(), coord! {x: 389_200.0, y: 243_700.0 }.into());
/// ```
pub fn sw(&self) -> Point<u32> {
Point::new(self.eastings(), self.northings())
pub fn sw(&self) -> Point {
Point::new(self.eastings() as f64, self.northings() as f64)
}

/// Returns the point at the osgb's
Expand All @@ -121,12 +121,12 @@ impl OSGB {
///
/// let gridref: OSGB = "SO892437".parse().unwrap();
///
/// assert_eq!(gridref.nw(), coord! {x: 389_200, y: 243_800 }.into());
/// assert_eq!(gridref.nw(), coord! {x: 389_200.0, y: 243_800.0 }.into());
/// ```
pub fn nw(&self) -> Point<u32> {
pub fn nw(&self) -> Point {
Point::new(
self.eastings(),
self.northings() + self.point.precision().metres(),
self.eastings() as f64,
(self.northings() + self.point.precision().metres()) as f64,
)
}

Expand All @@ -140,12 +140,12 @@ impl OSGB {
///
/// let gridref: OSGB = "SO892437".parse().unwrap();
///
/// assert_eq!(gridref.ne(), coord! {x: 389_300, y: 243_800 }.into());
/// assert_eq!(gridref.ne(), coord! {x: 389_300.0, y: 243_800.0 }.into());
/// ```
pub fn ne(&self) -> Point<u32> {
pub fn ne(&self) -> Point {
Point::new(
self.eastings() + self.point.precision().metres(),
self.northings() + self.point.precision().metres(),
(self.eastings() + self.point.precision().metres()) as f64,
(self.northings() + self.point.precision().metres()) as f64,
)
}

Expand All @@ -159,12 +159,12 @@ impl OSGB {
///
/// let gridref: OSGB = "SO892437".parse().unwrap();
///
/// assert_eq!(gridref.se(), coord! {x: 389_300, y: 243_700 }.into());
/// assert_eq!(gridref.se(), coord! {x: 389_300.0, y: 243_700.0 }.into());
/// ```
pub fn se(&self) -> Point<u32> {
pub fn se(&self) -> Point {
Point::new(
self.eastings() + self.point.precision().metres(),
self.northings(),
(self.eastings() + self.point.precision().metres()) as f64,
self.northings() as f64,
)
}

Expand All @@ -178,12 +178,12 @@ impl OSGB {
///
/// let gridref: OSGB = "SO892437".parse().unwrap();
///
/// assert_eq!(gridref.centre(), coord! {x: 389_250, y: 243_750 }.into());
/// assert_eq!(gridref.centre(), coord! {x: 389_250.0, y: 243_750.0 }.into());
/// ```
pub fn centre(&self) -> Point<u32> {
pub fn centre(&self) -> Point {
Point::new(
self.eastings() + (self.point.precision().metres() / 2),
self.northings() + (self.point.precision().metres() / 2),
self.eastings() as f64 + (self.point.precision().metres() as f64 / 2.0),
self.northings() as f64 + (self.point.precision().metres() as f64 / 2.0),
)
}

Expand All @@ -201,17 +201,17 @@ impl OSGB {
/// Polygon::new(
/// LineString::from(
/// vec![
/// Point::new(389_200, 243_700),
/// Point::new(389_200, 243_800),
/// Point::new(389_300, 243_800),
/// Point::new(389_300, 243_700)
/// Point::new(389_200.0, 243_700.0),
/// Point::new(389_200.0, 243_800.0),
/// Point::new(389_300.0, 243_800.0),
/// Point::new(389_300.0, 243_700.0)
/// ]
/// ),
/// vec![]
/// )
/// );
/// ```
pub fn perimeter(&self) -> Polygon<u32> {
pub fn perimeter(&self) -> Polygon {
Polygon::new(
LineString::from(vec![self.sw(), self.nw(), self.ne(), self.se()]),
vec![],
Expand Down Expand Up @@ -295,11 +295,11 @@ mod test {
let ne = osgb.ne();
let se = osgb.se();

assert_eq!(sw, Point::new(0, 0));
assert_eq!(nw, Point::new(0, 100));
assert_eq!(ne, Point::new(100, 100));
assert_eq!(se, Point::new(100, 0));
assert_eq!(osgb.centre(), Point::new(50, 50));
assert_eq!(sw, Point::new(0.0, 0.0));
assert_eq!(nw, Point::new(0.0, 100.0));
assert_eq!(ne, Point::new(100.0, 100.0));
assert_eq!(se, Point::new(100.0, 0.0));
assert_eq!(osgb.centre(), Point::new(50.0, 50.0));
assert_eq!(
osgb.perimeter(),
Polygon::new(LineString::from(vec![sw, nw, ne, se]), vec![])
Expand Down
57 changes: 28 additions & 29 deletions src/osi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl OSI {

/// Returns the point at the OSI's
/// 'South West' corner - its origin.
/// Recalculates the grid reference to a new precision.
///
/// # Example
/// ```
Expand All @@ -77,9 +76,9 @@ impl OSI {
///
/// let gridref: OSI = "O892437".parse().unwrap();
///
/// assert_eq!(gridref.sw(), coord! {x: 389_200, y: 243_700 }.into());
/// assert_eq!(gridref.sw(), coord! {x: 389_200.0, y: 243_700.0 }.into());
/// ```
pub fn sw(&self) -> Point<u32> {
pub fn sw(&self) -> Point {
Point::new(self.point.eastings().into(), self.point.northings().into())
}

Expand All @@ -93,12 +92,12 @@ impl OSI {
///
/// let gridref: OSI = "O892437".parse().unwrap();
///
/// assert_eq!(gridref.nw(), coord! {x: 389_200, y: 243_800 }.into());
/// assert_eq!(gridref.nw(), coord! {x: 389_200.0, y: 243_800.0 }.into());
/// ```
pub fn nw(&self) -> Point<u32> {
pub fn nw(&self) -> Point {
Point::new(
self.point.eastings().inner(),
self.point.northings().inner() + self.point.precision().metres(),
self.point.eastings().inner() as f64,
(self.point.northings().inner() + self.point.precision().metres()) as f64,
)
}

Expand All @@ -112,12 +111,12 @@ impl OSI {
///
/// let gridref: OSI = "O892437".parse().unwrap();
///
/// assert_eq!(gridref.ne(), coord! {x: 389_300, y: 243_800 }.into());
/// assert_eq!(gridref.ne(), coord! {x: 389_300.0, y: 243_800.0 }.into());
/// ```
pub fn ne(&self) -> Point<u32> {
pub fn ne(&self) -> Point {
Point::new(
self.point.eastings().inner() + self.point.precision().metres(),
self.point.northings().inner() + self.point.precision().metres(),
(self.point.eastings().inner() + self.point.precision().metres()) as f64,
(self.point.northings().inner() + self.point.precision().metres()) as f64,
)
}

Expand All @@ -131,12 +130,12 @@ impl OSI {
///
/// let gridref: OSI = "O892437".parse().unwrap();
///
/// assert_eq!(gridref.se(), coord! {x: 389_300, y: 243_700 }.into());
/// assert_eq!(gridref.se(), coord! {x: 389_300.0, y: 243_700.0 }.into());
/// ```
pub fn se(&self) -> Point<u32> {
pub fn se(&self) -> Point {
Point::new(
self.point.eastings().inner() + self.point.precision().metres(),
self.point.northings().inner(),
(self.point.eastings().inner() + self.point.precision().metres()) as f64,
self.point.northings().inner() as f64,
)
}

Expand All @@ -150,12 +149,12 @@ impl OSI {
///
/// let gridref: OSI = "O892437".parse().unwrap();
///
/// assert_eq!(gridref.centre(), coord! {x: 389_250, y: 243_750 }.into());
/// assert_eq!(gridref.centre(), coord! {x: 389_250.0, y: 243_750.0 }.into());
/// ```
pub fn centre(&self) -> Point<u32> {
pub fn centre(&self) -> Point {
Point::new(
self.point.eastings().inner() + (self.point.precision().metres() / 2),
self.point.northings().inner() + (self.point.precision().metres() / 2),
self.point.eastings().inner() as f64 + (self.point.precision().metres() as f64 / 2.0),
self.point.northings().inner() as f64 + (self.point.precision().metres() as f64 / 2.0),
)
}

Expand All @@ -173,17 +172,17 @@ impl OSI {
/// Polygon::new(
/// LineString::from(
/// vec![
/// Point::new(389_200, 243_700),
/// Point::new(389_200, 243_800),
/// Point::new(389_300, 243_800),
/// Point::new(389_300, 243_700)
/// Point::new(389_200.0, 243_700.0),
/// Point::new(389_200.0, 243_800.0),
/// Point::new(389_300.0, 243_800.0),
/// Point::new(389_300.0, 243_700.0)
/// ]
/// ),
/// vec![]
/// )
/// );
/// ```
pub fn perimeter(&self) -> Polygon<u32> {
pub fn perimeter(&self) -> Polygon {
Polygon::new(
LineString::from(vec![self.sw(), self.nw(), self.ne(), self.se()]),
vec![],
Expand Down Expand Up @@ -235,11 +234,11 @@ mod test {
let ne = osi.ne();
let se = osi.se();

assert_eq!(sw, Point::new(0, 0));
assert_eq!(nw, Point::new(0, 100));
assert_eq!(ne, Point::new(100, 100));
assert_eq!(se, Point::new(100, 0));
assert_eq!(osi.centre(), Point::new(50, 50));
assert_eq!(sw, Point::new(0.0, 0.0));
assert_eq!(nw, Point::new(0.0, 100.0));
assert_eq!(ne, Point::new(100.0, 100.0));
assert_eq!(se, Point::new(100.0, 0.0));
assert_eq!(osi.centre(), Point::new(50.0, 50.0));
assert_eq!(
osi.perimeter(),
Polygon::new(LineString::from(vec![sw, nw, ne, se]), vec![])
Expand Down
4 changes: 2 additions & 2 deletions tests/api/osgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ fn parses_valid_strings() {
for item in data {
let grid: OSGB = item.input_string.parse().unwrap();

assert_eq!(item.eastings, grid.sw().x());
assert_eq!(item.northings, grid.sw().y());
assert_eq!(item.eastings, grid.sw().x() as u32);
assert_eq!(item.northings, grid.sw().y() as u32);
assert_eq!(item.precision, grid.precision());
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/api/osi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ fn parses_valid_strings() {
for item in data {
let grid: OSI = item.input_string.parse().unwrap();

assert_eq!(item.eastings, grid.sw().x());
assert_eq!(item.northings, grid.sw().y());
assert_eq!(item.eastings, grid.sw().x() as u32);
assert_eq!(item.northings, grid.sw().y() as u32);
assert_eq!(item.precision, grid.precision());
}
}
Expand Down

0 comments on commit 19f4739

Please sign in to comment.