Skip to content

feat(raster): Clip by extent reads only its part of a large tiled GeoTIFF (#2865) - #2881

Merged
Mavrick-F merged 4 commits into
opengeos:mainfrom
Mavrick-F:feat/2865-windowed-read
Nov 24, 2026
Merged

Mavrick-F merged 4 commits into
opengeos:mainfrom
Mavrick-F:feat/2865-windowed-read

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

First step of #2865: the GeoTIFF reader can read one area of an image, and Clip by extent uses it.

Before this, Clip by extent of a large tiled GeoTIFF read in place read the finest level of the whole file that fits memory, then clipped. On the #2761 mosaic that's the 1:8 level, so a small clip came out at an eighth of the file's resolution.

The reader. raster::read_path_part(path, part, max, same_as) reads a Part of the image, given as fractions of its width and height.

  • It picks the finest level whose part fits max samples: the full image, an overview, or every k-th pixel of a file without overviews.
  • It decodes only the strips or tiles that hold the part, and reads the transparency mask for the part alone.
  • It returns where the part lies in the grid it was read from (PartRead::at, grid, bounds). Callers can then find a window on that grid and move it onto the part in whole pixels, without rounding again.
  • It applies the .aux.xml as the whole read does.
  • It returns None, so the caller reads the whole file, for:
    • a rotated or skewed file
    • a file without coordinates, or with a world file beside it
    • a file that is no longer the revision same_as was read from

Whole reads go through the same chunk loop with the whole image as the span, unchanged.

Clip by extent. For a layer read in place, Clip finds the part from the held overview, one file pixel wider each way. It reads that part within a third of what a run holds (the part, its clip and the clip's file are held at once). It finds the window on the full grid, so the result is the clip of the file held whole, to the pixel. If the part doesn't hold the whole window, Clip reads the whole file as before.

Measured on the #2761 mosaic (10.6 GB, 83517 × 78739, 3 bands), Linux, release rustgis-cli, one run each. The clip is [-56.07, -12.80, -55.93, -12.70]:

main This PR
Result 128 × 94 (from the 10439 × 9842 overview) 1020 × 747 (the file's full resolution)
Time, peak memory 3.10 s, 1.30 GB 0.23 s, 88 MB

Memory. A clip holds its part of the layer, not the whole layer: about the clip's own size, at most a third of what a run may hold. The part is let go before the clip is written, and no copy is kept for the run.

Surfaces

  • Desktop app, command line, Python, phone: Clip by extent of a layer read in place reads only its part. It runs off the UI thread as before (workflows::IN_BACKGROUND).
  • Browser: unchanged. Nothing is read in place from a file there.
  • Ribbon, command search, Assistant, MCP, plugins: no command or parameter changes.
  • The way back: the result is a new layer, removed or undone as before.

Testing

  • raster::geotiff::tests::a_part_is_the_whole_file_read_at_its_level_then_cut_to_it checks a part against the whole file read at the same level and cut. It covers:
    • full size, an overview, and every other pixel, including a 1001-pixel file
    • bands stored apart, signed bytes, bottom-up files
    • a part reaching the image's edges
    • placement, no-data, system, band names, at/grid, coverage of the requested ground, and bytes read (under a quarter of the file)
  • a_turned_or_unplaced_file_is_not_read_by_part
  • The chunk iterator and mask tests now cover parts of the grid, and check that no chunk without one of the part's pixels is read.
  • tests/tiled_files.rs::a_clip_by_extent_reads_only_its_part_of_a_tiled_file uses a 0.001° fixture read in place:
    • Four clips equal the clips of the file held whole, pixel for pixel, with the same transform. None reads the file whole.
    • A file replaced since the layer was read is read whole and clipped where it now is.
    • It fails without the part read (the file is read whole). It also fails if the window is found on the part's own origin, giving 121 columns where 120 are right, with the origin one ulp off.
  • Locally: cargo fmt, the rustgis-data raster tests, and the engine tiled-file, cancellation, process, clip and imagery tests.
  • Fork CI (lint, tests, layers, wasm, licences) passed on this commit.

UI evidence

No visible UI change.

Related issue

Part of #2865. Making the per-cell commands work a block at a time follows in a separate PR.

Summary by CodeRabbit

  • New Features
    • Extent-based clipping can read only the required portion of supported GeoTIFF files, preserving the clipped raster’s dimensions, alignment, and band data.
    • For larger requested areas, partial reads can use an appropriate overview or sampled image level within configured limits.
    • If partial reading is unsupported or the source file has changed, clipping falls back to reading the full raster.

…TIFF (opengeos#2865)

The GeoTIFF reader reads a part of an image: the finest level whose part
fits the sample limit, decoding only the strips or tiles that hold it, its
transparency mask read for the part alike (`raster::read_path_part`). A
rotated or unplaced file, or one with a world file beside it, is read
whole as before.

Clip by extent of a layer read in place from a file reads only the part
under the extent, so a small clip of a mosaic too large to hold comes out
at the file's full resolution.
…p's share of memory

- Every k-th pixel of a file whose size k doesn't divide: the part is
  found on the image's extent in the grid's pixels, not the grid's size,
  so no column it covers is left out.
- `read_path_part` says where the part lies in the grid it was read
  from; Clip finds its window on that grid, as a whole read does, and
  moves it onto the part in whole pixels, so an edge on a pixel's edge
  isn't rounded into the next pixel.
- A file no longer the revision the layer was read from is read whole,
  and so is one whose part doesn't hold the window.
- Clip reads a part within a third of what a run holds (the part, its
  clip, and the clip's file at once), and a part isn't kept for the run
  past its clip: only what the run says of it is.
…nned in its turn

- `PartRead` gives the bounds of the grid read: every k-th pixel's grid can
  pass the held overview's, and a box cut to those lost a column.
- The file's revision is taken once and compared with the layer's, the same
  one the read is then checked against.
- A part's limit is a share of what a run holds, planned once the read has
  its turn, with the reads before it counted.
- What a run says of a part (an overview, rounded values) is said only once
  the clip uses it, not for a part put aside for the whole file.
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds span-based GeoTIFF part reads and uses them for extent clipping when available. Part reads include grid placement and bounds. When a part is unavailable or does not cover the requested window, clipping falls back to a whole-file read.

Changes: GeoTIFF part reads and extent clipping

Layer / File(s) Summary
Part read contract and file entry points
crates/data/src/raster/mod.rs, crates/data/src/raster/file_revision.rs, crates/data/src/raster/geotiff.rs
Adds normalized part coordinates and PartRead metadata. Adds file-backed part-read entry points with source-revision checks and sidecar metadata handling.
Span-based decoding and mask sampling
crates/data/src/raster/geotiff.rs, crates/data/src/raster/mask.rs
Restricts chunk traversal, output placement, overview selection, and mask sampling to the requested span. Tests cover partial spans, pixel placement, and unsupported part reads.
Tiled read tracking and partial-read workflow
crates/engine/src/cmd/gp.rs, crates/engine/src/cmd/workflows.rs
Adds partial reads during active runs and stores their placement and read notes. Whole-file raster reads remain cacheable.
Extent clipping with part reads
crates/engine/src/cmd/imagery.rs, crates/engine/src/tests/tiled_files.rs
Maps extent windows between grids and clips from a bounded file part when possible. Falls back to a whole-file read if the part does not cover the window. Integration tests compare results and check source replacement handling.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant ClipByExtent
  participant InputReadPart
  participant ReadTiledPart
  participant ReadPathPart
  participant GeoTIFFReader
  participant ClipWindow
  ClipByExtent->>InputReadPart: Request extent part
  InputReadPart->>ReadTiledPart: Read part within sample limit
  ReadTiledPart->>ReadPathPart: Request file-backed part
  ReadPathPart->>GeoTIFFReader: Decode requested GeoTIFF span
  GeoTIFFReader-->>ReadPathPart: Return part result or no part
  ReadPathPart-->>ReadTiledPart: Return part result
  ReadTiledPart-->>InputReadPart: Return raster and placement
  InputReadPart-->>ClipByExtent: Return part or whole-read fallback
  ClipByExtent->>ClipWindow: Clip requested window
Loading

Possibly related issues

  • opengeos/RustGIS#2865 — Adds the GeoTIFF windowed read used by extent clipping, which the issue identifies as reading a whole level before clipping. The issue’s block-wise processing requests are outside this change.
  • opengeos/RustGIS#2592 — Adds a windowed read path for tiled GeoTIFFs, but does not add the issue’s requested strip support.

Possibly related PRs

  • opengeos/RustGIS#2772 — Adds in-place reads for eligible local tiled GeoTIFFs and held overviews, which this change uses when requesting file parts.

Suggested reviewers: giswqs, deniial00

Merge Risk: Minimal · up to c7779

Clip by extent now reads only the needed part of a tiled GeoTIFF and falls back to a whole-file read when the part cannot be used. No merge-blocking issue was found, and the fallback paths and tests cover the main failure modes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "feat(raster): Clip by extent reads only its part of a large tiled GeoTIFF (#2865)" matches the main change. The changeset adds a partial GeoTIFF read (read_path_part) and uses it so Clip by…
Description check Passed The description follows the template's Summary, Testing, UI evidence, and Related issue sections. The Summary explains the reader change and the Clip by extent change, with measured results. Testing l…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "feat(raster): Clip by extent reads only its part of a large tiled GeoTIFF (#2865)" matches the main change. The changeset adds a partial GeoTIFF read (read_path_part) and uses it so Clip by extent reads only the needed part of a large tiled file in place. The title is specific and scoped to the raster area. It is a conventional-commit style title with an issue reference, which is acceptable.

Full details: Description check

Explanation

The description follows the template's Summary, Testing, UI evidence, and Related issue sections. The Summary explains the reader change and the Clip by extent change, with measured results. Testing lists specific tests and local and CI runs. UI evidence states 'No visible UI change.', which the template allows when there is no visible change. Related issue references Part of #2865. The description is thorough and matches the changeset and objectives.

Finishing Touches

Generate unit tests (beta)

  • 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 reads a raster square,
And hops through tiles with patient care.
The clipped-out bounds are mapped just right,
With overview levels kept in sight.
If parts fall short, the whole file’s there,
Then carrots mark the pixels rare.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 604a1400-5ffa-48d3-b770-386967adc114
Commits

Reviewing files that changed from the base of the PR and between 7805b36 and c777968.

Files selected for processing: 5

  • crates/data/src/raster/geotiff.rs
  • crates/data/src/raster/mod.rs
  • crates/engine/src/cmd/gp.rs
  • crates/engine/src/cmd/workflows.rs
  • crates/engine/src/tests/tiled_files.rs

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

Comment thread crates/engine/src/cmd/gp.rs Outdated
#[cfg(not(target_arch = "wasm32"))]
impl ReadNote {
pub(crate) fn of(name: &str, r: &rustgis_data::raster::Raster) -> ReadNote {
ReadNote { name: name.to_string(), rounded: r.rounded.iter().sum(), overview: r.overview.map(|full| ((r.width, r.height), full)) }

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 notice reports the part's size as the raster's size. For a part read at an overview level, read_image stores overview as the part's size in file pixels (geotiff.rs, the file(ow, sx) mapping), not the file's full size. ReadNote::of then builds "… overview of the {fw} × {fh} raster" from that value, so a Clip of a part of an 83517 × 78739 file would say "…overview of the 60000 × 60000 raster" or similar. That is a wrong number in a notice, which breaks the "says what, how much" rule.

file_pixels() needs the part's file size, so the fix is to keep the full file size next to the part (for example a full: (u32, u32) field on PartRead and Place) and have ReadNote::of use it. The overview doc in mod.rs and the file_pixels doc (which says overview "stays the file's own size") also need updating.

The part test in geotiff.rs pins overview to the part's size, and tiled_files.rs never checks the note text for a part read at a coarser level, so neither catches this. A test asserting the notice wording for that case would.

Confidence: high on the mismatch, medium on how often a user hits it (needs a clip large enough to read at an overview level).

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.

Fixed in c777968. PartRead now carries the file's full size, and a part read from an overview says "at its 500 × 500 overview of the 1000 × 1000 raster, a 62 × 177 part of it". The file_pixels doc is updated, and a_clip_by_extent_reads_only_its_part_of_a_tiled_file asserts that notice.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

Reviewed the whole diff (8 files, +843/−140) against pr-context/AGENTS.md. The reader logic is careful. The part windows, chunk and mask spans, the Clip window found on the finer grid, the revision check and the wasm cfg gates all check out on reading. I could not compile or run the tests here.

Bugs

  • Wrong raster size in the read notice (crates/engine/src/cmd/gp.rs:1670, inline). When a part is read at an overview level, read_image stores overview as the part's size in file pixels, not the file's. ReadNote::of then reports it as "of the N × M raster", so a clip of part of a large file names the wrong raster size. Breaks the "says what, how much" rule. Fix: carry the full file size alongside the part. Confidence high on the mismatch, medium on how often it is hit. The existing tests pin overview to the part size and never check the note text, so they miss it.

Security

  • None found.

Performance

  • None found. Chunk selection is proportional to the part, and the part is read under tiled_turn.

Quality

  • crates/data/src/raster/mod.rs:1111-1113 (not inline, because that line is outside the diff): the file_pixels doc says overview "stays the file's own size", which is no longer true for parts. Fix it together with the bug above.
  • Minor: edge_pixels_on now computes r.extent() eagerly on every call rather than only on the fallback. Negligible cost, noted only.

AGENTS.md

  • No violations found. No new commands, so the surface snapshot is unchanged. The clip stays on the background path, and the new read goes through decimate, which checks Cancel and counts progress. Wasm gating is consistent with the cfg attributes I checked. No unwrap or expect in non-test code.

Already raised: 0. There were no prior review threads on this PR.

Change size

Compared: the whole PR against main (merge base 132a81c to PR head 7805b36).

Measure Value
Files changed 8 (0 added, 8 modified, 0 removed, 0 renamed)
Size before 1.08 MB
Size after 1.11 MB
Total size change +37.4 KB
File Before After Change
crates/data/src/raster/geotiff.rs 274.5 KB 292.0 KB +17.5 KB
crates/data/src/raster/mod.rs 144.0 KB 148.9 KB +4.8 KB
crates/engine/src/tests/tiled_files.rs 18.1 KB 22.5 KB +4.4 KB
crates/engine/src/cmd/imagery.rs 152.8 KB 156.8 KB +4.0 KB
crates/engine/src/cmd/workflows.rs 131.2 KB 134.7 KB +3.5 KB
crates/engine/src/cmd/gp.rs 359.4 KB 361.3 KB +1.9 KB
crates/data/src/raster/mask.rs 16.8 KB 17.8 KB +1.0 KB
crates/data/src/raster/file_revision.rs 6.8 KB 7.0 KB +0.2 KB

…he part's

The notice said "at its W × H overview of the P × Q raster", P × Q being
the part's size in the file's pixels. `PartRead` now gives the file's full
size, and a part's notice names the level, the file and the part:
"at its 500 × 500 overview of the 1000 × 1000 raster, a 62 × 177 part of it".
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-2881/
Web app https://preview.rustgis.app/pr-2881/app/
Commit c777968

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

Comment on lines +1863 to +1872
// The pixels kept of the grid read (`w` × `h`): a part's, else all of them.
// The image's extent in the grid's pixels: every k-th pixel's grid passes its edge when k
// doesn't divide its size.
let extent = if decimate_by > 1 {
(f64::from(full_w) / f64::from(decimate_by), f64::from(full_h) / f64::from(decimate_by))
} else {
(f64::from(w), f64::from(h))
};
let span = part.map(|p| p.span((w, h), extent));
let (ow, oh) = span.map_or((w, h), Span::size);

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 comment "The pixels kept of the grid read (w × h): a part's, else all of them." sits above the extent block, but it describes span, ow and oh, which come after it. Moving it down next to them reads more clearly. Nothing changes in behaviour.

Suggested change
// The pixels kept of the grid read (`w` × `h`): a part's, else all of them.
// The image's extent in the grid's pixels: every k-th pixel's grid passes its edge when k
// doesn't divide its size.
let extent = if decimate_by > 1 {
(f64::from(full_w) / f64::from(decimate_by), f64::from(full_h) / f64::from(decimate_by))
} else {
(f64::from(w), f64::from(h))
};
let span = part.map(|p| p.span((w, h), extent));
let (ow, oh) = span.map_or((w, h), Span::size);
// The image's extent in the grid's pixels: every k-th pixel's grid passes its edge when k
// doesn't divide its size.
let extent = if decimate_by > 1 {
(f64::from(full_w) / f64::from(decimate_by), f64::from(full_h) / f64::from(decimate_by))
} else {
(f64::from(w), f64::from(h))
};
// The pixels kept of the grid read (`w` × `h`): a part's, else all of them.
let span = part.map(|p| p.span((w, h), extent));
let (ow, oh) = span.map_or((w, h), Span::size);

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: it moves a comment, with no change in behaviour.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, crash paths, security issues or AGENTS.md breaks in the changed code. I checked the part-read path end to end: the chunk and mask span arithmetic, the placement of an overview's pixels, the overview meaning change and its consumers, the revision checks around the decode, the Clip path through reading_files (it runs on the same thread, so RUN_READS is set), the wasm gating, and the banned-call scan on added lines. I could not compile or run the tests here (no shell), so the build and test results are unverified.

Bugs

None found.

Security

None found.

Performance

None found.

Quality

  • Misplaced comment in read_image (posted inline, geotiff.rs:1863): a comment about span/ow/oh sits above the extent block. Confidence high. The suggestion moves it.
  • Cryptic comment (not posted, nit): geotiff.rs:1663, "Only a part is ever not read.", is accurate but hard to parse. Consider rewording it to say that read_image returns None only for a part.
  • Weak assertion (not posted, nit): tiled_files.rs:417 checks only that no note contains "finest level". For a full-resolution part clip the run probably produces no notices at all, so asserting notes.is_empty() would be stricter. Confidence medium, since I could not run it.

AGENTS.md

None found. Non-test code has no unwrap/expect/panic, no new commands or parameters, no new layer data copies, and the wasm gating looks consistent.

Earlier review threads

One earlier thread (gp.rs, the notice reporting a part's size as the raster's size) is outdated. The current code addresses it: ReadNote::of_part uses the level's size and the file's full size, file_pixels and the overview doc are updated, and the test pins the note text. I did not re-post it. I left 1 earlier concern alone for that reason.

Change size

Compared with the base branch (merge base 132a81c to PR head c777968).

Measure Value
Files changed 8 (0 added, 8 modified, 0 removed, 0 renamed)
Size before 1.08 MB
Size after 1.12 MB
Total size change +38.5 KB
File Before After Change
crates/data/src/raster/geotiff.rs 274.4 KB 292.0 KB +17.6 KB
crates/engine/src/tests/tiled_files.rs 18.1 KB 23.2 KB +5.2 KB
crates/data/src/raster/mod.rs 144.0 KB 149.0 KB +5.0 KB
crates/engine/src/cmd/imagery.rs 152.8 KB 156.8 KB +4.0 KB
crates/engine/src/cmd/workflows.rs 131.1 KB 134.7 KB +3.5 KB
crates/engine/src/cmd/gp.rs 359.4 KB 362.3 KB +2.9 KB
crates/data/src/raster/mask.rs 16.8 KB 17.8 KB +1.0 KB
crates/data/src/raster/file_revision.rs 6.8 KB 7.0 KB +0.2 KB

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.

1 participant