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

Add conversions from Line, Triangle, Rect and GeometryCollection #136

Merged
merged 4 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add more granular errors
* `GeoJsonUnknownType` has been split into `NotAFeature` and `EmptyType`
* Add additional Value context to errors where possible
* Add conversions from Geo-Types Line, Triangle, Rect and GeometryCollection

## 0.19.0

Expand Down
137 changes: 135 additions & 2 deletions src/conversion/from_geo_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@ where
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
Copy link
Member

Choose a reason for hiding this comment

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

Any idea what this line accomplishes?

It seems similar to this rust-lang/rust#43348 which produces output in the docs like this "Unix only" label:

Screen Shot 2020-09-03 at 9 47 09 AM

Looks like it was originally added for quick_collection in 12c4f18 but I don't see any effect:
Screen Shot 2020-09-03 at 9 49 58 AM

No need to change anything in this PR - I realize this was copied from the similar methods in this file, just curious if anyone has context to see if it's working as intended.

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 think it was my attempt at adding a marker to the docs to indicate that it's only available when the geo-types feature is activated, but that functionality has either changed slightly, or I screwed it up: https://stackoverflow.com/a/61417700/416626

impl<'a, T> From<&'a geo_types::Line<T>> for geometry::Value
where
T: Float,
{
fn from(line: &geo_types::Line<T>) -> Self {
let coords = create_from_line_type(line);

geometry::Value::LineString(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::Triangle<T>> for geometry::Value
where
T: Float,
{
fn from(triangle: &geo_types::Triangle<T>) -> Self {
let coords = create_from_triangle_type(triangle);

geometry::Value::Polygon(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::Rect<T>> for geometry::Value
where
T: Float,
{
fn from(rect: &geo_types::Rect<T>) -> Self {
let coords = create_from_rect_type(rect);

geometry::Value::Polygon(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::MultiLineString<T>> for geometry::Value
where
Expand Down Expand Up @@ -108,14 +144,17 @@ where
geo_types::Geometry::Point(ref point) => geometry::Value::from(point),
geo_types::Geometry::MultiPoint(ref multi_point) => geometry::Value::from(multi_point),
geo_types::Geometry::LineString(ref line_string) => geometry::Value::from(line_string),
geo_types::Geometry::Line(ref line) => geometry::Value::from(line),
geo_types::Geometry::Triangle(ref triangle) => geometry::Value::from(triangle),
geo_types::Geometry::Rect(ref rect) => geometry::Value::from(rect),
geo_types::Geometry::GeometryCollection(ref gc) => geometry::Value::from(gc),
geo_types::Geometry::MultiLineString(ref multi_line_string) => {
geometry::Value::from(multi_line_string)
}
geo_types::Geometry::Polygon(ref polygon) => geometry::Value::from(polygon),
geo_types::Geometry::MultiPolygon(ref multi_polygon) => {
geometry::Value::from(multi_polygon)
}
_ => panic!("GeometryCollection not allowed"),
Copy link
Member

Choose a reason for hiding this comment

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

🔥

}
}
}
Expand All @@ -140,6 +179,30 @@ where
.collect()
}

fn create_from_line_type<T>(line_string: &geo_types::Line<T>) -> LineStringType
where
T: Float,
{
vec![
create_point_type(&line_string.start_point()),
create_point_type(&line_string.end_point()),
]
}

fn create_from_triangle_type<T>(triangle: &geo_types::Triangle<T>) -> PolygonType
where
T: Float,
{
create_polygon_type(&triangle.to_polygon())
}

fn create_from_rect_type<T>(rect: &geo_types::Rect<T>) -> PolygonType
where
T: Float,
{
create_polygon_type(&rect.to_polygon())
}

fn create_multi_line_string_type<T>(
multi_line_string: &geo_types::MultiLineString<T>,
) -> Vec<LineStringType>
Expand Down Expand Up @@ -189,7 +252,8 @@ mod tests {
use crate::{Geometry, Value};
use geo_types;
use geo_types::{
GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
Coordinate, GeometryCollection, Line, LineString, MultiLineString, MultiPoint,
MultiPolygon, Point, Polygon, Rect, Triangle,
};

#[test]
Expand Down Expand Up @@ -253,6 +317,75 @@ mod tests {
}
}

#[test]
fn geo_line_conversion_test() {
let p1 = Point::new(40.02f64, 116.34f64);
let p2 = Point::new(13.02f64, 24.34f64);

let geo_line = Line::new(p1, p2);
let geojson_line_point = Value::from(&geo_line);

if let Value::LineString(c) = geojson_line_point {
assert_almost_eq!(p1.x(), c[0][0], 1e-6);
assert_almost_eq!(p1.y(), c[0][1], 1e-6);
assert_almost_eq!(p2.x(), c[1][0], 1e-6);
assert_almost_eq!(p2.y(), c[1][1], 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_line_point);
}
}

#[test]
fn geo_triangle_conversion_test() {
let c1 = Coordinate { x: 0., y: 0. };
let c2 = Coordinate { x: 10., y: 20. };
let c3 = Coordinate { x: 20., y: -10. };

let triangle = Triangle(c1, c2, c3);

let geojson_polygon = Value::from(&triangle);

// Geo-types Polygon construction introduces an extra vertex: let's check it!
if let Value::Polygon(c) = geojson_polygon {
assert_almost_eq!(c1.x as f64, c[0][0][0], 1e-6);
assert_almost_eq!(c1.y as f64, c[0][0][1], 1e-6);
assert_almost_eq!(c2.x as f64, c[0][1][0], 1e-6);
assert_almost_eq!(c2.y as f64, c[0][1][1], 1e-6);
assert_almost_eq!(c3.x as f64, c[0][2][0], 1e-6);
assert_almost_eq!(c3.y as f64, c[0][2][1], 1e-6);
assert_almost_eq!(c1.x as f64, c[0][3][0], 1e-6);
assert_almost_eq!(c1.y as f64, c[0][3][1], 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_polygon);
}
}

#[test]
fn geo_rect_conversion_test() {
let c1 = Coordinate { x: 0., y: 0. };
let c2 = Coordinate { x: 10., y: 20. };

let rect = Rect::new(c1, c2);

let geojson_polygon = Value::from(&rect);

// Geo-types Polygon construction introduces an extra vertex: let's check it!
if let Value::Polygon(c) = geojson_polygon {
assert_almost_eq!(c1.x as f64, c[0][0][0], 1e-6);
assert_almost_eq!(c1.y as f64, c[0][0][1], 1e-6);
assert_almost_eq!(c1.x as f64, c[0][1][0], 1e-6);
assert_almost_eq!(c2.y as f64, c[0][1][1], 1e-6);
assert_almost_eq!(c2.x as f64, c[0][2][0], 1e-6);
assert_almost_eq!(c2.y as f64, c[0][2][1], 1e-6);
assert_almost_eq!(c2.x as f64, c[0][3][0], 1e-6);
assert_almost_eq!(c1.y as f64, c[0][3][1], 1e-6);
assert_almost_eq!(c1.x as f64, c[0][4][0], 1e-6);
assert_almost_eq!(c1.y as f64, c[0][4][1], 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_polygon);
}
}

#[test]
fn geo_multi_line_string_conversion_test() {
let p1 = Point::new(40.02f64, 116.34f64);
Expand Down