From 87536ae0480c3a7b44a06e9e298db2277a8bcd2f Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 30 Nov 2026 17:39:30 -0600 Subject: [PATCH 1/2] fix(data): preserve KML fields declared as text Look up the local SchemaData reference before type inference so declared strings keep their values and padding. --- crates/data/src/kml.rs | 63 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/crates/data/src/kml.rs b/crates/data/src/kml.rs index 561e1b52da..4bde76c860 100644 --- a/crates/data/src/kml.rs +++ b/crates/data/src/kml.rs @@ -88,7 +88,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> { geometry_losses: GeometryLosses::default(), }; r.collect_styles(doc.root()); - r.walk(doc.root(), &mut Vec::new(), 0); + r.walk(doc.root(), &schema_strings(doc.root()), &mut Vec::new(), 0); r.collect_fields(doc.root()); Ok(r.layers(&name)) } @@ -233,6 +233,20 @@ fn explicit_string(n: &Node) -> bool { name == "string" && n.lookup_namespace_uri(prefix) == Some("http://www.w3.org/2001/XMLSchema") } +/// Index local schema declarations so a field's type follows its own SchemaData reference. +fn schema_strings<'a>(root: Node<'a, '_>) -> HashSet<(&'a str, &'a str)> { + let mut strings = HashSet::new(); + for schema in root.descendants().filter(|n| local(n) == "Schema") { + let Some(id) = schema.attribute("id") else { continue }; + for field in schema.children().filter(|n| local(n) == "SimpleField" && n.attribute("type") == Some("string")) { + if let Some(name) = field.attribute("name") { + strings.insert((id, name)); + } + } + } + strings +} + fn local<'a>(n: &Node<'a, '_>) -> &'a str { n.tag_name().name() } @@ -592,7 +606,8 @@ impl Reader { self.styles.contains_key(&id).then_some(id) } - fn walk(&mut self, n: Node, folders: &mut Vec, depth: usize) { + /// Visits nested folders in source order, keeping the path used by each placemark. + fn walk(&mut self, n: Node, strings: &HashSet<(&str, &str)>, folders: &mut Vec, depth: usize) { if depth > MAX_DEPTH { return; } @@ -603,21 +618,22 @@ impl Reader { if named { folders.push(child_text(&c, "name").unwrap_or_default()); } - self.walk(c, folders, depth + 1); + self.walk(c, strings, folders, depth + 1); if named { folders.pop(); } } - "Placemark" => self.placemark(&c, folders), + "Placemark" => self.placemark(&c, strings, folders), // Pictures, read by `ground_overlays`. "GroundOverlay" => {} - "kml" => self.walk(c, folders, depth + 1), + "kml" => self.walk(c, strings, folders, depth + 1), _ => {} } } } - fn placemark(&mut self, pm: &Node, folders: &[String]) { + /// Keeps the first value for each extended data field and retains each drawable geometry kind. + fn placemark(&mut self, pm: &Node, strings: &HashSet<(&str, &str)>, folders: &[String]) { let mut parts = Parts::default(); for g in pm.children().filter(|c| c.is_element()) { geometry(&g, &mut parts, 0); @@ -638,7 +654,16 @@ impl Reader { let text = value.map(|v| if string { raw_text_of(&v) } else { data_text(&v, k) }).unwrap_or_default(); (k.to_string(), text, string) }), - "SimpleData" => d.attribute("name").map(|k| (k.to_string(), data_text(&d, k), false)), + "SimpleData" => d.attribute("name").map(|k| { + let string = d + .parent_element() + .filter(|parent| local(parent) == "SchemaData") + .and_then(|parent| parent.attribute("schemaUrl")) + .and_then(|url| url.strip_prefix('#')) + .is_some_and(|id| strings.contains(&(id, k))); + let text = if string { raw_text_of(&d) } else { data_text(&d, k) }; + (k.to_string(), text, string) + }), _ => None, }; if let Some((k, v, string)) = pair.filter(|(k, _, _)| !k.trim().is_empty()) { @@ -1130,6 +1155,30 @@ mod tests { assert_eq!(ds.value(0, id), &Value::Text("12345678901234567891".into())); } + /// A local schema keeps numeric-looking house numbers as text, including padding. + #[test] + fn local_kml_schema_strings_keep_their_spelling() { + let xml = br##" + + 12 7 + 1,2 + + 42 + 1,2 3,4 + + + "##; + let layers = read("permits.kml", xml).unwrap(); + let points = &layers[0].dataset; + let house = points.field_index("House").unwrap(); + assert_eq!(points.fields[house].ty, FieldType::Text); + assert_eq!(points.value(0, house), &Value::Text(" 12 ".into())); + assert_eq!(points.value(0, points.field_index("sequence").unwrap()), &Value::Int(7)); + let lines = &layers[1].dataset; + assert_eq!(lines.value(0, lines.field_index("House").unwrap()), &Value::Int(42)); + assert!(layers.iter().all(|layer| layer.report.messages.is_empty())); + } + #[test] fn source_ids_and_declared_empty_columns_survive_kml_round_trips() { let xml = r##" From 601162b4dde0d48e9a9e44188641f827cbe0e77a Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 30 Nov 2026 19:18:49 -0600 Subject: [PATCH 2/2] fix(data): a KML text value leaves out the file's indentation --- crates/data/src/kml.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/crates/data/src/kml.rs b/crates/data/src/kml.rs index 4bde76c860..207aa0789d 100644 --- a/crates/data/src/kml.rs +++ b/crates/data/src/kml.rs @@ -233,7 +233,8 @@ fn explicit_string(n: &Node) -> bool { name == "string" && n.lookup_namespace_uri(prefix) == Some("http://www.w3.org/2001/XMLSchema") } -/// Index local schema declarations so a field's type follows its own SchemaData reference. +/// The (schema id, field name) pairs the file's own schemas declare `string`. A value is looked up by the +/// schema its SchemaData names; one found makes its column text for the whole layer it is in. fn schema_strings<'a>(root: Node<'a, '_>) -> HashSet<(&'a str, &'a str)> { let mut strings = HashSet::new(); for schema in root.descendants().filter(|n| local(n) == "Schema") { @@ -265,6 +266,19 @@ fn text_of(n: &Node) -> String { raw_text_of(n).trim().to_string() } +/// The text of a field declared `string`, padding and all, without the line breaks and indentation an +/// indented file puts around it. +fn string_text(n: &Node) -> String { + let raw = raw_text_of(n); + let (start, end) = (raw.trim_start(), raw.trim_end()); + let lead = raw.len() - start.len(); + let trail = raw.len() - end.len(); + let broken = |edge: Option<&str>| edge.is_some_and(|s| s.contains('\n')); + let from = if broken(raw.get(..lead)) { lead } else { 0 }; + let to = if broken(raw.get(end.len()..)) { raw.len() - trail } else { raw.len() }; + raw.get(from..to).unwrap_or_default().to_string() +} + fn data_text(n: &Node, key: &str) -> String { if key == "description" { raw_text_of(n) } else { text_of(n) } } @@ -661,7 +675,7 @@ impl Reader { .and_then(|parent| parent.attribute("schemaUrl")) .and_then(|url| url.strip_prefix('#')) .is_some_and(|id| strings.contains(&(id, k))); - let text = if string { raw_text_of(&d) } else { data_text(&d, k) }; + let text = if string && k != "description" { string_text(&d) } else { data_text(&d, k) }; (k.to_string(), text, string) }), _ => None, @@ -1179,6 +1193,29 @@ mod tests { assert!(layers.iter().all(|layer| layer.report.messages.is_empty())); } + /// An indented file's line breaks around a value aren't in it, and a value of one layer declared + /// `string` makes the layer's column text. + #[test] + fn local_kml_schema_strings_leave_out_the_files_indentation() { + let place = |schema: &str, house: &str| { + format!( + "{house}1,2" + ) + }; + let values = ["\n 012\n ", " 12 ", "\n ", "a\nb", "\n 12 "]; + let places: String = values.iter().map(|v| place("permits", v)).collect(); + let xml = format!( + "{places}{}", + place("other", "7") + ); + let layers = read("permits.kml", xml.as_bytes()).unwrap(); + let ds = &layers[0].dataset; + let house = ds.field_index("House").unwrap(); + assert_eq!(ds.fields[house].ty, FieldType::Text); + let read: Vec = (0..6).map(|r| ds.value(r, house).to_string()).collect(); + assert_eq!(read, ["012", " 12 ", "", "a\nb", "12 ", "7"]); + } + #[test] fn source_ids_and_declared_empty_columns_survive_kml_round_trips() { let xml = r##"