From 24ace6bee1bb5980f1381c881e76171896b14a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20H=C3=BCgel?= Date: Thu, 3 Sep 2020 13:56:51 +0100 Subject: [PATCH] Add remaining tests for new conversions --- src/conversion/from_geo_types.rs | 54 +++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/conversion/from_geo_types.rs b/src/conversion/from_geo_types.rs index 0e9df050..ecb6587e 100644 --- a/src/conversion/from_geo_types.rs +++ b/src/conversion/from_geo_types.rs @@ -252,7 +252,8 @@ mod tests { use crate::{Geometry, Value}; use geo_types; use geo_types::{ - GeometryCollection, LineString, Line, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, + Coordinate, GeometryCollection, Line, LineString, MultiLineString, MultiPoint, + MultiPolygon, Point, Polygon, Rect, Triangle, }; #[test] @@ -334,6 +335,57 @@ mod tests { } } + #[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);