Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ A result is right, or RustGIS says what it lost or assumed
- Known silent losses: DuckDB queries (`ST_Force2D`) drop Z without a notice; the OSM reader leaves out rings that don't close, rings and member ways with
nodes or ways the file doesn't hold, and inner rings outside every outer one; and the text
readers' losses listed as `Silent` in `crates/engine/tests/integration/never_silent_reading.rs`, among them
unreadable coordinates, holes and parts in KML, GeoRSS, MapInfo and
unreadable coordinates, holes and parts in MapInfo and
DXF,
and attribute fields/values in XLSB short-cell workbooks (the original SheetJS 0.18.5 clean
workbook retains its point but no attributes, without a notice). A fix makes
Expand Down
42 changes: 34 additions & 8 deletions crates/data/src/georss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ fn bbox(ll: Coord, ur: Coord) -> Option<Geometry> {
}

/// An item's place, `Ok(None)` when it has none, `Err` when it has one that can't be read.
/// Unreadable polygon holes are omitted and counted in `lost_holes`.
fn place(item: &Node, lost_holes: &mut usize) -> std::result::Result<Option<Geometry>, ()> {
/// Unreadable polygon holes and coordinate pairs past the first in points are counted where omitted.
fn place(item: &Node, lost_holes: &mut usize, excess_points: &mut usize) -> std::result::Result<Option<Geometry>, ()> {
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(());
let mut point = |pts: Vec<Coord>| {
*excess_points += pts.len().saturating_sub(1);
pts.first().map(|c| Some(Geometry::Point(*c))).ok_or(())
};
if let Some(n) = g("point") {
return parse(n)?.first().map(|c| Some(Geometry::Point(*c))).ok_or(());
return point(parse(n)?);
}
if let Some(n) = g("line") {
let pts = parse(n)?;
Expand All @@ -93,7 +97,7 @@ fn place(item: &Node, lost_holes: &mut usize) -> std::result::Result<Option<Geom
};
let pos = |name: &str| gml.descendants().find(|d| d.is_element() && local(d) == name).ok_or(());
return match local(&gml) {
"Point" => parse(pos("pos")?)?.first().map(|c| Some(Geometry::Point(*c))).ok_or(()),
"Point" => point(parse(pos("pos")?)?),
"LineString" => {
let pts = parse(pos("posList")?)?;
if pts.len() >= 2 { Ok(Some(Geometry::Line(vec![pts]))) } else { Err(()) }
Expand Down Expand Up @@ -129,9 +133,7 @@ fn place(item: &Node, lost_holes: &mut usize) -> std::result::Result<Option<Geom
// W3C Basic Geo: geo:lat and geo:long, on the item or in a geo:Point.
let holder = g("Point").unwrap_or(*item);
match (child_text(&holder, "lat"), child_text(&holder, "long").or_else(|| child_text(&holder, "lon"))) {
(Some(lat), Some(lon)) => {
lat_lons(&format!("{lat} {lon}")).and_then(|v| v.first().copied()).map(|c| Some(Geometry::Point(c))).ok_or(())
}
(Some(lat), Some(lon)) => point(lat_lons(&format!("{lat} {lon}")).ok_or(())?),
(None, None) => Ok(None),
_ => Err(()),
}
Expand Down Expand Up @@ -167,9 +169,10 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
Dataset::new(format!("{name} lines"), GeometryType::Polyline, fields()),
Dataset::new(format!("{name} polygons"), GeometryType::Polygon, fields()),
];
let mut excess_points = 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, &mut lost_holes) {
match place(&item, &mut lost_holes, &mut excess_points) {
Ok(Some(g)) => {
let i = match g.kind() {
GeometryType::Polyline => 1,
Expand All @@ -185,6 +188,10 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
}
}
let mut report = ReadReport { skipped, ..ReadReport::default() };
if excess_points > 0 {
let (pairs, verb) = if excess_points == 1 { ("pair", "was") } else { ("pairs", "were") };
report.messages.push(format!("{excess_points} excess GeoRSS Point coordinate {pairs} {verb} left out"));
}
if skipped > 0 {
report.messages.push(format!("{skipped} items had places that couldn't be read"));
}
Expand Down Expand Up @@ -216,6 +223,25 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
mod tests {
use super::*;

#[test]
fn xml_loss_excess_point_pairs_are_counted() {
for place in [
"<georss:point>1 2 3 4 5 6</georss:point>",
"<georss:where><gml:Point><gml:pos>1 2 3 4 5 6</gml:pos></gml:Point></georss:where>",
] {
let xml = format!(
r#"<rss xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><channel><item><title>kept</title>{place}</item></channel></rss>"#
);
let layers = read("a.rss", xml.as_bytes()).unwrap();
assert_eq!(layers.len(), 1);
assert_eq!(layers[0].0.features[0].geometry, Some(Geometry::Point(Coord::new(2.0, 1.0))));
assert_eq!(layers[0].0.value(0, 0), &Value::Text("kept".into()));
assert_eq!(layers[0].1.messages, ["2 excess GeoRSS Point coordinate pairs were left out"]);
let clean = xml.replace("1 2 3 4 5 6", "1 2");
assert!(read("a.rss", clean.as_bytes()).unwrap()[0].1.messages.is_empty());
}
}

const RSS: &str = r#"<?xml version="1.0"?>
<rss version="2.0" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel><title>Quakes</title>
Expand Down
33 changes: 32 additions & 1 deletion crates/data/src/gpx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
return Err(DataError::Format(format!("{file_name}: not a GPX file")));
}
let mut skipped = 0usize;
let mut short_segments = 0usize;
let point_fields = |extra: &[(&str, FieldType)]| {
let mut f: Vec<Field> = extra.iter().map(|(n, t)| Field::new(*n, *t)).collect();
f.extend(["name", "ele", "time", "desc", "cmt", "sym", "type"].iter().map(|n| Field::new(*n, FieldType::Text)));
Expand Down Expand Up @@ -148,6 +149,8 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
count += part.len();
if part.len() >= 2 {
parts.push(part);
} else if !part.is_empty() {
short_segments += 1;
}
}
if parts.is_empty() {
Expand Down Expand Up @@ -187,9 +190,17 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
out.push((ds, report));
}
if let Some((_, report)) = out.first_mut() {
if short_segments > 0 {
let (segment, action) = if short_segments == 1 {
("segment", "was left out of tracks; its point remains in track points")
} else {
("segments", "were left out of tracks; their points remain in track points")
};
report.messages.push(format!("{name}: {short_segments} GPX track {segment} with fewer than two usable points {action}"));
}
report.skipped = skipped;
if skipped > 0 {
report.messages.push(format!("{name}: {skipped} points, routes or tracks without usable coordinates were skipped"));
report.messages.push(format!("{name}: {skipped} points, routes or tracks without usable geometry were skipped"));
}
}
if out.is_empty() {
Expand All @@ -202,6 +213,26 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
mod tests {
use super::*;

#[test]
fn xml_loss_short_segments_are_counted_but_their_points_remain() {
let xml = br#"<gpx><trk><name>kept</name><trkseg><trkpt lat="1" lon="2"/><trkpt lat="3" lon="4"/></trkseg><trkseg><trkpt lat="5" lon="6"/></trkseg><trkseg/></trk></gpx>"#;
let layers = read("a.gpx", xml).unwrap();
assert_eq!(layers.len(), 2);
assert_eq!(layers[0].0.features[0].geometry, Some(Geometry::Line(vec![vec![Coord::new(2.0, 1.0), Coord::new(4.0, 3.0)]])));
assert_eq!(layers[0].0.value(0, 0), &Value::Text("kept".into()));
assert_eq!(layers[1].0.features.len(), 3);
assert_eq!(layers[1].0.features[2].geometry, Some(Geometry::Point(Coord::new(6.0, 5.0))));
assert_eq!(layers[1].0.value(2, 1), &Value::Int(2));
assert_eq!(
layers[0].1.messages,
["a: 1 GPX track segment with fewer than two usable points was left out of tracks; its point remains in track points"]
);
assert!(layers[1].1.messages.is_empty());
let clean =
read("a.gpx", br#"<gpx><trk><trkseg><trkpt lat="1" lon="2"/><trkpt lat="3" lon="4"/></trkseg><trkseg/></trk></gpx>"#).unwrap();
assert!(clean.iter().all(|(_, r)| r.messages.is_empty()));
}

const GPX: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="test" xmlns="http://www.topografix.com/GPX/1/1">
<wpt lat="48.85" lon="2.35"><ele>35</ele><name>Paris</name><sym>Flag</sym></wpt>
Expand Down
Loading
Loading