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

Implement geo_traits for geojson types. #245

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ edition = "2018"

[features]
default = ["geo-types"]
geo-traits = ["dep:geo-traits", "dep:bytemuck"]
Copy link
Member

Choose a reason for hiding this comment

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

How do you feel about making this a default dependency? I wouldn't imagine it would add too much to compilation time?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fine by me! As long as we keep it optional

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure who's driving this PR at this point, but it seems like this is still outstanding.


[dependencies]
serde = { version="~1.0", features = ["derive"] }
serde = { version = "~1.0", features = ["derive"] }
serde_json = "~1.0"
geo-types = { version = "0.7.13", features = ["serde"], optional = true }
geo-traits = { version = "0.2.0", optional = true }
bytemuck = { version = "1", features = ["derive"], optional = true }
thiserror = "1.0.20"
log = "0.4.17"

Expand All @@ -39,4 +42,3 @@ harness = false
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

47 changes: 47 additions & 0 deletions src/geo_traits_impl/coord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use super::PointType;
use geo_traits::Dimensions;

impl geo_traits::CoordTrait for PointType {
type T = f64;

fn dim(&self) -> Dimensions {
match self.0.len() {
0 | 1 => panic!("Position must have at least 2 dimensions"),
Copy link
Member

Choose a reason for hiding this comment

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

Consider: { "type": "Point", "coordinates": [] } right?

Granted, to be valid, a geojson MUST have at least two coordinates.

But this library is used to parse input that may be invalid, and I don't think we should be panic'ing in release builds for invalid input. I think this will surprise people in a bad way.

Instead, returning either Dimensions::Unknown(0) or Dimensions::Xy could be reasonable. Or dim could return an Option.

Copy link
Member

Choose a reason for hiding this comment

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

Instead, returning either Dimensions::Unknown(0) or Dimensions::Xy could be reasonable. Or dim could return an Option.

We talked about this above: #245 (comment), #245 (comment), #245 (comment).

And see also georust/geo#1263 (comment).

I'd personally choose a default of Dimensions::Xy. I don't think Dimensions::Unknown(0) should ever be used.

2 => Dimensions::Xy,
3 => Dimensions::Xyz,
_ => Dimensions::Unknown(self.0.len()),
}
}

fn x(&self) -> Self::T {
self.0[0]
}

fn y(&self) -> Self::T {
self.0[1]
}

fn nth_or_panic(&self, n: usize) -> Self::T {
self.0[n]
}
}

impl geo_traits::CoordTrait for &PointType {
type T = f64;

fn dim(&self) -> Dimensions {
PointType::dim(self)
}

fn x(&self) -> Self::T {
PointType::x(self)
}

fn y(&self) -> Self::T {
PointType::y(self)
}

fn nth_or_panic(&self, n: usize) -> Self::T {
PointType::nth_or_panic(self, n)
}
}
Loading
Loading