|
| 1 | +use geo::MultiPolygon; |
| 2 | +use geojson::{GeoJson, Geometry}; |
| 3 | + |
| 4 | +pub fn multi_polygon_from_str(s: &str) -> Result<MultiPolygon, geojson::Error> { |
| 5 | + let geojson: GeoJson = s.parse()?; |
| 6 | + Geometry::try_from(geojson).and_then(MultiPolygon::try_from) |
| 7 | +} |
| 8 | + |
| 9 | +pub fn point_from_str(s: &str) -> Result<geo::Point, geojson::Error> { |
| 10 | + let geojson: GeoJson = s.parse()?; |
| 11 | + Geometry::try_from(geojson).and_then(geo::Point::try_from) |
| 12 | +} |
| 13 | + |
| 14 | +#[cfg(test)] |
| 15 | +mod test { |
| 16 | + use crate::{ |
| 17 | + backend::geo_from_str::point_from_str, |
| 18 | + constants::geojson_strings::{bautzen, bautzen_ost, bautzen_west, geo_points, gorlitz}, |
| 19 | + }; |
| 20 | + use geo::Contains; |
| 21 | + |
| 22 | + use super::multi_polygon_from_str; |
| 23 | + |
| 24 | + #[test] |
| 25 | + fn test_non_matching_geo_types() { |
| 26 | + assert!(multi_polygon_from_str(geo_points::P1_BAUTZEN_OST).is_err()); |
| 27 | + assert!(point_from_str(bautzen::BAUTZEN).is_err()); |
| 28 | + assert!(multi_polygon_from_str("").is_err()); |
| 29 | + assert!(point_from_str("").is_err()); |
| 30 | + } |
| 31 | + |
| 32 | + #[test] |
| 33 | + fn test_matching_geo_types() { |
| 34 | + assert!(point_from_str(geo_points::P1_BAUTZEN_OST).is_ok()); |
| 35 | + assert!(point_from_str(geo_points::P2_BAUTZEN_OST).is_ok()); |
| 36 | + assert!(point_from_str(geo_points::P3_BAUTZEN_OST).is_ok()); |
| 37 | + assert!(point_from_str(geo_points::P4_BAUTZEN_OST).is_ok()); |
| 38 | + assert!(point_from_str(geo_points::P1_BAUTZEN_WEST).is_ok()); |
| 39 | + assert!(point_from_str(geo_points::P2_BAUTZEN_WEST).is_ok()); |
| 40 | + assert!(point_from_str(geo_points::P3_BAUTZEN_WEST).is_ok()); |
| 41 | + assert!(point_from_str(geo_points::P4_BAUTZEN_WEST).is_ok()); |
| 42 | + assert!(point_from_str(geo_points::P1_GORLITZ).is_ok()); |
| 43 | + assert!(point_from_str(geo_points::P2_GORLITZ).is_ok()); |
| 44 | + assert!(point_from_str(geo_points::P3_GORLITZ).is_ok()); |
| 45 | + assert!(point_from_str(geo_points::P4_GORLITZ).is_ok()); |
| 46 | + assert!(point_from_str(geo_points::P1_OUTSIDE).is_ok()); |
| 47 | + assert!(point_from_str(geo_points::P2_OUTSIDE).is_ok()); |
| 48 | + assert!(point_from_str(geo_points::P3_OUTSIDE).is_ok()); |
| 49 | + assert!(point_from_str(geo_points::P4_OUTSIDE).is_ok()); |
| 50 | + |
| 51 | + assert!(multi_polygon_from_str(bautzen::BAUTZEN).is_ok()); |
| 52 | + assert!(multi_polygon_from_str(bautzen_ost::BAUTZEN_OST).is_ok()); |
| 53 | + assert!(multi_polygon_from_str(bautzen_west::BAUTZEN_WEST).is_ok()); |
| 54 | + assert!(multi_polygon_from_str(gorlitz::GORLITZ).is_ok()); |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_points_in_correct_zones() { |
| 59 | + let test_points = geo_points::TestPoints::new(); |
| 60 | + let bautzen = multi_polygon_from_str(bautzen::BAUTZEN).unwrap(); |
| 61 | + let bautzen_ost = multi_polygon_from_str(bautzen_ost::BAUTZEN_OST).unwrap(); |
| 62 | + let bautzen_west = multi_polygon_from_str(bautzen_west::BAUTZEN_WEST).unwrap(); |
| 63 | + let gorlitz = multi_polygon_from_str(gorlitz::GORLITZ).unwrap(); |
| 64 | + for p in test_points.bautzen_ost { |
| 65 | + assert!(bautzen.contains(&p)); |
| 66 | + assert!(bautzen_ost.contains(&p)); |
| 67 | + assert!(!bautzen_west.contains(&p)); |
| 68 | + assert!(!gorlitz.contains(&p)); |
| 69 | + } |
| 70 | + for p in test_points.bautzen_west { |
| 71 | + assert!(bautzen.contains(&p)); |
| 72 | + assert!(!bautzen_ost.contains(&p)); |
| 73 | + assert!(bautzen_west.contains(&p)); |
| 74 | + assert!(!gorlitz.contains(&p)); |
| 75 | + } |
| 76 | + for p in test_points.gorlitz { |
| 77 | + assert!(!bautzen.contains(&p)); |
| 78 | + assert!(!bautzen_ost.contains(&p)); |
| 79 | + assert!(!bautzen_west.contains(&p)); |
| 80 | + assert!(gorlitz.contains(&p)); |
| 81 | + } |
| 82 | + for p in test_points.outside { |
| 83 | + assert!(!bautzen.contains(&p)); |
| 84 | + assert!(!bautzen_ost.contains(&p)); |
| 85 | + assert!(!bautzen_west.contains(&p)); |
| 86 | + assert!(!gorlitz.contains(&p)); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments