-
Notifications
You must be signed in to change notification settings - Fork 59
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,42 @@ where | |
} | ||
} | ||
|
||
#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))] | ||
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 | ||
|
@@ -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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 |
||
} | ||
} | ||
} | ||
|
@@ -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> | ||
|
@@ -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] | ||
|
@@ -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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
Looks like it was originally added for quick_collection in 12c4f18 but I don't see any effect:
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.
There was a problem hiding this comment.
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