Skip to content

Commit 24ace6b

Browse files
committed
Add remaining tests for new conversions
1 parent 48b884d commit 24ace6b

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/conversion/from_geo_types.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ mod tests {
252252
use crate::{Geometry, Value};
253253
use geo_types;
254254
use geo_types::{
255-
GeometryCollection, LineString, Line, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
255+
Coordinate, GeometryCollection, Line, LineString, MultiLineString, MultiPoint,
256+
MultiPolygon, Point, Polygon, Rect, Triangle,
256257
};
257258

258259
#[test]
@@ -334,6 +335,57 @@ mod tests {
334335
}
335336
}
336337

338+
#[test]
339+
fn geo_triangle_conversion_test() {
340+
let c1 = Coordinate { x: 0., y: 0. };
341+
let c2 = Coordinate { x: 10., y: 20. };
342+
let c3 = Coordinate { x: 20., y: -10. };
343+
344+
let triangle = Triangle(c1, c2, c3);
345+
346+
let geojson_polygon = Value::from(&triangle);
347+
348+
// Geo-types Polygon construction introduces an extra vertex: let's check it!
349+
if let Value::Polygon(c) = geojson_polygon {
350+
assert_almost_eq!(c1.x as f64, c[0][0][0], 1e-6);
351+
assert_almost_eq!(c1.y as f64, c[0][0][1], 1e-6);
352+
assert_almost_eq!(c2.x as f64, c[0][1][0], 1e-6);
353+
assert_almost_eq!(c2.y as f64, c[0][1][1], 1e-6);
354+
assert_almost_eq!(c3.x as f64, c[0][2][0], 1e-6);
355+
assert_almost_eq!(c3.y as f64, c[0][2][1], 1e-6);
356+
assert_almost_eq!(c1.x as f64, c[0][3][0], 1e-6);
357+
assert_almost_eq!(c1.y as f64, c[0][3][1], 1e-6);
358+
} else {
359+
panic!("Not valid geometry {:?}", geojson_polygon);
360+
}
361+
}
362+
363+
#[test]
364+
fn geo_rect_conversion_test() {
365+
let c1 = Coordinate { x: 0., y: 0. };
366+
let c2 = Coordinate { x: 10., y: 20. };
367+
368+
let rect = Rect::new(c1, c2);
369+
370+
let geojson_polygon = Value::from(&rect);
371+
372+
// Geo-types Polygon construction introduces an extra vertex: let's check it!
373+
if let Value::Polygon(c) = geojson_polygon {
374+
assert_almost_eq!(c1.x as f64, c[0][0][0], 1e-6);
375+
assert_almost_eq!(c1.y as f64, c[0][0][1], 1e-6);
376+
assert_almost_eq!(c1.x as f64, c[0][1][0], 1e-6);
377+
assert_almost_eq!(c2.y as f64, c[0][1][1], 1e-6);
378+
assert_almost_eq!(c2.x as f64, c[0][2][0], 1e-6);
379+
assert_almost_eq!(c2.y as f64, c[0][2][1], 1e-6);
380+
assert_almost_eq!(c2.x as f64, c[0][3][0], 1e-6);
381+
assert_almost_eq!(c1.y as f64, c[0][3][1], 1e-6);
382+
assert_almost_eq!(c1.x as f64, c[0][4][0], 1e-6);
383+
assert_almost_eq!(c1.y as f64, c[0][4][1], 1e-6);
384+
} else {
385+
panic!("Not valid geometry {:?}", geojson_polygon);
386+
}
387+
}
388+
337389
#[test]
338390
fn geo_multi_line_string_conversion_test() {
339391
let p1 = Point::new(40.02f64, 116.34f64);

0 commit comments

Comments
 (0)