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
3 changes: 3 additions & 0 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_json = "~1.0"
geo-types = { version = "0.7.13", features = ["serde"], optional = true }
geo-traits = { version = "0.1.0", optional = true }
Copy link
Member

Choose a reason for hiding this comment

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

@frewsxcv would you be able to update this pr to 0.2.0? Or otherwise I could make a new PR

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 won't be able to get to this for the next few days. Feel free to open a new pull request or push directly to this one

bytemuck = { version = "1", features = ["derive"], optional = true }
thiserror = "1.0.20"
log = "0.4.17"

Expand Down
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::{CoordTrait, 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_unchecked(&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_unchecked(&self, n: usize) -> Self::T {
PointType::nth_unchecked(self, n)
}
}
240 changes: 240 additions & 0 deletions src/geo_traits_impl/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
use super::{
GeometryCollectionType, LineStringType, MultiLineStringType, MultiPointType, MultiPolygonType,
PointType, PolygonType,
};
use bytemuck::TransparentWrapper;
use geo_traits::{
Dimensions, GeometryCollectionTrait, LineStringTrait, MultiLineStringTrait, MultiPointTrait,
MultiPolygonTrait, PointTrait, PolygonTrait, UnimplementedLine, UnimplementedRect,
UnimplementedTriangle,
};

impl geo_traits::GeometryTrait for crate::Value {
type T = f64;
type PointType<'a> = PointType;
type LineStringType<'a> = LineStringType;
type PolygonType<'a> = PolygonType;
type MultiPointType<'a> = MultiPointType;
type MultiLineStringType<'a> = MultiLineStringType;
type MultiPolygonType<'a> = MultiPolygonType;
type GeometryCollectionType<'a> = GeometryCollectionType;
type RectType<'a> = UnimplementedRect<Self::T>;
type TriangleType<'a> = UnimplementedTriangle<Self::T>;
type LineType<'a> = UnimplementedLine<Self::T>;

fn as_type(
&self,
) -> geo_traits::GeometryType<
'_,
Self::PointType<'_>,
Self::LineStringType<'_>,
Self::PolygonType<'_>,
Self::MultiPointType<'_>,
Self::MultiLineStringType<'_>,
Self::MultiPolygonType<'_>,
Self::GeometryCollectionType<'_>,
Self::RectType<'_>,
Self::TriangleType<'_>,
Self::LineType<'_>,
> {
match self {
crate::Value::Point(p) => geo_traits::GeometryType::Point(PointType::wrap_ref(p)),
crate::Value::LineString(ls) => {
geo_traits::GeometryType::LineString(LineStringType::wrap_ref(ls))
}
crate::Value::Polygon(p) => geo_traits::GeometryType::Polygon(PolygonType::wrap_ref(p)),
crate::Value::MultiPoint(mp) => {
geo_traits::GeometryType::MultiPoint(MultiPointType::wrap_ref(mp))
}
crate::Value::MultiLineString(mls) => {
geo_traits::GeometryType::MultiLineString(MultiLineStringType::wrap_ref(mls))
}
crate::Value::MultiPolygon(mp) => {
geo_traits::GeometryType::MultiPolygon(MultiPolygonType::wrap_ref(mp))
}
crate::Value::GeometryCollection(gc) => {
geo_traits::GeometryType::GeometryCollection(GeometryCollectionType::wrap_ref(gc))
}
}
}

fn dim(&self) -> Dimensions {
match self {
crate::Value::Point(ref p) => PointType::wrap_ref(p).dim(),
crate::Value::LineString(ref ls) => LineStringType::wrap_ref(ls).dim(),
crate::Value::Polygon(ref p) => PolygonType::wrap_ref(p).dim(),
crate::Value::MultiPoint(ref mp) => MultiPointType::wrap_ref(mp).dim(),
crate::Value::MultiLineString(ref mls) => MultiLineStringType::wrap_ref(mls).dim(),
crate::Value::MultiPolygon(ref mp) => MultiPolygonType::wrap_ref(mp).dim(),
crate::Value::GeometryCollection(ref gc) => GeometryCollectionType::wrap_ref(gc).dim(),
}
}
}

impl geo_traits::GeometryTrait for &crate::Value {
type T = f64;
type PointType<'b>
= PointType
where
Self: 'b;
type LineStringType<'b>
= LineStringType
where
Self: 'b;
type PolygonType<'b>
= PolygonType
where
Self: 'b;
type MultiPointType<'b>
= MultiPointType
where
Self: 'b;
type MultiLineStringType<'b>
= MultiLineStringType
where
Self: 'b;
type MultiPolygonType<'b>
= MultiPolygonType
where
Self: 'b;
type GeometryCollectionType<'b>
= GeometryCollectionType
where
Self: 'b;
type RectType<'b>
= UnimplementedRect<Self::T>
where
Self: 'b;
type TriangleType<'b>
= UnimplementedTriangle<Self::T>
where
Self: 'b;
type LineType<'b>
= UnimplementedLine<Self::T>
where
Self: 'b;

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

fn as_type(
&self,
) -> geo_traits::GeometryType<
'_,
Self::PointType<'_>,
Self::LineStringType<'_>,
Self::PolygonType<'_>,
Self::MultiPointType<'_>,
Self::MultiLineStringType<'_>,
Self::MultiPolygonType<'_>,
Self::GeometryCollectionType<'_>,
Self::RectType<'_>,
Self::TriangleType<'_>,
Self::LineType<'_>,
> {
crate::Value::as_type(self)
}
}

impl geo_traits::GeometryTrait for crate::Geometry {
type T = f64;
type PointType<'b> = PointType;
type LineStringType<'b> = LineStringType;
type PolygonType<'b> = PolygonType;
type MultiPointType<'b> = MultiPointType;
type MultiLineStringType<'b> = MultiLineStringType;
type MultiPolygonType<'b> = MultiPolygonType;
type GeometryCollectionType<'b> = GeometryCollectionType;
type RectType<'b> = UnimplementedRect<Self::T>;
type TriangleType<'b> = UnimplementedTriangle<Self::T>;
type LineType<'b> = UnimplementedLine<Self::T>;

fn dim(&self) -> Dimensions {
self.value.dim()
}

fn as_type(
&self,
) -> geo_traits::GeometryType<
'_,
Self::PointType<'_>,
Self::LineStringType<'_>,
Self::PolygonType<'_>,
Self::MultiPointType<'_>,
Self::MultiLineStringType<'_>,
Self::MultiPolygonType<'_>,
Self::GeometryCollectionType<'_>,
Self::RectType<'_>,
Self::TriangleType<'_>,
Self::LineType<'_>,
> {
self.value.as_type()
}
}

impl geo_traits::GeometryTrait for &crate::Geometry {
type T = f64;
type PointType<'b>
= PointType
where
Self: 'b;
type LineStringType<'b>
= LineStringType
where
Self: 'b;
type PolygonType<'b>
= PolygonType
where
Self: 'b;
type MultiPointType<'b>
= MultiPointType
where
Self: 'b;
type MultiLineStringType<'b>
= MultiLineStringType
where
Self: 'b;
type MultiPolygonType<'b>
= MultiPolygonType
where
Self: 'b;
type GeometryCollectionType<'b>
= GeometryCollectionType
where
Self: 'b;
type RectType<'b>
= UnimplementedRect<Self::T>
where
Self: 'b;
type TriangleType<'b>
= UnimplementedTriangle<Self::T>
where
Self: 'b;
type LineType<'b>
= UnimplementedLine<Self::T>
where
Self: 'b;

fn as_type(
&self,
) -> geo_traits::GeometryType<
'_,
Self::PointType<'_>,
Self::LineStringType<'_>,
Self::PolygonType<'_>,
Self::MultiPointType<'_>,
Self::MultiLineStringType<'_>,
Self::MultiPolygonType<'_>,
Self::GeometryCollectionType<'_>,
Self::RectType<'_>,
Self::TriangleType<'_>,
Self::LineType<'_>,
> {
crate::Geometry::as_type(self)
}

fn dim(&self) -> Dimensions {
crate::Geometry::dim(self)
}
}
62 changes: 62 additions & 0 deletions src/geo_traits_impl/geometry_collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use super::GeometryCollectionType;
use geo_traits::{Dimensions, GeometryTrait};

impl geo_traits::GeometryCollectionTrait for GeometryCollectionType {
type T = f64;
type GeometryType<'b>
= &'b crate::Geometry
where
Self: 'b;

fn dim(&self) -> Dimensions {
self.geometry(0).unwrap().dim()
frewsxcv marked this conversation as resolved.
Show resolved Hide resolved
}

fn geometries(
&self,
) -> impl DoubleEndedIterator + ExactSizeIterator<Item = Self::GeometryType<'_>> {
self.0.iter()
}

fn geometry(&self, i: usize) -> Option<Self::GeometryType<'_>> {
self.0.get(i)
}

unsafe fn geometry_unchecked(&self, i: usize) -> Self::GeometryType<'_> {
kylebarron marked this conversation as resolved.
Show resolved Hide resolved
self.0.get_unchecked(i)
}

fn num_geometries(&self) -> usize {
self.0.len()
}
}

impl<'a> geo_traits::GeometryCollectionTrait for &'a GeometryCollectionType {
type T = f64;
type GeometryType<'b>
= &'b crate::Geometry
where
Self: 'b;

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

fn geometries(
&self,
) -> impl DoubleEndedIterator + ExactSizeIterator<Item = Self::GeometryType<'_>> {
GeometryCollectionType::geometries(self)
}

fn geometry(&self, i: usize) -> Option<Self::GeometryType<'_>> {
GeometryCollectionType::geometry(self, i)
}

unsafe fn geometry_unchecked(&self, i: usize) -> Self::GeometryType<'_> {
GeometryCollectionType::geometry_unchecked(self, i)
}

fn num_geometries(&self) -> usize {
GeometryCollectionType::num_geometries(self)
}
}
Loading