From 646937600796616a82cdf6ab1608ae6dd6a232f7 Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 10 Nov 2026 02:16:59 +0800 Subject: [PATCH 1/5] fix(data): the GML reader says what it leaves out (#1620) Notices now cover lost parts inside a member, shapes the reader can't read (every GML 3.2.1 geometry, surface patch and curve segment, on their own feature type too), repeated properties and properties past the column cap, per feature type and in document order. --- crates/data/src/gml.rs | 338 +++++++++++++++++++++++++++++++---------- 1 file changed, 258 insertions(+), 80 deletions(-) diff --git a/crates/data/src/gml.rs b/crates/data/src/gml.rs index ba9edbde90..574bfea044 100644 --- a/crates/data/src/gml.rs +++ b/crates/data/src/gml.rs @@ -74,41 +74,53 @@ const GEOMETRIES: &[&str] = &[ "LinearRing", ]; -/// Whether a GML element under a property is a shape (read, or of a kind not read). GML's values -/// that aren't geometry are few: times, definitions, dictionaries and units, metadata, measures -/// and value objects. +/// Geometry elements this reader doesn't take as a feature's shape: the instantiable members of +/// the substitution groups of `gml:AbstractGeometry`, `gml:AbstractSurfacePatch` and +/// `gml:AbstractCurveSegment` in the GML 3.2.1 schema (geometryBasic0d1d.xsd, geometryAggregates.xsd, +/// geometryComplexes.xsd, geometryPrimitives.xsd at schemas.opengis.net/gml/3.2.1) that aren't in +/// [`GEOMETRIES`]. +const SHAPES_NOT_READ: &[&str] = &[ + "Arc", + "ArcByBulge", + "ArcByCenterPoint", + "ArcString", + "ArcStringByBulge", + "BSpline", + "Bezier", + "Circle", + "CircleByCenterPoint", + "Clothoid", + "Cone", + "CubicSpline", + "Cylinder", + "Geodesic", + "GeodesicString", + "LineStringSegment", + "OffsetCurve", + "PolygonPatch", + "Rectangle", + "Sphere", + "Triangle", + "CompositeCurve", + "CompositeSolid", + "CompositeSurface", + "GeometricComplex", + "MultiGeometry", + "MultiSolid", + "OrientableCurve", + "OrientableSurface", + "PolyhedralSurface", + "Ring", + "Shell", + "Solid", + "Tin", + "TriangulatedSurface", +]; + +/// Whether a GML element under a property is a shape (read, or of a kind not read). Any other GML +/// value (a time, a definition, a description, a measure…) is not. fn is_shape(name: &str) -> bool { - !(name.starts_with("Time") - || name.ends_with("Definition") - || name.ends_with("Dictionary") - || name.ends_with("Unit") - || name.ends_with("MetaData") - || matches!( - name, - "DefinitionCollection" - | "Null" - | "Boolean" - | "Category" - | "Count" - | "Quantity" - | "BooleanList" - | "CategoryList" - | "CountList" - | "QuantityList" - | "CategoryExtent" - | "CountExtent" - | "QuantityExtent" - | "CompositeValue" - | "ValueArray" - | "ValueExtent" - | "Measure" - | "Length" - | "Angle" - | "Scale" - | "Area" - | "Volume" - | "Speed" - )) + GEOMETRIES.contains(&name) || SHAPES_NOT_READ.contains(&name) } /// The coordinates under a node: `gml:pos`/`gml:posList` (with `srsDimension`), @@ -264,25 +276,34 @@ fn geometry(g: &Node, zm: &Cell, lost: &Cell) -> Option { } } -/// How many members of a multi-geometry give nothing to keep: a member that is only a reference -/// (`xlink:href`), or a part too short (a line of one point). 0 for other geometries. +/// How many parts of a multi-geometry give nothing to keep: a member that is only a reference +/// (`xlink:href`), or a part too short (a line of one point) or with unreadable coordinates. 0 for other geometries. fn parts_lost(g: &Node) -> usize { let kind = local(g); if !kind.starts_with("Multi") { return 0; } let scratch = Cell::new(false); - let gives = |part: &Node| { - part.descendants().any(|x| { - x.is_element() - && is_gml(&x) - && match (kind, local(&x)) { - ("MultiPoint", "Point") => coords(&x, &scratch).is_some_and(|c| !c.is_empty()), - ("MultiLineString" | "MultiCurve", "LineString" | "Curve") => coords(&x, &scratch).is_some_and(|c| c.len() >= 2), - ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch") => polygon(&x, &scratch, &Cell::new(0)).is_some(), - _ => false, - } - }) + let leaf = |x: &Node| { + x.is_element() + && is_gml(x) + && matches!( + (kind, local(x)), + ("MultiPoint", "Point") + | ("MultiLineString" | "MultiCurve", "LineString" | "Curve") + | ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch") + ) + }; + let gives = |x: &Node| match kind { + "MultiPoint" => coords(x, &scratch).is_some_and(|c| !c.is_empty()), + "MultiLineString" | "MultiCurve" => coords(x, &scratch).is_some_and(|c| c.len() >= 2), + _ => polygon(x, &scratch, &Cell::new(0)).is_some(), + }; + // A member lost whole (a reference, no part in it) counts once; one holding parts counts those + // it can't give, so a member with one part read and one not says the second. + let lost = |part: Node| { + let leaves: Vec = part.descendants().filter(&leaf).collect(); + if leaves.is_empty() { 1 } else { leaves.iter().filter(|x| !gives(x)).count() } }; // A member property holds one part (`curveMember`), a members property several (`curveMembers`). g.children() @@ -297,10 +318,32 @@ fn parts_lost(g: &Node) -> usize { Vec::new() } }) - .filter(|p| !gives(p)) - .count() + .map(lost) + .sum() } +/// The first few distinct names of what a notice counts; more are said as `…`. +#[derive(Default)] +struct Names(Vec, bool); + +impl Names { + fn add(&mut self, p: String) { + if !self.0.contains(&p) { + if self.0.len() < 3 { + self.0.push(p); + } else { + self.1 = true; + } + } + } + fn said(&self) -> String { + self.0.join(", ") + if self.1 { ", …" } else { "" } + } +} + +/// Most attribute columns of a feature type. +const MAX_COLUMNS: usize = 500; + /// A feature type being read: its attribute columns in order of appearance, and its rows. #[derive(Default)] struct Kind { @@ -311,9 +354,15 @@ struct Kind { /// Properties not read (a nested record, a time, a shape past the one kept): how many, and the /// first few names. unread: usize, - unread_names: Vec, - /// More distinct properties were left unread than are named. - unread_more: bool, + unread_names: Names, + /// Features whose shape couldn't be read. + skipped: usize, + /// Values of a property the feature already had (the last is kept), and values of properties + /// past the column cap: how many, and the first few names. + repeated: usize, + repeated_names: Names, + capped: usize, + capped_names: Names, /// Members of multi-geometries that gave nothing to keep. parts_lost: usize, /// Rings and surface patches of its geometries left out. @@ -341,7 +390,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> } let mut kinds: BTreeMap = BTreeMap::new(); let mut order: Vec = Vec::new(); - let (mut skipped, mut file_srs): (usize, Option<(Crs, bool)>) = (0, None); + let mut file_srs: Option<(Crs, bool)> = None; let mut unknown_srs: Option = None; let mut over = 0usize; // Rings and parts left out of the geometries kept. @@ -364,10 +413,11 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> let mut rings_lost = 0; // Properties not read, and those holding GML of a kind not read (another kind of // geometry): the feature's unreadable shape when it has none, else not read. - let (mut unread, mut shapes_not_read) = (Vec::new(), Vec::new()); + // Each with the property's place, so the notice names them in document order. + let (mut unread, mut shapes_not_read): (Vec<(usize, String)>, Vec<(usize, String)>) = (Vec::new(), Vec::new()); // Geometry properties that couldn't be read: said as not read when another gave the shape. let mut failed = Vec::new(); - for p in f.children().filter(|c| c.is_element()) { + for (at, p) in f.children().filter(|c| c.is_element()).enumerate() { // A property holding a geometry, or a simple value. // A feature's bounding box (gml:boundedBy) is neither its shape nor an attribute. if is_gml(&p) && local(&p) == "boundedBy" { @@ -375,7 +425,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> } if let Some(g) = p.children().find(|c| c.is_element() && is_gml(c) && GEOMETRIES.contains(&local(c))) { if geom.is_some() { - unread.push(local(&p).to_string()); + unread.push((at, local(&p).to_string())); continue; } let s = g.attribute("srsName").or_else(|| g.descendants().find_map(|d| d.attribute("srsName"))); @@ -397,42 +447,46 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> } None => { bad = true; - failed.push(local(&p).to_string()); + failed.push((at, local(&p).to_string())); } } } else if let Some(c) = p.children().find(|c| c.is_element() && is_gml(c)) { // A geometry of a kind not read (a CompositeCurve, a Solid…) may be the feature's // only shape; another GML value (a time, a definition) is no shape. - if is_shape(local(&c)) { &mut shapes_not_read } else { &mut unread }.push(local(&p).to_string()); + if is_shape(local(&c)) { &mut shapes_not_read } else { &mut unread }.push((at, local(&p).to_string())); } else if p.children().any(|c| c.is_element()) { // A nested record isn't an attribute. - unread.push(local(&p).to_string()); + unread.push((at, local(&p).to_string())); } else { let key = local(&p).to_string(); - if !kind.columns.contains(&key) && kind.columns.len() < 500 { + if !kind.columns.contains(&key) { + if kind.columns.len() >= MAX_COLUMNS { + kind.capped += 1; + kind.capped_names.add(key); + continue; + } kind.columns.push(key.clone()); } - attrs.insert(key, text_of(&p).trim().to_string()); + if attrs.insert(key.clone(), text_of(&p).trim().to_string()).is_some() { + kind.repeated += 1; + kind.repeated_names.add(key); + } } } // With no shape read, the first one that failed or isn't read is the feature's unreadable // shape; any others are said as not read. failed.append(&mut shapes_not_read); + failed.sort_by_key(|(at, _)| *at); if geom.is_none() && !failed.is_empty() { bad = true; unread.extend(failed.drain(..).skip(1)); } else { unread.append(&mut failed); } - for p in unread { + unread.sort_by_key(|(at, _)| *at); + for (_, p) in unread { kind.unread += 1; - if !kind.unread_names.contains(&p) { - if kind.unread_names.len() < 3 { - kind.unread_names.push(p); - } else { - kind.unread_more = true; - } - } + kind.unread_names.add(p); } // A gml:id is a feature's id when it has no other. if let Some(id) = f.attributes().find(|a| a.name() == "id" || a.name() == "fid").map(|a| a.value().to_string()) { @@ -442,15 +496,12 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> attrs.insert("gml_id".into(), id); } // A geometry that couldn't be read, unless another property gave the feature its shape. - skipped += usize::from(bad && geom.is_none()); + kind.skipped += usize::from(bad && geom.is_none()); kind.zm += usize::from(zm && geom.is_some()); kind.lost += rings_lost; kind.rows.push((geom, attrs)); } - let mut report = ReadReport { skipped, ..ReadReport::default() }; - if skipped > 0 { - report.messages.push(format!("{skipped} features had geometry that couldn't be read")); - } + let mut report = ReadReport::default(); if over > 0 { report.messages.push(format!("{over} features of more than {MAX_LAYERS} feature types were left out")); } @@ -473,7 +524,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> let title = if single { stem.clone() } else { format!("{stem} {name}") }; let mut ds = Dataset::new(title, GeometryType::None, fields); let (zm, unread, parts) = (kind.zm, kind.unread, kind.parts_lost); - let unread_names = kind.unread_names.join(", ") + if kind.unread_more { ", …" } else { "" }; + let unread_names = kind.unread_names.said(); let mut outside = false; for (g, attrs) in kind.rows { let g = g.map(|g| match &transform { @@ -496,6 +547,12 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> ds.geometry_type = crate::io::dominant_type(&ds); // The file's notices go with its first layer only. let mut rep = if out.is_empty() { report.clone() } else { ReadReport::default() }; + // Features whose shape couldn't be read are said with their own feature type. + rep.skipped = kind.skipped; + if kind.skipped > 0 { + let what = if kind.skipped == 1 { "feature" } else { "features" }; + rep.messages.push(format!("{}: {} {what} had geometry that couldn't be read", ds.name, kind.skipped)); + } if file_srs.is_none() && outside { rep.messages.push("the GML doesn't say its coordinate system (srsName) and its coordinates aren't longitude/latitude".into()); } @@ -514,8 +571,10 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> } if parts > 0 { let what = if parts == 1 { "part" } else { "parts" }; - rep.messages - .push(format!("{}: {parts} {what} of multi-geometries not read (a reference, too short, or of a kind not read)", ds.name)); + rep.messages.push(format!( + "{}: {parts} {what} of multi-geometries not read (a reference, too short, invalid coordinates, or of a kind not read)", + ds.name + )); } if unread > 0 { let what = if unread == 1 { "property" } else { "properties" }; @@ -524,6 +583,24 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> ds.name )); } + if kind.repeated > 0 { + let what = if kind.repeated == 1 { "value" } else { "values" }; + rep.messages.push(format!( + "{}: {} {what} of repeated properties not read, only the last is kept ({})", + ds.name, + kind.repeated, + kind.repeated_names.said() + )); + } + if kind.capped > 0 { + let what = if kind.capped == 1 { "value" } else { "values" }; + rep.messages.push(format!( + "{}: {} {what} of properties past the first {MAX_COLUMNS} names not read ({})", + ds.name, + kind.capped, + kind.capped_names.said() + )); + } out.push((ds, rep)); } Ok(out) @@ -620,7 +697,7 @@ mod tests { .1 .messages .iter() - .any(|m| m == "r: 1 part of multi-geometries not read (a reference, too short, or of a kind not read)"), + .any(|m| m == "r: 1 part of multi-geometries not read (a reference, too short, invalid coordinates, or of a kind not read)"), "{:?}", layers[0].1.messages ); @@ -713,7 +790,7 @@ mod tests { assert_eq!((ds.features.len(), rep.skipped), (2, 0)); assert!(matches!(&ds.features[1].geometry, Some(Geometry::Line(l)) if l.len() == 2)); let said: Vec<&String> = rep.messages.iter().filter(|m| m.contains("multi-geometries")).collect(); - assert_eq!(said, ["m: 1 part of multi-geometries not read (a reference, too short, or of a kind not read)"]); + assert_eq!(said, ["m: 1 part of multi-geometries not read (a reference, too short, invalid coordinates, or of a kind not read)"]); } /// Properties that aren't attributes (a nested record, a time, a second shape) are said on @@ -782,7 +859,7 @@ mod tests { let layers = read("u.gml", gml.as_bytes()).unwrap(); let (_, rep) = &layers[0]; assert_eq!(rep.skipped, 1, "{:?}", rep.messages); - assert!(rep.messages.iter().any(|m| m.ends_with("further shapes (m, b)")), "{:?}", rep.messages); + assert!(rep.messages.iter().any(|m| m.ends_with("further shapes (b, m)")), "{:?}", rep.messages); // A shape that can't be read, with another that can: the feature has its shape, and the // one left out is said. let gml = r#" @@ -798,6 +875,107 @@ mod tests { ); } + fn doc(members: &str) -> String { + format!( + r#"{members}"# + ) + } + + fn said(layer: &(Dataset, ReadReport), what: &str) -> bool { + layer.1.messages.iter().any(|m| m.contains(what)) + } + + /// #1620: a member holding one part read and one not says the second; a whole one says nothing. + #[test] + fn a_member_partly_read_says_its_lost_part() { + let patch = |pos: &str| { + format!( + "{pos}" + ) + }; + let member = |second: &str| { + doc(&format!( + "{}{}", + patch("10 48 11 48 11 49 10 48"), + patch(second) + )) + }; + let lossy = read("m.gml", member("x y z").as_bytes()).unwrap(); + assert!( + said(&lossy[0], "m: 1 part of multi-geometries not read (a reference, too short, invalid coordinates"), + "{:?}", + lossy[0].1.messages + ); + assert!(lossy[0].0.features[0].geometry.is_some()); + let clean = read("m.gml", member("10 48 12 48 12 49 10 48").as_bytes()).unwrap(); + assert!(!said(&clean[0], "multi-geometries"), "{:?}", clean[0].1.messages); + } + + /// #1620: an interior ring left out is said (already so since #1612). + #[test] + fn an_unreadable_interior_ring_is_said() { + let poly = |hole: &str| { + doc(&format!( + "10 48 11 48 11 49 10 48{hole}" + )) + }; + let lossy = read("i.gml", poly("x y").as_bytes()).unwrap(); + assert!(said(&lossy[0], ": 1 ring or surface patch of a geometry was left out"), "{:?}", lossy[0].1.messages); + let clean = read("i.gml", poly("10.2 48.1 10.8 48.1 10.8 48.3 10.2 48.1").as_bytes()).unwrap(); + assert!(!said(&clean[0], "ring"), "{:?}", clean[0].1.messages); + } + + /// #1620: features without a readable shape are counted and said per feature type. + #[test] + fn unreadable_shapes_are_said_on_their_own_feature_type() { + let two = |second: &str| { + doc(&format!( + "10 48{second}" + )) + }; + let lossy = read("t.gml", two("").as_bytes()).unwrap(); + assert_eq!((lossy[0].1.skipped, lossy[1].1.skipped), (0, 1)); + assert!(!said(&lossy[0], "couldn't be read") && said(&lossy[1], "t parks: 1 feature had geometry that couldn't be read")); + let clean = read("t.gml", two("10 49").as_bytes()).unwrap(); + assert!(clean.iter().all(|l| l.1.skipped == 0 && !said(l, "couldn't be read"))); + } + + /// #1620: unread property names come in document order, and a `gml:description` value is no shape. + #[test] + fn unread_names_follow_the_document_and_a_description_is_no_shape() { + let gml = doc( + "10 481", + ); + let layers = read("o.gml", gml.as_bytes()).unwrap(); + assert!(said(&layers[0], "further shapes (z, b, a)"), "{:?}", layers[0].1.messages); + let gml = doc("x"); + let layers = read("d.gml", gml.as_bytes()).unwrap(); + assert_eq!(layers[0].1.skipped, 0, "{:?}", layers[0].1.messages); + assert!(said(&layers[0], "d: 1 property not read"), "{:?}", layers[0].1.messages); + } + + /// #1620: a repeated property keeps its last value and says the rest; so does one past 500 names. + #[test] + fn repeated_and_capped_properties_are_said() { + let feature = |props: &str| doc(&format!("{props}")); + let layers = read("r.gml", feature("ABC").as_bytes()).unwrap(); + assert_eq!(layers[0].0.value(0, 0), &Value::Text("C".into())); + assert!( + said(&layers[0], "r: 2 values of repeated properties not read, only the last is kept (alias)"), + "{:?}", + layers[0].1.messages + ); + let layers = read("r.gml", feature("AB").as_bytes()).unwrap(); + assert!(!said(&layers[0], "repeated") && !said(&layers[0], "past the first"), "{:?}", layers[0].1.messages); + + let many = |n: usize| (0..n).map(|i| format!("v")).collect::(); + let layers = read("c.gml", feature(&many(501)).as_bytes()).unwrap(); + assert_eq!(layers[0].0.fields.len(), MAX_COLUMNS); + assert!(said(&layers[0], "c: 1 value of properties past the first 500 names not read (p500)"), "{:?}", layers[0].1.messages); + let layers = read("c.gml", feature(&many(500)).as_bytes()).unwrap(); + assert!(!said(&layers[0], "past the first"), "{:?}", layers[0].1.messages); + } + #[test] fn junk_is_refused() { assert!(read("x.gml", b"").is_err()); From 64b9cf0453457e6c1ea51a28ac7aa70c31760ac7 Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 16:27:18 -0500 Subject: [PATCH 2/5] fix(data): GML counts grids, unread patches and nested references Grid and RectifiedGrid are geometries in GML 3.2.1, so a feature whose only shape is one is an unreadable shape. A triangle or rectangle patch beside a patch read, and a reference-only member nested in a composite, are now counted as parts of multi-geometries not read. --- crates/data/src/gml.rs | 45 +++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/crates/data/src/gml.rs b/crates/data/src/gml.rs index 574bfea044..de85fb193c 100644 --- a/crates/data/src/gml.rs +++ b/crates/data/src/gml.rs @@ -77,7 +77,7 @@ const GEOMETRIES: &[&str] = &[ /// Geometry elements this reader doesn't take as a feature's shape: the instantiable members of /// the substitution groups of `gml:AbstractGeometry`, `gml:AbstractSurfacePatch` and /// `gml:AbstractCurveSegment` in the GML 3.2.1 schema (geometryBasic0d1d.xsd, geometryAggregates.xsd, -/// geometryComplexes.xsd, geometryPrimitives.xsd at schemas.opengis.net/gml/3.2.1) that aren't in +/// geometryComplexes.xsd, geometryPrimitives.xsd, grids.xsd at schemas.opengis.net/gml/3.2.1) that aren't in /// [`GEOMETRIES`]. const SHAPES_NOT_READ: &[&str] = &[ "Arc", @@ -105,11 +105,13 @@ const SHAPES_NOT_READ: &[&str] = &[ "CompositeSolid", "CompositeSurface", "GeometricComplex", + "Grid", "MultiGeometry", "MultiSolid", "OrientableCurve", "OrientableSurface", "PolyhedralSurface", + "RectifiedGrid", "Ring", "Shell", "Solid", @@ -291,19 +293,28 @@ fn parts_lost(g: &Node) -> usize { (kind, local(x)), ("MultiPoint", "Point") | ("MultiLineString" | "MultiCurve", "LineString" | "Curve") - | ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch") + | ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch" | "Triangle" | "Rectangle") ) }; - let gives = |x: &Node| match kind { - "MultiPoint" => coords(x, &scratch).is_some_and(|c| !c.is_empty()), - "MultiLineString" | "MultiCurve" => coords(x, &scratch).is_some_and(|c| c.len() >= 2), + // Triangles and rectangles are patches the reader doesn't keep. + let gives = |x: &Node| match (kind, local(x)) { + (_, "Triangle" | "Rectangle") => false, + ("MultiPoint", _) => coords(x, &scratch).is_some_and(|c| !c.is_empty()), + ("MultiLineString" | "MultiCurve", _) => coords(x, &scratch).is_some_and(|c| c.len() >= 2), _ => polygon(x, &scratch, &Cell::new(0)).is_some(), }; + // A member nested in a part (a composite's `surfaceMember`) that is only a reference. + let reference = |x: &Node| x.is_element() && is_gml(x) && local(x).ends_with("Member") && !x.children().any(|c| c.is_element()); // A member lost whole (a reference, no part in it) counts once; one holding parts counts those - // it can't give, so a member with one part read and one not says the second. + // it can't give and the references nested in it, so a member with one part read and one not + // says the second. let lost = |part: Node| { let leaves: Vec = part.descendants().filter(&leaf).collect(); - if leaves.is_empty() { 1 } else { leaves.iter().filter(|x| !gives(x)).count() } + if leaves.is_empty() { + 1 + } else { + leaves.iter().filter(|x| !gives(x)).count() + part.descendants().skip(1).filter(&reference).count() + } }; // A member property holds one part (`curveMember`), a members property several (`curveMembers`). g.children() @@ -909,6 +920,22 @@ mod tests { assert!(lossy[0].0.features[0].geometry.is_some()); let clean = read("m.gml", member("10 48 12 48 12 49 10 48").as_bytes()).unwrap(); assert!(!said(&clean[0], "multi-geometries"), "{:?}", clean[0].1.messages); + assert!(matches!(&clean[0].0.features[0].geometry, Some(Geometry::Polygon(p)) if p.len() == 2)); + // A triangle beside a patch read, and a reference nested in a composite beside a polygon + // read: each is a part not read. + let tri = "10 48 11 48 11 49 10 48"; + let gml = doc(&format!( + "{}{tri}", + patch("10 48 11 48 11 49 10 48") + )); + let layers = read("m.gml", gml.as_bytes()).unwrap(); + assert!(said(&layers[0], "m: 1 part of multi-geometries not read"), "{:?}", layers[0].1.messages); + let gml = doc( + "10 48 11 48 11 49 10 48", + ); + let layers = read("m.gml", gml.as_bytes()).unwrap(); + assert!(layers[0].0.features[0].geometry.is_some()); + assert!(said(&layers[0], "m: 1 part of multi-geometries not read"), "{:?}", layers[0].1.messages); } /// #1620: an interior ring left out is said (already so since #1612). @@ -948,6 +975,10 @@ mod tests { ); let layers = read("o.gml", gml.as_bytes()).unwrap(); assert!(said(&layers[0], "further shapes (z, b, a)"), "{:?}", layers[0].1.messages); + // A grid (gml:AbstractImplicitGeometry) is a shape not read, the feature's only one here. + let gml = doc(""); + let layers = read("g.gml", gml.as_bytes()).unwrap(); + assert_eq!(layers[0].1.skipped, 1, "{:?}", layers[0].1.messages); let gml = doc("x"); let layers = read("d.gml", gml.as_bytes()).unwrap(); assert_eq!(layers[0].1.skipped, 0, "{:?}", layers[0].1.messages); From 759819a7e7395d713b2cdcad3254b895898b587d Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 17:34:37 -0500 Subject: [PATCH 3/5] fix(data): a GML surface says the triangles and rectangles it leaves out A plain gml:Surface with a patch read beside a Triangle or Rectangle kept the polygon and dropped the other patch without a notice. Count them as patches left out (multi-surfaces stay with parts_lost) and say a patch of a kind not read in the notice. --- crates/data/src/gml.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/data/src/gml.rs b/crates/data/src/gml.rs index de85fb193c..207e1634cd 100644 --- a/crates/data/src/gml.rs +++ b/crates/data/src/gml.rs @@ -258,6 +258,11 @@ fn geometry(g: &Node, zm: &Cell, lost: &Cell) -> Option { poly }) .collect(); + // A plain surface's triangles and rectangles aren't read: said as left out (a + // multi-surface's are counted by `parts_lost`). + if !polys.is_empty() && !local(g).starts_with("Multi") { + lost.set(lost.get() + kids(&["Triangle", "Rectangle"]).len()); + } (!polys.is_empty()).then_some(Geometry::Polygon(polys)) } "Envelope" | "Box" => { @@ -572,7 +577,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> let what = if kind.lost == 1 { "ring or surface patch of a geometry was" } else { "rings or surface patches of geometries were" }; rep.messages.push(format!( - "{}: {} {what} left out: too few points, unreadable coordinates, a patch with no exterior ring, or an exterior ring past the first", + "{}: {} {what} left out: too few points, unreadable coordinates, a patch with no exterior ring or of a kind not read, or an exterior ring past the first", ds.name, kind.lost )); } @@ -938,6 +943,26 @@ mod tests { assert!(said(&layers[0], "m: 1 part of multi-geometries not read"), "{:?}", layers[0].1.messages); } + /// A plain surface's triangle or rectangle beside a patch read is said as left out; two + /// patches read say nothing. + #[test] + fn a_surface_says_its_unread_patches() { + let ring = "10 48 11 48 11 49 10 48"; + let surface = |second: &str| { + doc(&format!( + "{ring}{ring}" + )) + }; + for kind in ["Triangle", "Rectangle"] { + let layers = read("s.gml", surface(kind).as_bytes()).unwrap(); + assert!(layers[0].0.features[0].geometry.is_some()); + assert!(said(&layers[0], "s: 1 ring or surface patch of a geometry was left out"), "{kind}: {:?}", layers[0].1.messages); + assert!(said(&layers[0], "or of a kind not read"), "{:?}", layers[0].1.messages); + } + let clean = read("s.gml", surface("PolygonPatch").as_bytes()).unwrap(); + assert!(!said(&clean[0], "surface patch"), "{:?}", clean[0].1.messages); + } + /// #1620: an interior ring left out is said (already so since #1612). #[test] fn an_unreadable_interior_ring_is_said() { From 961566daca64b38a21db10f3a156a015b2beee0c Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 17:59:46 -0500 Subject: [PATCH 4/5] fix(data): GML says unread shapes nested beside a part read An arc in a composite curve beside a line read, or a surface that is only a reference in a composite beside a polygon read, was dropped without a notice; each now counts as a part not read. A plain surface's cones, cylinders and spheres count as patches left out, and GML 2's GeometryCollection is a shape not read. --- crates/data/src/gml.rs | 70 +++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/crates/data/src/gml.rs b/crates/data/src/gml.rs index 207e1634cd..f981e17d63 100644 --- a/crates/data/src/gml.rs +++ b/crates/data/src/gml.rs @@ -78,7 +78,7 @@ const GEOMETRIES: &[&str] = &[ /// the substitution groups of `gml:AbstractGeometry`, `gml:AbstractSurfacePatch` and /// `gml:AbstractCurveSegment` in the GML 3.2.1 schema (geometryBasic0d1d.xsd, geometryAggregates.xsd, /// geometryComplexes.xsd, geometryPrimitives.xsd, grids.xsd at schemas.opengis.net/gml/3.2.1) that aren't in -/// [`GEOMETRIES`]. +/// [`GEOMETRIES`], and GML 2's `GeometryCollection`. const SHAPES_NOT_READ: &[&str] = &[ "Arc", "ArcByBulge", @@ -94,6 +94,7 @@ const SHAPES_NOT_READ: &[&str] = &[ "CubicSpline", "Cylinder", "Geodesic", + "GeometryCollection", "GeodesicString", "LineStringSegment", "OffsetCurve", @@ -258,10 +259,11 @@ fn geometry(g: &Node, zm: &Cell, lost: &Cell) -> Option { poly }) .collect(); - // A plain surface's triangles and rectangles aren't read: said as left out (a + // A plain surface's triangles, rectangles and parametric patches (cones, cylinders, + // spheres) aren't read: said as left out (a // multi-surface's are counted by `parts_lost`). if !polys.is_empty() && !local(g).starts_with("Multi") { - lost.set(lost.get() + kids(&["Triangle", "Rectangle"]).len()); + lost.set(lost.get() + kids(&["Triangle", "Rectangle", "Cone", "Cylinder", "Sphere"]).len()); } (!polys.is_empty()).then_some(Geometry::Polygon(polys)) } @@ -298,28 +300,32 @@ fn parts_lost(g: &Node) -> usize { (kind, local(x)), ("MultiPoint", "Point") | ("MultiLineString" | "MultiCurve", "LineString" | "Curve") - | ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch" | "Triangle" | "Rectangle") + | ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch") ) }; - // Triangles and rectangles are patches the reader doesn't keep. - let gives = |x: &Node| match (kind, local(x)) { - (_, "Triangle" | "Rectangle") => false, - ("MultiPoint", _) => coords(x, &scratch).is_some_and(|c| !c.is_empty()), - ("MultiLineString" | "MultiCurve", _) => coords(x, &scratch).is_some_and(|c| c.len() >= 2), + let gives = |x: &Node| match kind { + "MultiPoint" => coords(x, &scratch).is_some_and(|c| !c.is_empty()), + "MultiLineString" | "MultiCurve" => coords(x, &scratch).is_some_and(|c| c.len() >= 2), _ => polygon(x, &scratch, &Cell::new(0)).is_some(), }; + // A shape nested in a part with nothing in it the reader takes: a triangle, an arc, a + // surface that is only a reference. + let unread_shape = |x: &Node| x.is_element() && is_gml(x) && is_shape(local(x)) && !x.descendants().any(|d| leaf(&d)); // A member nested in a part (a composite's `surfaceMember`) that is only a reference. let reference = |x: &Node| x.is_element() && is_gml(x) && local(x).ends_with("Member") && !x.children().any(|c| c.is_element()); // A member lost whole (a reference, no part in it) counts once; one holding parts counts those - // it can't give and the references nested in it, so a member with one part read and one not - // says the second. + // it can't give, and the unread shapes and references nested in it outside a part read or a + // shape already counted, so a member with one part read and one not says the second. let lost = |part: Node| { let leaves: Vec = part.descendants().filter(&leaf).collect(); if leaves.is_empty() { - 1 - } else { - leaves.iter().filter(|x| !gives(x)).count() + part.descendants().skip(1).filter(&reference).count() + return 1; } + // Whether something between `x` and the part is a part read or a shape counted. + let inside = |x: &Node| x.ancestors().skip(1).take_while(|a| *a != part).any(|a| unread_shape(&a) || leaf(&a)); + let shapes = part.descendants().skip(1).filter(|x| unread_shape(x) && !inside(x)).count(); + let refs = part.descendants().skip(1).filter(|x| reference(x) && !inside(x)).count(); + leaves.iter().filter(|x| !gives(x)).count() + shapes + refs }; // A member property holds one part (`curveMember`), a members property several (`curveMembers`). g.children() @@ -959,10 +965,46 @@ mod tests { assert!(said(&layers[0], "s: 1 ring or surface patch of a geometry was left out"), "{kind}: {:?}", layers[0].1.messages); assert!(said(&layers[0], "or of a kind not read"), "{:?}", layers[0].1.messages); } + let layers = read("s.gml", surface("Cylinder").as_bytes()).unwrap(); + assert!(said(&layers[0], "s: 1 ring or surface patch of a geometry was left out"), "{:?}", layers[0].1.messages); let clean = read("s.gml", surface("PolygonPatch").as_bytes()).unwrap(); assert!(!said(&clean[0], "surface patch"), "{:?}", clean[0].1.messages); } + /// Unread shapes nested beside a part read are said: an arc in a composite curve, a surface + /// that is only a reference; a curve's own arc segment is not a part lost. + #[test] + fn nested_unread_shapes_are_said() { + let line = "10 48 11 48"; + let arc = "10 48 10.5 48.5 11 48"; + let multi = |inner: &str| doc(&format!("{inner}")); + let gml = multi(&format!( + "{arc}{line}" + )); + let layers = read("n.gml", gml.as_bytes()).unwrap(); + assert!(layers[0].0.features[0].geometry.is_some()); + assert!(said(&layers[0], "n: 1 part of multi-geometries not read"), "{:?}", layers[0].1.messages); + let poly = "10 48 11 48 11 49 10 48"; + let gml = multi(&format!( + "{poly}" + )); + let layers = read("n.gml", gml.as_bytes()).unwrap(); + assert!(said(&layers[0], "n: 1 part of multi-geometries not read"), "{:?}", layers[0].1.messages); + let gml = multi(&format!( + "{arc}{line}" + )); + let layers = read("n.gml", gml.as_bytes()).unwrap(); + assert!(!said(&layers[0], "multi-geometries"), "{:?}", layers[0].1.messages); + } + + /// A GML 2 feature whose only shape is a GeometryCollection had geometry that couldn't be read. + #[test] + fn a_gml2_geometry_collection_is_a_shape_not_read() { + let gml = r#"10,48"#; + let layers = read("c.gml", gml.as_bytes()).unwrap(); + assert_eq!(layers[0].1.skipped, 1, "{:?}", layers[0].1.messages); + } + /// #1620: an interior ring left out is said (already so since #1612). #[test] fn an_unreadable_interior_ring_is_said() { From 7dcb5a1a138f6b15b4c9fa82412e99575fc951ee Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 18:24:17 -0500 Subject: [PATCH 5/5] perf(data): mark the GML elements holding a part once The nested unread-shape check walked each ancestor's subtree for every candidate, quadratic on a hostile member. Mark the elements holding a part once per member so each check walks up the tree only. --- crates/data/src/gml.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/data/src/gml.rs b/crates/data/src/gml.rs index f981e17d63..b9d2de93e2 100644 --- a/crates/data/src/gml.rs +++ b/crates/data/src/gml.rs @@ -308,9 +308,6 @@ fn parts_lost(g: &Node) -> usize { "MultiLineString" | "MultiCurve" => coords(x, &scratch).is_some_and(|c| c.len() >= 2), _ => polygon(x, &scratch, &Cell::new(0)).is_some(), }; - // A shape nested in a part with nothing in it the reader takes: a triangle, an arc, a - // surface that is only a reference. - let unread_shape = |x: &Node| x.is_element() && is_gml(x) && is_shape(local(x)) && !x.descendants().any(|d| leaf(&d)); // A member nested in a part (a composite's `surfaceMember`) that is only a reference. let reference = |x: &Node| x.is_element() && is_gml(x) && local(x).ends_with("Member") && !x.children().any(|c| c.is_element()); // A member lost whole (a reference, no part in it) counts once; one holding parts counts those @@ -321,6 +318,13 @@ fn parts_lost(g: &Node) -> usize { if leaves.is_empty() { return 1; } + // The elements holding a part (the parts too), marked once so each check below is a walk up + // the tree, not down it: a hostile member can't make it quadratic. + let holds: std::collections::HashSet = + leaves.iter().flat_map(|l| l.ancestors().take_while(|a| *a != part).map(|a| a.id())).collect(); + // A shape nested in a part with nothing in it the reader takes: a triangle, an arc, a + // surface that is only a reference. + let unread_shape = |x: &Node| x.is_element() && is_gml(x) && is_shape(local(x)) && !holds.contains(&x.id()); // Whether something between `x` and the part is a part read or a shape counted. let inside = |x: &Node| x.ancestors().skip(1).take_while(|a| *a != part).any(|a| unread_shape(&a) || leaf(&a)); let shapes = part.descendants().skip(1).filter(|x| unread_shape(x) && !inside(x)).count(); @@ -972,7 +976,8 @@ mod tests { } /// Unread shapes nested beside a part read are said: an arc in a composite curve, a surface - /// that is only a reference; a curve's own arc segment is not a part lost. + /// that is only a reference; a curve's own arc segment is not a part lost (the curve keeps its + /// control points, a separate approximation). #[test] fn nested_unread_shapes_are_said() { let line = "10 48 11 48";