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
8 changes: 3 additions & 5 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ The shared pieces the everyday workflow stands on.
### Correct results
A result is right, or RustGIS says what it lost or assumed
([AGENTS.md](AGENTS.md#never-silent)).
- 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; DXF polyface faces with missing vertices and unreadable supplied Z outside 3D POLYLINE vertices; 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 MapInfo,
and attribute fields/values in XLSB short-cell workbooks (the original SheetJS 0.18.5 clean
- Known silent losses: DXF polyface faces with missing vertices and unreadable supplied Z outside 3D POLYLINE vertices; and the text
reader loss listed as `Silent` in `crates/engine/tests/integration/never_silent_reading.rs`:
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
its case `Said` or `Imported` _(open)_.
- Lossy sample files in `never_silent_reading.rs` for the readers it doesn't cover yet:
Expand Down
152 changes: 144 additions & 8 deletions crates/data/src/mapinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,25 @@ fn area(r: &[Coord]) -> f64 {

/// A region's rings as polygons: a ring inside another (an odd number of others) is a hole.
pub(crate) fn region(rings: Vec<Vec<Coord>>) -> Option<Geometry> {
region_counted(rings, &mut 0)
}

/// Counts rings at the closure/filter seam, including empty rings, without changing nesting.
fn region_counted(rings: Vec<Vec<Coord>>, short: &mut usize) -> Option<Geometry> {
let mut rings: Vec<Vec<Coord>> = rings
.into_iter()
.filter_map(|mut r| {
if r.first() != r.last() {
r.push(*r.first()?);
if let Some(&first) = r.first()
&& r.last() != Some(&first)
{
r.push(first);
}
if r.len() < 4 {
*short += 1;
None
} else {
Some(r)
}
(r.len() >= 4).then_some(r)
})
.collect();
// Past this many rings, finding holes would take too long: each ring is a polygon.
Expand Down Expand Up @@ -284,6 +296,7 @@ pub fn read(mif: &str, mid: Option<&str>, name: &str) -> Result<(Dataset, ReadRe
(0..n).map(|k| Some(Coord::new(num(j + 2 * k)?, num(j + 2 * k + 1)?))).collect()
};
let mut error = None;
let (mut arcs, mut texts, mut short_parts, mut short_rings, mut empty_objects) = (0usize, 0usize, 0usize, 0usize, 0usize);
while i < w.len() {
let kw = up(i);
if !OBJECTS.contains(&kw.as_str()) {
Expand Down Expand Up @@ -311,7 +324,13 @@ pub fn read(mif: &str, mid: Option<&str>, name: &str) -> Result<(Dataset, ReadRe
parts.push(pts(j + 1, n)?);
j += 1 + 2 * n;
}
parts.retain(|p| p.len() >= 2);
let before = short_parts;
parts.retain(|p| {
let keep = p.len() >= 2;
short_parts += usize::from(!keep);
keep
});
empty_objects += usize::from(parts.is_empty() && short_parts > before);
Some(((!parts.is_empty()).then_some(Geometry::Line(parts)), j))
})(),
"REGION" => (|| {
Expand All @@ -326,7 +345,10 @@ pub fn read(mif: &str, mid: Option<&str>, name: &str) -> Result<(Dataset, ReadRe
rings.push(pts(j + 1, n)?);
j += 1 + 2 * n;
}
Some((region(rings), j))
let before = short_rings;
let geometry = region_counted(rings, &mut short_rings);
empty_objects += usize::from(geometry.is_none() && short_rings > before);
Some((geometry, j))
})(),
"RECT" | "ROUNDRECT" => (|| {
let (x1, y1, x2, y2) = (num(i + 1)?, num(i + 2)?, num(i + 3)?, num(i + 4)?);
Expand All @@ -335,8 +357,14 @@ pub fn read(mif: &str, mid: Option<&str>, name: &str) -> Result<(Dataset, ReadRe
})(),
"ELLIPSE" => (|| Some((Some(ellipse(num(i + 1)?, num(i + 2)?, num(i + 3)?, num(i + 4)?)), i + 5)))(),
// Kept as rows without geometry.
"ARC" => Some((None, i + 7)),
"TEXT" => Some((None, i + 6)),
"ARC" => {
arcs += 1;
Some((None, i + 7))
}
"TEXT" => {
texts += 1;
Some((None, i + 6))
}
_ => None,
};
match r {
Expand All @@ -350,6 +378,36 @@ pub fn read(mif: &str, mid: Option<&str>, name: &str) -> Result<(Dataset, ReadRe
}
}
}
for (count, one, many) in [
(arcs, "MapInfo arc object was kept as a row without geometry", "MapInfo arc objects were kept as rows without geometry"),
(
texts,
"MapInfo text object was kept as a row without its position or text",
"MapInfo text objects were kept as rows without their position or text",
),
(
short_parts,
"MapInfo line part with fewer than two coordinates was omitted",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When every part of a multi-part Pline is short, parts is empty and the object is kept as a row without geometry (then_some at line 332). This notice then says only that a part was omitted, so it does not tell the user the whole line went. It would be clearer to say the object was kept without geometry in that case, or to count it the way the ARC and TEXT notices do. A test with a single one-point part would pin the wording.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in eb9c620 for both Pline and Region. A separate counted notice says when all omitted short parts leave the object as an attribute row without geometry. The tests check retained MID values, row alignment, skipped count and exact notices.

"MapInfo line parts with fewer than two coordinates were omitted",
),
(
short_rings,
"MapInfo polygon ring with fewer than four coordinates after closure was omitted",
"MapInfo polygon rings with fewer than four coordinates after closure were omitted",
),
] {
if count > 0 {
let subject = if count == 1 { one } else { many };
report.messages.push(format!("{count} {subject}"));
}
}
if empty_objects > 0 {
let subject = if empty_objects == 1 { "object was kept as a row" } else { "objects were kept as rows" };
let possessive = if empty_objects == 1 { "its" } else { "their" };
report
.messages
.push(format!("{empty_objects} MapInfo {subject} without geometry because all {possessive} parts or rings were too short"));
}
if let Some(e) = error {
report.messages.push(e);
}
Expand Down Expand Up @@ -424,6 +482,84 @@ pub fn read(mif: &str, mid: Option<&str>, name: &str) -> Result<(Dataset, ReadRe
mod tests {
use super::*;

/// The retained object and MID row stay aligned when geometry or one component is omitted.
#[test]
fn omitted_geometry_is_said() {
let cases = [
(
"Arc 0 0 2 2 0 90",
"1 MapInfo arc object was kept as a row without geometry",
"2 MapInfo arc objects were kept as rows without geometry",
None,
),
(
"Text \"Label\" 0 0 1 1",
"1 MapInfo text object was kept as a row without its position or text",
"2 MapInfo text objects were kept as rows without their position or text",
None,
),
(
"Pline Multiple 2 2 0 0 1 1 1 5 5",
"1 MapInfo line part with fewer than two coordinates was omitted",
"2 MapInfo line parts with fewer than two coordinates were omitted",
Some(Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(1.0, 1.0)]])),
),
(
"Region 2 4 0 0 4 0 4 4 0 0 2 1 1 2 2",
"1 MapInfo polygon ring with fewer than four coordinates after closure was omitted",
"2 MapInfo polygon rings with fewer than four coordinates after closure were omitted",
Some(Geometry::Polygon(vec![vec![vec![
Coord::new(0.0, 0.0),
Coord::new(4.0, 0.0),
Coord::new(4.0, 4.0),
Coord::new(0.0, 0.0),
]]])),
),
];
for (object, message, repeated_message, geometry) in cases {
let mif = format!("Version 300\nCoordSys Earth Projection 1, 104\nColumns 1\nid Integer\nData\n{object}\nPoint 9 9\n");
let (ds, report) = read(&mif, Some("7\n8\n"), "parts").unwrap();
assert_eq!(ds.features.len(), 2);
assert_eq!(ds.features[0].geometry, geometry);
assert_eq!(ds.features[0].attrs, vec![Value::Int(7)]);
assert_eq!(ds.features[1].geometry, Some(Geometry::Point(Coord::new(9.0, 9.0))));
assert_eq!(ds.features[1].attrs, vec![Value::Int(8)]);
assert_eq!(report.skipped, 0);
assert_eq!(report.messages, [message]);
let mif = format!("Columns 1\nid Integer\nData\n{object}\n{object}\n");
let (twice, report) = read(&mif, Some("7\n8\n"), "twice").unwrap();
assert_eq!(twice.features.len(), 2);
assert_eq!(twice.features[0].geometry, geometry);
assert_eq!(twice.features[1].geometry, geometry);
assert_eq!(report.messages, [repeated_message]);
}
let (_, clean) =
read("Columns 1\nid Integer\nData\nPline 2 0 0 1 1\nRegion 1 3 0 0 4 0 4 4\nNone", Some("1\n2\n3"), "clean").unwrap();
assert!(clean.messages.is_empty(), "{clean:?}");
}

/// A row stays aligned even when all of its short line parts or polygon rings disappear.
#[test]
fn all_short_parts_keep_rows_and_report_missing_geometry() {
for (object, part_note) in [
("Pline Multiple 1 1 1 1", "1 MapInfo line part with fewer than two coordinates was omitted"),
("Region 1 2 0 0 1 1", "1 MapInfo polygon ring with fewer than four coordinates after closure was omitted"),
] {
let mif = format!("Version 300\nCoordSys Earth Projection 1, 104\nColumns 1\nid Integer\nData\n{object}\nPoint 9 9\n");
let (ds, report) = read(&mif, Some("7\n8\n"), "parts").unwrap();
assert_eq!(ds.features.len(), 2);
assert!(ds.features[0].geometry.is_none());
assert_eq!(ds.features[0].attrs, [Value::Int(7)]);
assert_eq!(ds.features[1].geometry, Some(Geometry::Point(Coord::new(9., 9.))));
assert_eq!(ds.features[1].attrs, [Value::Int(8)]);
assert_eq!(report.skipped, 0);
assert_eq!(
report.messages,
[part_note, "1 MapInfo object was kept as a row without geometry because all its parts or rings were too short",]
);
}
}

const MIF: &str = "Version 300\nCharset \"WindowsLatin1\"\nDelimiter \",\"\nCoordSys Earth Projection 1, 104\nColumns 3\n name Char(20)\n pop Integer\n area Decimal(10,2)\nData\n\nPoint 10 20\n Symbol (35,0,12)\nPline 3\n10 20\n11 21\n12 20\n Pen (1,2,0)\nRegion 2\n 5\n0 0\n10 0\n10 10\n0 10\n0 0\n 4\n2 2\n4 2\n3 4\n2 2\n Pen (1,2,0)\n Brush (2,16777215,16777215)\n Center 5 5\nPline Multiple 2\n 2\n0 0\n1 1\n 2\n2 2\n3 3\nText \"Hello world\"\n 1 1 2 2\nNone\nRect 0 0 1 1\n";
const MID: &str = "\"A\",1,1.5\n\"B, c\",2,2.5\n\"C\",3,3.5\n\"D\",4,4.5\n\"E\",5,5.5\n\"F\",6,6.5\n\"G\",7,7.5\n";

Expand Down Expand Up @@ -480,7 +616,7 @@ mod tests {
assert!(matches!(&ds.features[3].geometry, Some(Geometry::Line(l)) if l.len() == 2));
assert!(ds.features[4].geometry.is_none() && ds.features[5].geometry.is_none(), "text and none");
assert!(matches!(&ds.features[6].geometry, Some(Geometry::Polygon(_))));
assert!(rep.messages.is_empty(), "{:?}", rep.messages);
assert_eq!(rep.messages, ["1 MapInfo text object was kept as a row without its position or text"]);
}

#[test]
Expand Down
Loading
Loading