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
5 changes: 3 additions & 2 deletions crates/data/src/raster/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ pub(super) fn read_with_nodata(
r.lost.extend(super::planetary::note_of(crs));
if assumed {
r.crs_assumed = true;
r.lost.push(super::assumed_wgs84_note(
crs_issue.unwrap_or("it doesn't say its coordinate system (a .prj file next to an ASCII grid does)"),
r.lost.push(crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
super::assumed_wgs84_note(crs_issue.unwrap_or("it doesn't say its coordinate system (a .prj file next to an ASCII grid does)")),
));
}
if rounded > 0 {
Expand Down
13 changes: 8 additions & 5 deletions crates/data/src/raster/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub struct Layout {
/// What the header holds but isn't read: a colour table or GDAL metadata that doesn't decode,
/// a colour table too short for its pixels. One line each, for a window to say (AGENTS.md,
/// Never silent); drawing in place shows the file grey without them, as before.
pub lost: Vec<String>,
pub lost: crate::loss::Notes,
/// Who holds the rights to the picture (its Copyright tag), as [`super::Raster::copyright`].
pub copyright: Option<String>,
}
Expand Down Expand Up @@ -394,10 +394,10 @@ fn read_layout(r: Sparse) -> Result<Parse> {
Err(e) => Ok(Err(e.to_string())),
};
// Another body's system is said, as when the file is read whole.
let mut lost: Vec<String> = super::planetary::note_of(geo.0).into_iter().collect();
let mut lost: crate::loss::Notes = super::planetary::note_of(geo.0).into_iter().collect();
// A datum assumed, and the file's copyright, as when it is read whole.
if geo.3 {
lost.push(super::geotiff::NO_EPSG_GEOGRAPHIC.to_string());
lost.push(crate::loss::Note::loss(crate::loss::LossCode::AssumedCrs, super::geotiff::NO_EPSG_GEOGRAPHIC));
}
// Its bytes asked for when not read yet, as the other tags' are.
let copyright = match tag(&mut dec, Tag::Copyright)? {
Expand Down Expand Up @@ -1151,7 +1151,10 @@ pub(crate) mod tests {
let clean = layout_of(example(64, 32));
assert!(clean.lost.is_empty() && clean.copyright.is_none(), "{:?}", clean.lost);
let assumed = layout_of(example_with(&Example { size: 64, tile: 32, no_epsg: true, ..Example::default() }));
assert_eq!(assumed.lost, vec![super::super::geotiff::NO_EPSG_GEOGRAPHIC.to_string()]);
assert_eq!(
assumed.lost.iter().map(|n| (n.text.as_str(), n.loss)).collect::<Vec<_>>(),
[(super::super::geotiff::NO_EPSG_GEOGRAPHIC, Some(crate::loss::LossCode::AssumedCrs))]
);
let mut r = crate::raster::Raster::new(
"credited",
64,
Expand Down Expand Up @@ -1376,7 +1379,7 @@ pub(crate) mod tests {
let entry = (0..n).map(|k| 10 + k * 12).find(|&e| bad[e..e + 2] == [0x80, 0xA4]).unwrap();
bad[entry + 8..entry + 12].copy_from_slice(&(len + 1000).to_le_bytes());
let Parse::Done(l) = layout(&Sparse::default().with_len(u64::from(len)).with(0, bad)).unwrap() else { panic!("not read") };
let said: Vec<&String> = l.lost.iter().chain(l.described.iter().flat_map(|d| d.lost.iter())).collect();
let said: Vec<&str> = l.lost.texts().chain(l.described.iter().flat_map(|d| d.lost.texts())).collect();
assert!(said.iter().any(|x| x.contains("band and class names and wavelengths are left out")), "{said:?}");
}

Expand Down
47 changes: 39 additions & 8 deletions crates/data/src/raster/dted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn read_from_limited(file_name: &str, mut source: impl std::io::Read, max: usize
if east - west > 1.0 + 1e-9 || north - south > 1.0 + 1e-9 || north > 90.0 + 1e-9 || east > 180.0 + 1e-9 {
return Err(bad("its posts span more than the one-degree cell a DTED file holds"));
}
let mut lost = Vec::new();
let mut lost = crate::loss::Notes::new();
// What the DSI says differently (3.12 d), and what the spec sets for the level (3.9.2).
let dsi = [
(digits(bytes, DSI + 273, 4), lat_tenths, "latitude interval"),
Expand Down Expand Up @@ -326,10 +326,15 @@ fn read_from_limited(file_name: &str, mut source: impl std::io::Read, max: usize
}
match field(bytes, DSI + 144, 5) {
Some("WGS84") => {}
// A datum it names but isn't read: its places are taken on WGS 84, without the shift.
Some(other) if !other.trim().is_empty() => {
lost.push(format!("its horizontal datum is {:?}, not WGS84; it is read as WGS 84", other.trim()));
lost.push(crate::loss::Note::loss(
crate::loss::LossCode::ApproximatedDatum,
format!("its horizontal datum is {:?}, not WGS84; it is read as WGS 84", other.trim()),
));
}
_ => lost.push("it doesn't name its horizontal datum; it is read as WGS 84, as MIL-PRF-89020B sets".into()),
// None named: the standard sets WGS 84, so nothing is assumed past it.
_ => lost.push("it doesn't name its horizontal datum; it is read as WGS 84, as MIL-PRF-89020B sets"),

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.

This is the same case the PR codes as assumed.crs elsewhere: the file gives no system, so it is read as WGS 84 (ASCII grids, GeoTIFF, COG and Zarr all code "doesn't say its coordinate system" as AssumedCrs). Here it's plain text, so a --strict run, Python's warnings and MCP's losses accept a DTED file read on an assumed datum. The PR lists "DTED's datum" under "No code fits these yet", but LossCode::AssumedCrs fits it, and the line above (a datum that is named but not WGS84) has the same problem. Lower confidence than the recursion above: the PR chose this on purpose, so the author should either code both DTED datum notes or say why this case differs.

Category: bug

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.

Coded in d31d8ff, half of it. A datum the file names other than WGS84 is read as WGS 84 without its shift, so it carries approximated.datum. A file that names none is read as WGS 84 because MIL-PRF-89020B sets that, so nothing is assumed past the standard, and it stays plain. A new test covers both, and a file that names WGS84.

}
let surface = match field(bytes, DSI + 141, 3) {
Some("E96") => "the EGM96 geoid (mean sea level)".to_string(),
Expand Down Expand Up @@ -451,12 +456,19 @@ fn read_from_limited(file_name: &str, mut source: impl std::io::Read, max: usize
}
let count = |n: usize| if n == 1 { format!("1 of its {cols} columns has") } else { format!("{n} of its {cols} columns have") };
if present < cols {
lost.push(format!("the file is cut short: {} no heights, read as no data", count(cols - present)));
// Heights the file should hold and doesn't: lost.
lost.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedValue,

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 registry's action text for dropped.value is written for a write: "Correct the value or use a destination field that can hold it." Here it marks heights read as no data in a cut-short or damaged DTED file, where that advice doesn't fit. The action is shown to users: --strict prints it (apps/rustgis-cli/src/main.rs:187), and the Assistant's losses carry it (crates/assistant/src/tools.rs:906).

The same gap applies to the other codes this PR puts on reads:

  • approximated.datum (line 332): "Export in the layer's own coordinate system…"
  • dropped.crs-name (Zarr proj: keys): "Keep the source file's coordinate system definition with the export…"
  • changed.cut (sidecar names): advice about Shapefile byte limits.

The data is kept correctly. The advice text in crates/data/src/loss.rs and docs/cli.md describes writes only. Either add read-side wording, or say in the registry that these codes also cover reads.

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.

Left for the release quality pass.

format!("the file is cut short: {} no heights, read as no data", count(cols - present)),
));
} else if let Some(extra) = body_len.checked_sub((cols * record) as u64).filter(|n| *n > 0) {
lost.push(format!("{extra} bytes after its last column are ignored"));
}
if damaged > 0 {
lost.push(format!("{} a damaged record header (not the 0xAA sentinel), read as no data", count(damaged)));
lost.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedValue,
format!("{} a damaged record header (not the 0xAA sentinel), read as no data", count(damaged)),
));
}
match misnumbered {
0 => {}
Expand All @@ -477,8 +489,7 @@ fn read_from_limited(file_name: &str, mut source: impl std::io::Read, max: usize
match twos {
0 => {}
1 => lost.push(
"1 of its heights was written in two's complement, not the signed magnitude MIL-PRF-89020B sets; it is read as two's complement"
.into(),
"1 of its heights was written in two's complement, not the signed magnitude MIL-PRF-89020B sets; it is read as two's complement",
),
n => lost.push(format!(
"{n} of its heights were written in two's complement, not the signed magnitude MIL-PRF-89020B sets; they are read as two's complement"
Expand Down Expand Up @@ -604,6 +615,21 @@ mod tests {
assert!(!r.lost.iter().any(|l| l.contains("checksum")), "{:?}", r.lost);
}

/// A datum the file names that isn't WGS84 is read as WGS 84, without its shift, and said with
/// its code; one the file doesn't name is WGS 84 as the standard sets, said plain.
#[test]
fn a_datum_named_but_not_read_is_coded() {
let b = dted("0000000E", "0000000N", (30, 30), (11, 11), |_, _| Some(1));
let datum = |name: &[u8; 5]| {
let mut b = b.clone();
b[DSI + 144..DSI + 149].copy_from_slice(name);
read("x.dt1", &b).unwrap().lost.iter().filter(|l| l.contains("horizontal datum")).map(|l| l.loss).collect::<Vec<_>>()
};
assert_eq!(datum(b"NAD27"), [Some(crate::loss::LossCode::ApproximatedDatum)]);
assert_eq!(datum(b" "), [None]);
assert!(datum(b"WGS84").is_empty());
}

#[test]
fn the_dsi_names_the_level() {
// 11 × 11 posts of 3″ is no level's spacing: the notice names the level checked against.
Expand Down Expand Up @@ -751,7 +777,12 @@ mod tests {
let cut = &bytes[..at(8, rows) + 7];
let r = read("x.dt2", cut).unwrap();
assert_eq!(r.value(0, 8, 0), None);
assert!(r.lost.iter().any(|l| l == "the file is cut short: 3 of its 11 columns have no heights, read as no data"), "{:?}", r.lost);
assert!(
r.lost.iter().any(|l| l.text == "the file is cut short: 3 of its 11 columns have no heights, read as no data"
&& l.loss == Some(crate::loss::LossCode::DroppedValue)),
"{:?}",
r.lost
);
// Bytes past the last column are said.
let mut longer = dted("0000000E", "0000000N", (3600, 3600), (11, rows), |_, _| Some(1));
longer.extend([0; 5]);
Expand Down
31 changes: 19 additions & 12 deletions crates/data/src/raster/geotiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2070,19 +2070,26 @@ fn read_image(
return Err(DataError::Format(e.clone()));
}
if let Some(asked) = asked.filter(|_| geo.custom_error.is_none() && !geo.geographic_model && !degrees) {
assumed = Some(format!("it doesn't say its coordinate system: read in {}, the system it was asked for in", asked.label()));
// Read in the system asked for: nothing assumed past the request.
assumed = Some(crate::loss::Note::plain(format!(
"it doesn't say its coordinate system: read in {}, the system it was asked for in",
asked.label()
)));
asked
} else if geo.geographic_model || degrees {
assumed = Some(match &geo.custom_error {
// The file's own projection couldn't be read, but its coordinates fit in degrees:
// why is said, not dropped.
Some(e) => super::assumed_wgs84_note(&format!(
"its own coordinate system can't be read ({})",
e.strip_prefix("GeoTIFF: ").unwrap_or(e)
)),
None if geo.geographic_model => NO_EPSG_GEOGRAPHIC.to_string(),
None => super::assumed_wgs84_note("it doesn't say its coordinate system"),
});
assumed = Some(crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
match &geo.custom_error {
// The file's own projection couldn't be read, but its coordinates fit in degrees:
// why is said, not dropped.
Some(e) => super::assumed_wgs84_note(&format!(
"its own coordinate system can't be read ({})",
e.strip_prefix("GeoTIFF: ").unwrap_or(e)
)),
None if geo.geographic_model => NO_EPSG_GEOGRAPHIC.to_string(),
None => super::assumed_wgs84_note("it doesn't say its coordinate system"),
},
));
Crs::LonLat
} else {
return Err(err("the file's coordinate system isn't recognised"));
Expand Down Expand Up @@ -3060,7 +3067,7 @@ mod tests {
let rotated = [30.0, 5.0, 0.0, 400_000.0, 3.0, -30.0, 0.0, 5_000_000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0];
let r = read(std::io::Cursor::new(tests_support::matrix_tiff(rotated, false)), "rot").unwrap();
assert_eq!(r.lost.len(), 1, "{:?}", r.lost);
let said = &r.lost[0];
let said = &r.lost[0].text;
assert!(said.starts_with("it is rotated or skewed, so it is resampled (nearest pixel) onto a north-up grid of "), "{said}");
assert!(said.contains(&format!("{} × {} pixels of 30.25 each", r.width, r.height)), "{said}");
assert!(!said.contains("coarser"), "{said}");
Expand Down
38 changes: 24 additions & 14 deletions crates/data/src/raster/grib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ fn read_limited<R: Read + Seek>(mut source: R, name: &str, sidecar_nodata: Optio
return Err(bad(if skipped.cut > 0 { "the first message is cut short" } else { "no GRIB message in the file" }));
}
if fields.is_empty() {
return Err(bad(format!("nothing in it can be read here: {}", skipped.notes(limit).join("; "))));
return Err(bad(format!(
"nothing in it can be read here: {}",
skipped.notes(limit).iter().map(|n| n.as_str()).collect::<Vec<_>>().join("; ")
)));
}
// Each grid's notices, once: grids at the same places may still say different things (one
// laid out from BAM's latitudes, one not). Past [`MAX_REASONS`] of them, the fields with more
Expand Down Expand Up @@ -505,35 +508,39 @@ impl Skipped {
tally(&mut self.damage, why);
}

fn notes(&self, limit: usize) -> Vec<String> {
let mut out: Vec<String> = self.reasons.iter().map(|(r, n)| format!("{} left out: {r}, not read yet", count(*n))).collect();
out.extend(self.damage.iter().map(|(r, n)| format!("{} left out: damaged ({r})", count(*n))));
/// Fields and messages left out are lost, with their codes (those past a memory limit as
/// such); bytes that aren't GRIB are passed over, plain.
fn notes(&self, limit: usize) -> Vec<crate::loss::Note> {
use crate::loss::{LossCode, Note};
let feature = |t: String| Note::loss(LossCode::DroppedFeature, t);
let mut out: Vec<Note> = self.reasons.iter().map(|(r, n)| feature(format!("{} left out: {r}, not read yet", count(*n)))).collect();
out.extend(self.damage.iter().map(|(r, n)| feature(format!("{} left out: damaged ({r})", count(*n)))));
out.extend(self.messages.iter().map(|(r, n)| {
let what = if *n == 1 { "1 message".to_string() } else { format!("{n} messages") };
format!("{what} left out: {r}")
feature(format!("{what} left out: {r}"))
}));
if self.other_grid > 0 {
out.push(format!("{} left out: on another grid than the first field's", count(self.other_grid)));
out.push(feature(format!("{} left out: on another grid than the first field's", count(self.other_grid))));
}
if self.over_limit > 0 {
out.push(format!(
out.push(Note::loss(LossCode::DroppedOverLimit, format!(
"{} left out: the bands read hold the most this computer keeps in memory ({limit} values); split the file with a GRIB tool (wgrib2 or CDO) and add its parts",
count(self.over_limit)
));
)));
}
if self.too_long > 0 {
out.push(format!(
out.push(Note::loss(LossCode::DroppedOverLimit, format!(
"{} left out: their message is larger than the memory left lets be read whole, even where one of its fields would fit; split the file with a GRIB tool (wgrib2 or CDO) and add its parts",
count(self.too_long)
));
)));
}
if self.cut > 0 {
out.push("the file ends inside a message, which is left out".to_string());
out.push(feature("the file ends inside a message, which is left out".to_string()));
}
if self.junk == 1 {
out.push("1 byte outside its messages isn't GRIB, and is passed over".to_string());
out.push(Note::plain("1 byte outside its messages isn't GRIB, and is passed over"));
} else if self.junk > 1 {
out.push(format!("{} bytes outside its messages aren't GRIB, and are passed over", self.junk));
out.push(Note::plain(format!("{} bytes outside its messages aren't GRIB, and are passed over", self.junk)));
}
out
}
Expand Down Expand Up @@ -769,7 +776,10 @@ fn to_raster(fields: Vec<Field>, name: &str) -> Result<Raster> {
}
if past > 0 {
let (what, has) = if past == 1 { ("1 value".to_string(), "has") } else { (format!("{past} values"), "have") };
r.lost.push(format!("{what} past the range of 32-bit floats (±3.4 × 10^38) {has} no value"));
r.lost.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedValue,
format!("{what} past the range of 32-bit floats (±3.4 × 10^38) {has} no value"),
));
}
Ok(r)
}
Expand Down
Loading
Loading