Skip to content

Commit

Permalink
Merge #64
Browse files Browse the repository at this point in the history
64: Support 'POINT EMPTY' conversion to geo_types r=michaelkirk a=rmanoka

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

closes  #61 

Co-authored-by: Rajsekar Manokaran <rajsekar@gmail.com>
  • Loading branch information
bors[bot] and rmanoka committed Mar 2, 2021
2 parents 49a3a81 + 4ed9cbd commit aa202b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
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
13 changes: 10 additions & 3 deletions 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() {
geo_types::Point::try_from(g)?.into()
} else {
geo_types::MultiPoint(vec![]).into()
}
}
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 Expand Up @@ -358,8 +365,8 @@ mod tests {

#[test]
fn convert_empty_point() {
let point = Point(None).as_item();
let res: Result<geo_types::Geometry<f64>, Error> = point.try_into();
let point = Point(None);
let res: Result<geo_types::Point<f64>, Error> = point.try_into();
assert!(res.is_err());
}

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

0 comments on commit aa202b3

Please sign in to comment.