Repository navigation
fix(data): a tile a sparse GeoTIFF leaves out holds 0, or its no-data value, as GDAL reads it #3592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
630a620
815a913
67872b1
9bdd00c
4173da9
9599abf
138f453
74d5d3e
dbf178b
53f37dd
cc47fec
4f1b204
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,20 +60,38 @@ impl Table { | |
| } | ||
|
|
||
| /// Whether the file leaves chunk `index` of the decoder's current image out (a sparse file's: | ||
| /// it has neither an offset nor bytes): all no-data, whatever the compression. A count of 0 | ||
| /// alone doesn't say so: some writers leave the counts of uncompressed chunks out. | ||
| /// it has neither an offset nor bytes). A count of 0 alone says so of a compressed chunk, which | ||
| /// can't be decoded from nothing, but not of an uncompressed one: some writers leave the | ||
| /// counts of uncompressed chunks out. | ||
| pub(crate) fn absent<R: Read + Seek>(&mut self, dec: &mut Decoder<R>, index: u32) -> bool { | ||
| let tiled = dec.find_tag(Tag::TileWidth).ok().flatten().is_some(); | ||
| matches!(self.get(dec, tiled, index), Ok((0, 0))) | ||
| let compressed = compressed(dec); | ||
| matches!(self.get(dec, tiled, index), Ok((offset, 0)) if offset == 0 || compressed) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rule isn't only for LERC. That matters because a damaged index is now treated the same as GDAL's sparse convention. A compressed chunk whose offset is set but whose count is 0 used to go to the decoder, which can't decode zero bytes, so the read failed. Now it fills silently with 0 or no data, with no notice. The reply that this PR "keeps each reader's rule" is about the in-place readers. It doesn't cover this whole-file change, and no test covers a compressed chunk with a nonzero offset and a count of 0 (the Two possible fixes: limit the offset-or-compressed clause to LERC, where the author argued for it, or keep the offset-0 requirement for other compressions. Or, if GDAL parity is the goal, say so in the PR and add a test that such a chunk reads as 0 or no data. I haven't checked how GDAL's libtiff handles count-0 chunks with an offset, so my confidence that this is wrong is medium. Category: bug
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked against GDAL: a chunk with no bytes and an offset reads as 0 there, compressed (Deflate) and uncompressed alike, so for compressed chunks the widened rule is GDAL's. Uncompressed ones keep needing a 0 offset too, as some writers leave their counts out. cc47fec adds the test: a JPEG strip with its offset kept and no bytes reads black, like one left out entirely. |
||
| } | ||
|
|
||
| /// How many chunks the decoder's current image has, and how many of them the file leaves out. | ||
| pub(crate) fn chunks<R: Read + Seek>(&mut self, dec: &mut Decoder<R>) -> (usize, usize) { | ||
| self.absent(dec, 0); | ||
| (self.offsets.len(), self.offsets.iter().zip(self.counts.iter()).filter(|chunk| *chunk == (&0, &0)).count()) | ||
| let compressed = compressed(dec); | ||
| let left_out = |(offset, count): (&u64, &u64)| *count == 0 && (*offset == 0 || compressed); | ||
| (self.offsets.len(), self.offsets.iter().zip(self.counts.iter()).filter(|chunk| left_out(*chunk)).count()) | ||
| } | ||
| } | ||
|
|
||
| /// Whether the current image's chunks are compressed (a Compression other than 1, none). | ||
| fn compressed<R: Read + Seek>(dec: &mut Decoder<R>) -> bool { | ||
| dec.find_tag_unsigned::<u16>(Tag::Compression).ok().flatten().unwrap_or(1) != 1 | ||
| } | ||
|
|
||
| /// The no-data value the current image's GDAL_NODATA tag gives, if it gives a number. | ||
| pub(crate) fn nodata_tag<R: Read + Seek>(dec: &mut Decoder<R>) -> Option<f64> { | ||
| dec.find_tag(Tag::Unknown(42113)) | ||
| .ok() | ||
| .flatten() | ||
| .and_then(|v| v.into_string().ok()) | ||
| .and_then(|s| s.trim_matches(char::from(0)).trim().parse::<f64>().ok()) | ||
| } | ||
|
|
||
| /// A decoded chunk: its samples, cropped to the image as `Decoder::read_chunk` returns them, and | ||
| /// which of them the chunk's validity masks mark invalid (`None` when none is). | ||
| pub(crate) struct Chunk { | ||
|
|
@@ -112,12 +130,7 @@ pub(crate) fn chunk<R: Read + Seek>(dec: &mut Decoder<R>, index: u32, table: &mu | |
| && bps == 8 | ||
| && format == 1 | ||
| && dec.find_tag_unsigned_vec::<u16>(Tag::ExtraSamples).ok().flatten().and_then(|v| v.last().copied()) == Some(2); | ||
| let nodata = dec | ||
| .find_tag(Tag::Unknown(42113)) | ||
| .ok() | ||
| .flatten() | ||
| .and_then(|v| v.into_string().ok()) | ||
| .and_then(|s| s.trim_matches(char::from(0)).trim().parse::<f64>().ok()); | ||
| let nodata = nodata_tag(dec); | ||
|
|
||
| // Where the chunk is in its plane, and how much of it is image. | ||
| let across = w.div_ceil(cw); | ||
|
|
@@ -140,12 +153,14 @@ pub(crate) fn chunk<R: Read + Seek>(dec: &mut Decoder<R>, index: u32, table: &mu | |
| let expected = data_type(format, bps)?; | ||
| let (offset, count) = table.get(dec, tiled, index)?; | ||
| if count == 0 { | ||
| // A chunk with no bytes (a sparse file's): all no-data, as GDAL reads it. | ||
| // A chunk with no bytes (a sparse file's): its no-data value, or 0 where it has none, as | ||
| // GDAL writes and reads one. With one, every sample is marked no data too: a value the | ||
| // samples' type can't hold (NaN in bytes) would be read as one. | ||
| let mut out = empty(format, bps, n)?; | ||
| for _ in 0..n { | ||
| push(&mut out, None, fill); | ||
| push(&mut out, None, nodata.unwrap_or(0.0)); | ||
| } | ||
| return Ok(Chunk { samples: out, gaps: Some(vec![true; n]) }); | ||
| return Ok(Chunk { samples: out, gaps: nodata.map(|_| vec![true; n]) }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| let count = usize::try_from(count).ok().filter(|n| *n <= MAX_CHUNK_BYTES).ok_or_else(|| err("a chunk is too large"))?; | ||
| // Read as far as the file goes, not allocated from the claim. | ||
|
|
@@ -626,7 +641,7 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn a_chunk_with_no_bytes_is_no_data() { | ||
| fn a_chunk_with_no_bytes_is_0_where_the_file_gives_no_no_data_value() { | ||
| // The first tile's byte count set to 0, as a sparse file has it. | ||
| let path = format!("{}/tests/data/lerc/lerc_zstd_f64.tif", env!("CARGO_MANIFEST_DIR")); | ||
| let mut b = std::fs::read(path).unwrap(); | ||
|
|
@@ -638,11 +653,16 @@ mod tests { | |
| let size = if u16_at(&b, counts + 2) == 3 { 2 } else { 4 }; | ||
| let first = u32_at(&b, counts + 8); | ||
| b[first..first + size].fill(0); | ||
| let r = geotiff::read(Cursor::new(b), "sparse").unwrap(); | ||
| assert_eq!(r.value(0, 0, 0), None); | ||
| assert_eq!(r.value(0, 15, 15), None); | ||
| let r = geotiff::read(Cursor::new(b.clone()), "sparse").unwrap(); | ||
| assert_eq!(r.nodata, None); | ||
| assert_eq!(r.value(0, 0, 0), Some(0.0)); | ||
| assert_eq!(r.value(0, 15, 15), Some(0.0)); | ||
| assert_eq!(r.value(0, 16, 0), Some(4.0), "the next tile reads"); | ||
| assert_eq!(r.value(0, 0, 16), Some(-24.0)); | ||
| // Its offset kept, the count of 0 says it is left out of a LERC file: it takes a sidecar's | ||
| // no-data value as one left out with no offset does. | ||
| let r = geotiff::read_with_nodata(Cursor::new(b), "sidecar", Some(-9999.0)).unwrap(); | ||
| assert_eq!((r.value(0, 0, 0), r.value(0, 16, 0)), (Some(-9999.0), Some(4.0))); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The notice is gone, and the reader now fills values the file doesn't store, with nothing said. Before, a sparse file with no no-data value was reported ("N of its M tiles or strips aren't in the file ... they are read as no data"). Now those tiles hold 0 and no notice is given. The PR says this is GDAL's behaviour and that "nothing is assumed", but AGENTS.md (Never silent) asks for a notice wherever a reader fills in values it does not have, with a count and a place. The tests only check the clean side (
r.lost.is_empty()). Suggested: keep one notice per file with the count of left-out tiles or strips, and phrase it as "read as 0 (GDAL's sparse-file convention)" rather than "assumed". Test both ways, as the rule asks. If the maintainers decide GDAL parity is enough, the PR should say so under "Suggested AGENTS.md additions".Category: agents-md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's GDAL's rule rather than an assumption: GDAL writes a sparse file without the blocks that are all the no-data value, or all 0 where there is none, and reads them back as that (its GeoTIFF driver page, Sparse files). So the value read is what was written, and there's nothing to say. A file with a no-data value, from its tag or its sidecar, reads it as no data.