diff --git a/crates/data/src/georss.rs b/crates/data/src/georss.rs index e2148317a8..e6557b099f 100644 --- a/crates/data/src/georss.rs +++ b/crates/data/src/georss.rs @@ -4,7 +4,7 @@ //! points, lines and polygons layers with their title, link, description, date and id. //! //! The file is hostile: no DTD is read, the node count is capped, and items whose coordinates -//! can't be read are skipped and counted. +//! can't be read are skipped and counted; unreadable polygon holes are omitted and counted. use roxmltree::{Node, ParsingOptions}; use rustgis_geom::Coord; @@ -71,7 +71,8 @@ fn bbox(ll: Coord, ur: Coord) -> Option { } /// An item's place, `Ok(None)` when it has none, `Err` when it has one that can't be read. -fn place(item: &Node) -> std::result::Result, ()> { +/// Unreadable polygon holes are omitted and counted in `lost_holes`. +fn place(item: &Node, lost_holes: &mut usize) -> std::result::Result, ()> { let g = |name: &str| item.children().find(|c| c.is_element() && local(c) == name); let parse = |n: Node| lat_lons(&text_of(&n)).ok_or(()); if let Some(n) = g("point") { @@ -115,6 +116,8 @@ fn place(item: &Node) -> std::result::Result, ()> { .and_then(ring) { rings.push(r); + } else { + *lost_holes += 1; } } Ok(Some(Geometry::Polygon(vec![rings]))) @@ -170,9 +173,9 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> Dataset::new(format!("{name} lines"), GeometryType::Polyline, fields()), Dataset::new(format!("{name} polygons"), GeometryType::Polygon, fields()), ]; - let (mut skipped, mut without) = (0usize, 0usize); + let (mut skipped, mut without, mut lost_holes) = (0usize, 0usize, 0usize); for item in root.descendants().filter(|n| n.is_element() && matches!(local(n), "item" | "entry")) { - match place(&item) { + match place(&item, &mut lost_holes) { Ok(Some(g)) => { let i = match g.kind() { GeometryType::Polyline => 1, @@ -194,6 +197,14 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> if without > 0 { report.messages.push(format!("{without} items have no place and were left out")); } + if lost_holes > 0 { + let what = if lost_holes == 1 { + "1 hole in a polygon was unreadable and left out".to_string() + } else { + format!("{lost_holes} holes in polygons were unreadable and left out") + }; + report.messages.push(what); + } // What the file as a whole said goes with its first layer only. let out: Vec<(Dataset, ReadReport)> = layers .into_iter() @@ -268,4 +279,69 @@ mod tests { assert_eq!(lat_lons("1 2 3"), None); assert_eq!(lat_lons("nan 2"), None); } + + #[test] + fn unreadable_interior_ring_is_said() { + let feed = |hole: &str| { + format!( + r#" + +Zoning +Park + 45 -110 46 -110 46 -111 45 -110 + {hole} + +"# + ) + }; + + let lossy = read("lossy.rss", feed("bad coords").as_bytes()).unwrap(); + assert_eq!(lossy[0].0.features.len(), 1); + assert!(lossy[0].0.features[0].geometry.is_some()); + assert!( + lossy[0].1.messages.iter().any(|m| m.contains("1 hole in a polygon was unreadable and left out")), + "{:?}", + lossy[0].1.messages + ); + + let clean = read("clean.rss", feed("45.2 -110.2 45.8 -110.2 45.8 -110.8 45.2 -110.2").as_bytes()).unwrap(); + assert_eq!(clean[0].0.features.len(), 1); + assert!(!clean[0].1.messages.iter().any(|m| m.contains("hole")), "{:?}", clean[0].1.messages); + + let multi = r#" + +Zoning +Park + 45 -110 46 -110 46 -111 45 -110 + bad one + bad two + +"#; + let lossy_multi = read("multi.rss", multi.as_bytes()).unwrap(); + assert!( + lossy_multi[0].1.messages.iter().any(|m| m.contains("2 holes in polygons were unreadable and left out")), + "{:?}", + lossy_multi[0].1.messages + ); + + let mixed = r#" + +Zoning +Park + 45 -110 46 -110 46 -111 45 -110 + 45.2 -110.2 45.8 -110.2 45.8 -110.8 45.2 -110.2 + corrupt hole + +"#; + let mixed_lossy = read("mixed.rss", mixed.as_bytes()).unwrap(); + assert!( + mixed_lossy[0].1.messages.iter().any(|m| m.contains("1 hole in a polygon was unreadable and left out")), + "{:?}", + mixed_lossy[0].1.messages + ); + match &mixed_lossy[0].0.features[0].geometry { + Some(Geometry::Polygon(polys)) => assert_eq!(polys[0].len(), 2, "must keep exterior and valid interior ring"), + other => panic!("expected polygon, got {other:?}"), + } + } }