Skip to content

GeoTIFF: read tiled files with bands stored apart without panicking - #549

Merged
giswqs merged 2 commits into
mainfrom
fix/issue-544-planar-cog
Oct 19, 2026
Merged

giswqs merged 2 commits into
mainfrom
fix/issue-544-planar-cog

Conversation

@giswqs

@giswqs giswqs commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Root cause

The tiff crate (0.11.3) works out a tile's padding from its index as row = tile / tiles_across without wrapping the index to its band plane. In a tiled GeoTIFF whose bands are stored apart (INTERLEAVE=BAND, PlanarConfiguration 2), every band after the first therefore has its bottom row of tiles treated as unpadded:

  • Read whole (read_image_to_buffer, used for images under a million pixels, from bytes in memory and in the browser), the decoder asserts on the buffer it sliced for those tiles and panics (image.rs:919), which is the crash in the issue.
  • Read a tile at a time (large local files such as the issue's 2088 × 1143 Landsat COG, and overviews made as a file is read), read_chunk returns those tiles at full tile height, and Chunks::place inferred the row length from the decoded length divided by the image rows, so the bottom strip of bands 2 and 3 came out skewed (a red band with noisy rows along the bottom of the Landsat image).

The first half of the issue (reading planar COGs in place by range) already works on main since #542: layer.addUrl on landsat_ts/1984.tif opens in place and renders correctly.

Fix

  • read_limited reads tiled multi-band planar images a tile at a time (decimate with k = 1) instead of through read_image_to_buffer; tiled planar images under 8 bits a sample, which can't be read so, keep the whole read when no bottom tile is padded (the decoder handles those) and otherwise return an error instead of reaching the decoder.
  • Chunks::place takes a chunk's row length from whichever shape (data or tile width, by image or tile rows) its decoded length matches, preferring the data width and rows.

Test

tiled_bands_stored_apart_are_read_whole builds planar tiled RGB TIFFs with padded right, bottom and corner tiles, and checks every pixel of every band read whole and with every 2nd pixel. Without the routing it panics in the tiff crate; without the stride fix it fails on band 1's bottom tiles.

Verification

  • The issue's file (landsat_ts/1984.tif) read through geotiff::read from memory: panicked before, reads 2088 × 1143 × 3 now.
  • Opened locally with layer.addData and exported: the bottom strip garbled on main, clean on this branch.
  • Opened by URL with layer.addUrl: in place, as on main.
  • cargo xtask ci and the wasm32 build of rustgis-web pass locally (one unrelated engine streaming test flaked once under load and passes on rerun).

Fixes #544

Summary by CodeRabbit

  • Bug Fixes
    • Improved reading of multi-band tiled GeoTIFF images, including tiles with padded rows, to return band data more reliably.
    • Decimated reads of planar tiled images with samples below 8 bits continue to work. Full-image reads now report an unsupported-format error when those tiles have padded rows.
    • Added coverage for full and decimated reads across image and tile dimensions.

The tiff crate works out a tile's padding from its index without
wrapping it to the band plane, so in a tiled image whose bands are
stored apart (GDAL's INTERLEAVE=BAND) every band after the first has
its bottom tiles treated as unpadded. Read whole, the decoder asserts
on the short buffer and panics; read a tile at a time (large files,
and overviews made as the file is read), those tiles come back at
full height and their rows were placed skewed, garbling the bottom
strip of every band but the first.

Such images are now always read a tile at a time, and a chunk's row
length is taken from the shape its decoded length matches rather than
from the image rows alone.

Fixes #544
@github-actions

Copy link
Copy Markdown
Contributor

Code review

I reviewed the single changed file, crates/data/src/raster/geotiff.rs: the Chunks stride logic, the new planar_tiles path in read_limited, and the new test. I didn't run anything (no Bash tool), so none of this was executed. I found no blocking problems.

Bugs

  • Stride heuristic can be ambiguous (low confidence, about 25%). shapes picks the first candidate whose cols * rows * samples equals values.len(). Two candidates can give the same product. For example, (dw, self.ch) and (self.cw, rows) collide on an edge tile that is clipped in both directions with just the right dimensions. When that happens, the wrong stride would skew the rows. The order puts the clipped-data shape first, which is the likeliest correct pick, and the test covers non-square and partial edges. I wouldn't change anything, but a comment naming this limitation would help.
  • Unsupported case now fails with an error instead of a panic (informational). Tiled, planar images with fewer than 8 bits per sample now return an Err. That fits the "never crash" rule. It is a deliberate limitation, not a bug.

Security

  • No issues. The arithmetic uses saturating_*, there is no indexing or unwrap, and the stride falls back to the old formula and is clamped with .max(1).

Performance

  • No issues. Reading a tile at a time on the planar_tiles path bypasses the single whole-image decode, which is needed to avoid the decoder assertion. The cost is the usual per-chunk overhead.

Quality

  • Regression test is thorough. It builds a synthetic planar tiled GeoTIFF and checks four edge-tile geometries at both full resolution and decimation k=2, and each pixel is verified. This is good coverage for a crash fix.
  • Minor: long lines. The shapes array and the .find(...) closure (lines 16 and 19) are long. Confirm that cargo fmt accepts them. The surrounding code already has long lines, so it is likely fine.

AGENTS.md

  • No unwrap, expect, panic or unsafe in the non-test code.
  • The fix has a regression test, as the guidelines require.
  • No new UI strings, so no tl! wrapping is needed.
  • The change touches only rustgis-data (L1) and adds nothing that would break wasm.

Change size

Compared: the whole PR against main (merge base 6a17430 to PR head 032e9cb).

Metric Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 93.8 KB
Size after 98.9 KB
Total size change +5.1 KB
File Before After Change
crates/data/src/raster/geotiff.rs 93.8 KB 98.9 KB +5.1 KB

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: dc5f1f86-0972-4171-9302-2a611d87cac1
📥 Commits

Reviewing files that changed from the base of the PR and between 032e9cb and 084ab70.

📒 Files selected for processing: 1
  • crates/data/src/raster/geotiff.rs

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

📝 Walkthrough

The GeoTIFF reader now places decoded chunks using matched tile dimensions and reads eligible multi-band planar tiled images chunk by chunk. Tests cover full and decimated reads across image and tile layouts.

Changes: Planar tiled GeoTIFF reading

Layer / File(s) Summary
Decoded chunk layout matching
crates/data/src/raster/geotiff.rs
Chunks::place matches decoded buffer lengths against actual or padded tile widths and row counts to determine row stride.
Chunked planar reads and validation
crates/data/src/raster/geotiff.rs
read_limited routes multi-band planar tiled images at 8 bits or more through chunk decoding. For full-image reads of planar tiled samples below 8 bits, it returns an unsupported-format error when tile rows are padded. Tests check full and every-second-pixel reads across four image and tile layouts.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 084ab

No current read failure is established in the changed paths. The sub-8-bit rejection lacks a direct regression test, but the implementation currently returns the intended error.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly identifies the main change: preventing panics when reading tiled GeoTIFF files with separately stored bands. It is concise and specific.
Linked Issues check Passed Issue [#544] requires safe whole-image reads for tiled planar GeoTIFFs and band-separated tile access. read_limited routes tiled multi-band planar images with at least 8 bits per sample through tile…
Out of Scope Changes check Passed The production changes affect planar GeoTIFF read routing and chunk placement. The added fixture and regression test verify the [#544] failure modes. No unrelated production behavior or unrelated test…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 1 files.
Full details: Linked Issues check

Explanation

Issue [#544] requires safe whole-image reads for tiled planar GeoTIFFs and band-separated tile access. read_limited routes tiled multi-band planar images with at least 8 bits per sample through tile reads. It returns a format error for padded whole reads below 8 bits instead of reaching the decoder assertion. Chunks::place matches decoded lengths to data and tile shapes, which fixes padded bottom-band placement. The regression test checks all pixels for padded right, bottom, and corner tiles in whole and decimated reads. The existing cog.rs layout maps each planar band and tile to a separate range chunk, so in-place access remains supported.

Full details: Out of Scope Changes check

Explanation

The production changes affect planar GeoTIFF read routing and chunk placement. The added fixture and regression test verify the [#544] failure modes. No unrelated production behavior or unrelated tests are evident in the reviewed changes.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks each tile with care
And finds the row stride waiting there
Three bands travel, chunk by chunk
Full reads and decimated reads are done
Across four layouts, values stay in view
The rabbit hops away, review complete

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-549/
Web app https://preview.rustgis.app/pr-549/app/
Commit 084ab70

GitHub Pages takes a minute or two to publish a new commit.

The decoder reads tiled images with bands stored apart whole when no
bottom tile is padded, so only padded ones, read in full, are refused
below 8 bits a sample. Names the shape tie-break in Chunks::place.
Comment thread crates/data/src/raster/geotiff.rs Resolved
Comment thread crates/data/src/raster/geotiff.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

The PR fixes #544 by reading tiled GeoTIFFs with separately stored bands (planar configuration 2) one tile at a time, instead of whole. I found no crash or correctness bug. I raised two low-confidence concerns as inline comments. I only read the diff and the surrounding code, and did not build or run the tests.

Bugs

  • Stride heuristic is ambiguous (low–medium confidence): the chunk stride is inferred from values.len() alone, so shapes of equal area can be confused. A non-square tile with a short bottom edge is one example. When no shape matches, the fallback gives a skewed stride silently. See the inline comment on geotiff.rs:645.
  • padded only checks height (low confidence): sub-8-bit planar tiled images with only the width padded are not refused. That is probably correct, since the panic is on bottom tiles, but it should be confirmed and commented. See the inline comment on geotiff.rs:933.

Security

  • Nothing found. The new code uses saturating_* arithmetic, max(1) guards and .find/.map_or, with no indexing, unwrap or unsafe.

Performance

  • Nothing found. The shape search checks 4 candidates per chunk. Planar tiled images now take the per-tile path instead of the whole-image read, which is intended.

Quality

  • The new error path for sub-8-bit planar tiled images has no test. The regression test covers 8-bit RGB only (low confidence).
  • The test covers three square-tile cases with height padding, one with width padding, and decimation by 2. It has no non-square tile case.

AGENTS.md

  • No violations seen. There is no unwrap or panic in non-test code, and the new error is returned rather than panicked. There is a regression test for the crash fix, and the change is wasm-safe. The new error message is a data-layer error, not interface text.

Change size

Compared: the whole PR against main (merge base 6a17430 to PR head 084ab70)

Metric Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 93.8 KB
Size after 99.3 KB
Total size change +5.5 KB
Path Before After Change
crates/data/src/raster/geotiff.rs 93.8 KB 99.3 KB +5.5 KB

@giswqs
giswqs merged commit a47ce62 into main Oct 19, 2026
19 checks passed
@giswqs
giswqs deleted the fix/issue-544-planar-cog branch October 19, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: band-interleaved (planar) COGs aren't read in place, and reading them whole panics in the tiff decoder

1 participant