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
107 changes: 103 additions & 4 deletions crates/data/src/loss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,40 @@ pub struct LossMessage {
pub struct Note {
pub text: String,
pub loss: Option<LossCode>,
/// What the note counts, for code that groups or adds up notes: it reads this, never the
/// words, which may be reworded or translated. Boxed, so a note without one stays small.
pub tally: Option<Box<Tally>>,
}

impl Note {
/// A note that reports a loss.
pub fn loss(code: LossCode, text: impl Into<String>) -> Self {
Self { text: text.into(), loss: Some(code) }
Self { text: text.into(), loss: Some(code), tally: None }
}

/// A note that reports no loss.
pub fn plain(text: impl Into<String>) -> Self {
Self { text: text.into(), loss: None }
Self { text: text.into(), loss: None, tally: None }
}

/// A note that says `tally` in its own words ([`Tally::text`]), with the loss it reports, if any.
pub fn tallied(loss: Option<LossCode>, tally: Tally) -> Self {
Self { text: tally.text(), loss, tally: Some(Box::new(tally)) }
}

/// The same note, counting `tally`.
pub fn with_tally(self, tally: Tally) -> Self {
Self { tally: Some(Box::new(tally)), ..self }
}

/// The same note, its text after `prefix: ` (the layer or file it is about).
pub fn prefixed(self, prefix: &str) -> Self {
Self { text: format!("{prefix}: {}", self.text), loss: self.loss }
Self { text: format!("{prefix}: {}", self.text), ..self }
}

/// The same note, its text changed by `f` (a qualifier added).
pub fn map_text(self, f: impl FnOnce(String) -> String) -> Self {
Self { text: f(self.text), loss: self.loss }
Self { text: f(self.text), ..self }
}

pub fn as_str(&self) -> &str {
Expand Down Expand Up @@ -267,6 +280,92 @@ impl PartialEq<String> for Note {
}
}

/// Who found what a [`Tally`] counts.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Finder {
/// GDAL's conversion of a file RustGIS doesn't read itself.
Gdal,
/// DuckDB's conversion of a table.
Duckdb,
/// RustGIS's own check.
Rustgis,
}

impl Finder {
fn prefix(self) -> &'static str {
match self {
Self::Gdal => "GDAL: ",
Self::Duckdb => "DuckDB: ",
Self::Rustgis => "",
}
}
}

/// What a [`Tally`] counts.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Counted {
/// Features or rows of another geometry type than the layer's, left out.
OtherGeometryType,
/// Rows without a geometry, left out.
RowsWithoutGeometry,
/// Features whose Z or M values were dropped.
ZmDropped,
/// Rows that couldn't be read while checking a replacement.
UnreadableRows,
/// A layer with no declared coordinate system, left unprojected: its count is 1.
NoDeclaredCrs,
/// Integer overflow warnings, whatever value overflowed.
IntegerOverflow,
/// Times a converting program gave this warning, in its own words.
Warning(String),
}

/// What a note counts, how many and who found it: what notes from several layers are grouped and
/// added up by, so that rewording a note changes no decision or total.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Tally {
pub by: Finder,
pub counted: Counted,
/// How many, or `None` where the number isn't known.
pub count: Option<u64>,
}

impl Tally {
pub fn new(by: Finder, counted: Counted, count: Option<u64>) -> Self {
Self { by, counted, count }
}

/// What this count says, naming no layer, so that it also says a total over several.
pub fn text(&self) -> String {
let n = self.count.map_or_else(|| "an unknown number of".to_string(), |n| n.to_string());
let what = match (&self.counted, self.by) {
(Counted::OtherGeometryType, Finder::Duckdb) => match self.count {
Some(_) => format!("{n} rows of another geometry type were left out"),
None => format!("{n} rows of another geometry type may have been left out"),
},
(Counted::OtherGeometryType, _) => match self.count {
Some(_) => format!("{n} features of another geometry type were left out"),
None => format!("{n} features of another geometry type may have been left out"),
},
(Counted::RowsWithoutGeometry, _) => match self.count {
Some(_) => format!("{n} rows without a geometry were left out"),
None => format!("{n} rows without a geometry may have been left out"),
},
(Counted::ZmDropped, Finder::Gdal) => match self.count {
Some(_) => format!("{n} source feature(s) had Z or M coordinates; conversion keeps only XY coordinates"),
None => format!("{n} source feature(s) may have had Z or M coordinates dropped; conversion keeps only XY coordinates"),
},
(Counted::ZmDropped, _) => crate::zm_note(self.count),
(Counted::UnreadableRows, _) => format!("{n} rows could not be read while checking the replacement"),
// Said of each layer that has none, so with no count.
(Counted::NoDeclaredCrs, _) => "no declared coordinate system; the coordinates were left unprojected".to_string(),
(Counted::IntegerOverflow, _) => format!("{n} integer overflow warning(s)"),
(Counted::Warning(line), _) => format!("{line} ({n} occurrence(s))"),
};
format!("{}{what}", self.by.prefix())
}
}

/// What a reader, a source or a write says, in order: plain text pushed onto it stays plain, and
/// a [`Note::loss`] keeps its code.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
Expand Down
56 changes: 39 additions & 17 deletions crates/data/src/source/gdal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,13 @@ fn convert_one_fresh_in(dir: &Path, src: &Path, layer: &str, family: Option<Fami
)?
.ok_or_else(|| DataError::Format(format!("layer '{layer}' has no compatible declared geometry")))?;
if converted.unprojected {
converted.notes.push(crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
format!("GDAL: '{layer}' has no declared coordinate system; its coordinates were left unprojected"),
));
converted.notes.push(
crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
format!("GDAL: '{layer}' has no declared coordinate system; its coordinates were left unprojected"),
)
.with_tally(crate::loss::Tally::new(crate::loss::Finder::Gdal, crate::loss::Counted::NoDeclaredCrs, Some(1))),
);
}
if converted.in_lonlat {
converted.notes.push(in_lonlat_note(layer));
Expand Down Expand Up @@ -510,12 +513,19 @@ fn convert_one_fresh_in(dir: &Path, src: &Path, layer: &str, family: Option<Fami
Some(0) => {}
Some(count) => {
zm_dropped = true;
zm_note = Some(crate::loss::Note::loss(
crate::loss::LossCode::DroppedZm,
format!(
"GDAL: '{layer}' contains Z or M coordinates on {count} source feature(s); conversion keeps only XY coordinates"
),
));
zm_note = Some(
crate::loss::Note::loss(
crate::loss::LossCode::DroppedZm,
format!(
"GDAL: '{layer}' contains Z or M coordinates on {count} source feature(s); conversion keeps only XY coordinates"
),
)
.with_tally(crate::loss::Tally::new(
crate::loss::Finder::Gdal,
crate::loss::Counted::ZmDropped,
Some(count),
)),
);
}
None => {
// A layer that declares them may still hold only x and y: said without a code, as
Expand All @@ -524,7 +534,8 @@ fn convert_one_fresh_in(dir: &Path, src: &Path, layer: &str, family: Option<Fami
zm_dropped = info.has_zm;
zm_note = Some(crate::loss::Note::plain(format!(
"GDAL: '{layer}': an unknown number of source feature(s) may have had Z or M coordinates dropped; conversion keeps only XY coordinates"
)));
))
.with_tally(crate::loss::Tally::new(crate::loss::Finder::Gdal, crate::loss::Counted::ZmDropped, None)));
}
}
}
Expand Down Expand Up @@ -728,18 +739,20 @@ fn convert_one_in(dir: &Path, src: &Path, layer: &str, request: ConversionReques
let count = counts.entry(line.to_string()).or_default();
*count = count.saturating_add(1);
}
let mut notes: crate::loss::Notes =
counts.into_iter().map(|(line, count)| format!("GDAL: {line} ({count} occurrence(s))")).collect();
let mut notes: crate::loss::Notes = counts.into_iter().map(|(line, count)| gdal_warning(line, count)).collect();
// A layer declaring one geometry type holds no other; only a mixed layer is counted. A
// count that fails doesn't undo a good conversion: the possible loss is said instead.
if let Some(family) = family.filter(|_| kind.is_none()) {
match count_other_families(&full, layer, family) {
Ok(0) => {}
Ok(other) => notes.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedFeature,
format!("GDAL: {other} features of another geometry type were left out"),
Ok(other) => notes.push(crate::loss::Note::tallied(
Some(crate::loss::LossCode::DroppedFeature),
crate::loss::Tally::new(crate::loss::Finder::Gdal, crate::loss::Counted::OtherGeometryType, Some(other)),
)),
Err(_) => notes.push(crate::loss::Note::tallied(

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.

The unknown-count branch carries no loss code (None), while the known-count branch above carries DroppedFeature. conversion_notices keys on the tally and the code, so for one file where layer A has a known count and layer B an unknown one, the opening summary says two notices for one kind (GDAL: 3 features ... were left out and GDAL: an unknown number of features ... may have been left out) and never the single unknown total the PR describes ("a total is unknown when any layer's count is"). It isn't silent, since both are said, but it doesn't match the stated grouping.

The test grouped_conversion_losses_keep_unknown_totals_in_either_order (project.rs) passes ConversionLoss::add directly with hand-set codes, so it never reaches this key split and wouldn't catch it. Two options: give this branch the same DroppedFeature code (features may be dropped, so the code is arguably right here, but it changes the single-layer notice's code), or keep the split and say so in the PR. Either way, a test through conversion_notices with one known and one unknown layer would pin it down.

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 05ebd18. Tallied notes are grouped by what they count alone, not their code, so a known and an unknown count of one kind are one notice, its total unknown. The group keeps the code any of its notes carries (dropped.feature from the known count), since that loss is in it. The single-layer notices keep their own codes. The test now goes through conversion_notices, in both orders: a_known_and_an_unknown_count_of_one_kind_are_one_notice_in_either_order.

None,
crate::loss::Tally::new(crate::loss::Finder::Gdal, crate::loss::Counted::OtherGeometryType, None),
)),
Err(_) => notes.push("GDAL: an unknown number of features of another geometry type may have been left out"),
}
}
notes
Expand Down Expand Up @@ -792,6 +805,15 @@ fn placement(fgb: &Path) -> Placement {
})
}

/// A warning GDAL gave `count` times while converting, in its words. Its integer overflows,
/// whatever the value, are counted as one kind, so several layers' are said once.
fn gdal_warning(line: String, count: u64) -> crate::loss::Note {
use crate::loss::{Counted, Finder, Note, Tally};
let text = format!("GDAL: {line} ({count} occurrence(s))");
let counted = if line.to_ascii_lowercase().contains("integer overflow") { Counted::IntegerOverflow } else { Counted::Warning(line) };
Note::plain(text).with_tally(Tally::new(Finder::Gdal, counted, Some(count)))
}

/// The notice for a layer converted to longitude/latitude because its system can't be read.
pub fn in_lonlat_note(layer: &str) -> String {
in_lonlat_notes(&[layer])
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub(crate) fn copy_notices_coded(copy: &mut Session, path: &str) -> Vec<(rustgis
rustgis_data::loss::Note::plain(format!("{path}: {earlier} earlier notices from drawing it aren't shown")),
));
}
said.extend(kept.into_iter().map(|n| (n.severity, rustgis_data::loss::Note { loss: n.loss(), text: n.text })));
said.extend(kept.into_iter().map(|n| (n.severity, rustgis_data::loss::Note { loss: n.loss(), text: n.text, tally: None })));
said
}

Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result<super::exports::Work
let mark = s.notice_mark();
super::exports::say_coded(
s,
other.into_iter().map(|n| (n.severity, rustgis_data::loss::Note { loss: n.loss(), text: n.text })).collect(),
other.into_iter().map(|n| (n.severity, rustgis_data::loss::Note { loss: n.loss(), text: n.text, tally: None })).collect(),
);
for note in notes.into_iter().chain(construction_notes) {
s.say(note);
Expand Down
Loading
Loading