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

Support 'POINT EMPTY' conversion to geo_types #64

Merged
merged 4 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Unreleased

* Add new entries here
* Support `POINT EMPTY` in conversion to `geo_types`.
Converts to `MultiPoint([])`.
* <https://github.com/georust/wkt/pull/64>

## 0.9.1

Expand Down
4 changes: 2 additions & 2 deletions benches/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ fn criterion_benchmark(c: &mut criterion::Criterion) {
c.bench_function("parse small", |bencher| {
let s = include_str!("./small.wkt");
bencher.iter(|| {
let _ = wkt::Wkt::from_str(s).unwrap();
let _ = wkt::Wkt::<f64>::from_str(s).unwrap();
});
});

c.bench_function("parse big", |bencher| {
let s = include_str!("./big.wkt");
bencher.iter(|| {
let _ = wkt::Wkt::from_str(s).unwrap();
let _ = wkt::Wkt::<f64>::from_str(s).unwrap();
});
});
}
Expand Down
9 changes: 8 additions & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,14 @@ where

fn try_from(geometry: Geometry<T>) -> Result<Self, Self::Error> {
Ok(match geometry {
Geometry::Point(g) => geo_types::Geometry::Point(g.try_into()?),
Geometry::Point(g) => {
// Special case as `geo::Point` can't be empty
if g.0.is_some() {
Copy link
Member

Choose a reason for hiding this comment

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

Very minor thing, but we should be able to write this in an infallible way, like:

 if let Some(c) = g.0 {
    geo_types::Geometry::Point(geo_types::Point(c.into()))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to allow any other error handling / extensions we might add in the future here.

geo_types::Geometry::Point(g.try_into()?)
} else {
geo_types::Geometry::MultiPoint(geo_types::MultiPoint(vec![]))
}
}
Geometry::LineString(g) => geo_types::Geometry::LineString(g.into()),
Geometry::Polygon(g) => geo_types::Geometry::Polygon(g.into()),
Geometry::MultiLineString(g) => geo_types::Geometry::MultiLineString(g.into()),
Expand Down
18 changes: 10 additions & 8 deletions src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,17 @@ where
use serde::Deserialize;
Wkt::deserialize(deserializer).and_then(|wkt: Wkt<T>| {
use std::convert::TryFrom;
geo_types::Point::try_from(wkt)
.map(|p| Some(p))
.or_else(|e| {
if let crate::conversion::Error::PointConversionError = e {
// map a WKT: 'POINT EMPTY' to an `Option<geo_types::Point>::None`
return Ok(None);
geo_types::Geometry::try_from(wkt)
.map_err(D::Error::custom)
.and_then(|geom| {
use geo_types::Geometry::*;
match geom {
Point(p) => Ok(Some(p)),
MultiPoint(mp) if mp.0.len() == 0 => Ok(None),
_ => geo_types::Point::try_from(geom)
.map(Some)
.map_err(D::Error::custom),
}

Err(D::Error::custom(e))
})
})
}
Expand Down