Skip to content

feat(raster): Mosaic Rasters on a thread of its own, with Cancel (#2907) - #2936

Merged
Mavrick-F merged 1 commit into
opengeos:mainfrom
elpidioassinaturas:feat/2907-mosaic-background
Nov 26, 2026
Merged

Mavrick-F merged 1 commit into
opengeos:mainfrom
elpidioassinaturas:feat/2907-mosaic-background

Conversation

@elpidioassinaturas

@elpidioassinaturas elpidioassinaturas commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2907 (part 1, the path). Part 2, the Mosaic section in the Workflows pane that calls it, comes after this.

raster.mosaic can make its raster on a thread of its own in the desktop app, with progress and Cancel, as Polygons to Raster does since #2739. Until part 2 nothing in the app's windows asks for it: it has no pane, and Command Search lists it greyed ("it needs input", search_disabled in chrome.rs), as inputs is required.

What changes

  • mosaic_rasters_work (cmd/imagery.rs) joins workflows::IN_BACKGROUND, and raster.mosaic becomes made_or_ran. The command line, Python, MCP, scripts, the Assistant and plugins still run it in place, through the same code.
  • Read of the session first: the inputs' layers (an Arc each, no copy), which inputs are files, the name and the path. The checks on count and name come first, as now.
  • On the thread: the files are read, each input is checked as it is taken, and all are copied into the result. Cancel is looked at between inputs and about every million cells copied (whole rows). Progress counts the inputs' rows of each band.
  • The checks keep their order. An input is checked against the ones before it, and the first refusal is the error. The layers ahead of the first file are checked as the command starts, so a mosaic of layers is refused there. From the first file on, the checks run on the thread, because an earlier file's error comes before a later input's.
  • The GeoTIFF is the plain one it writes today, now written beside its destination and put in place as the raster is added (write_plain_staged, Made::committing). A cancelled or abandoned run leaves the destination as it was and removes the temporary file. Cancel is looked at as it is encoded and every megabyte written.
  • Notices are said as the raster lands, in today's order: what reading left out, copyrights not kept, band names not kept, the file written. say_losses is split so its lines can be made off the session (loss_lines).
  • "ran", the internal key the other background raster commands have, is added to its parameters and to command-surface.txt.

The cells, the answer and the file are the same.

Two differences in a failed run

  • A run that fails once its inputs are read (the file can't be written, say) gives its notices in the error, in parentheses, as Polygons to Raster does. Before, they were notices given before the error. The thread can't tell the session, and the notices would be about a raster that isn't there.
  • If the layer can't be made after the GeoTIFF is written, no file is left. Before, it stayed.

Permissions

mosaic_reads_file still decides what is a file, for the command and for mosaic_stays_in_project, so #1234 holds: a plugin with edit alone mosaics project layers, and a file or a path needs files. Handing over a run (ran) doesn't get round it: the named inputs are asked about all the same.

Memory

As a multiple of the inputs' cells (4 bytes each), for tiles that don't overlap, so the result is the size of the inputs:

Files, before Files, now Layers, before and now
While joined 2× 2× 2×
While encoded, one band up to 3× up to 2× up to 3×
While encoded, several bands up to 4× up to 3× up to 4×

The inputs read from files are now let go of once joined, before anything is encoded. Layers stay in the project either way. The encoding is at most the result again (it is compressed), and several bands are also interleaved once. The result then stays in the project with its encoded copy, as before. Where tiles overlap, the inputs are more than the result.

These are from the code, not measured: cargo xtask memory measures table workflows only (load, join, calculate, export, edits, save) and has no raster scenario.

One case is new, as in #2739: a raster layer edited while a run reads it is copied (copy-on-write) until the run lets go of it.

Looks at Cancel: the long steps without one

The statistics of the result (Raster::new), and reading a file in a format whose reader doesn't look. The GeoTIFF reader does look.

Reaches

Tests

  • a_mosaic_made_on_another_thread_is_the_one_made_here: layers, files and both, overlapping (the first wins), with and without a GeoTIFF. Cells, transform, answer, notices and file bytes match those of a run in place. Nothing is said and no file is put in place before it lands, and a run let go of leaves the existing file as it was and no other. Ten refusals give the same words in place and on a thread, and whether the start or the run gives them is checked. A file that can't be put in place (a folder is there) is refused with what the inputs had to say.
  • long_commands: a mosaic of a layer, a file and a layer, written over an existing file. It is cancelled at each of its checks in turn and leaves the project and the files as they were.
  • clip_resample_and_mosaic_run_with_imagery_off now lands a mosaic as the app does too; plugin_mosaics_of_project_layers_need_no_files has the two ran cases.
  • cargo test -p rustgis-engine: 2,449 unit tests pass. Of the integration tests, only portable_projects::reload::wrong_geometry_missing_file… fails, on this computer, as it compares an operating system message that is in Portuguese here. After the last small edit, only the mosaic and cancellation tests were run again (11 pass).
  • cargo fmt --check, clippy with -D warnings on the crate with all targets, cargo xtask layers, test-hygiene, rows and attribution.
  • Not tested or measured: the app's window, the browser build and timings. The times in the issue are from 0.8.0. The rest of the workspace is left to CI.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • raster.mosaic now runs in the background, so you can continue working while a mosaic is created.
    • Mosaic creation checks for cancellation and reports input issues and raster notices when processing finishes.
    • Output files are staged and committed when the resulting layer is added, helping prevent incomplete files from being left behind.

…ngeos#2907)

raster.mosaic joins workflows::IN_BACKGROUND: the session is read first (the
layers shared, the names and the path), and the files are read, checked and
copied on the thread, with progress and Cancel. Its GeoTIFF is written beside
the destination and put in place as the raster is added. What it answers and
says is the same; everything but the app's dispatch still runs it in place.

Part 1 of opengeos#2907: the Workflows pane's section comes next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Caution

CodeRabbit couldn't post its review summary.

Error details

HttpError

let there = s.finish_raster("raster.mosaic", id, &p).unwrap();
assert_eq!(said(&s, mark, &path_there), said_here, "{inputs}");
assert_eq!(cells(&s, &there), cells(&s, &here), "{inputs}");
assert!((here.get("path").is_some(), there.get("path").is_some()) == (file.is_some(), file.is_some()), "{here} {there}");

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.

Nit: this assert! compares a tuple built from here/there against a tuple that repeats file.is_some(), so a failure message doesn't say which side differs. An assert_eq! with each side's own value reads more clearly and points at the case that broke:

Suggested change
assert!((here.get("path").is_some(), there.get("path").is_some()) == (file.is_some(), file.is_some()), "{here} {there}");
assert_eq!((here.get("path").is_some(), there.get("path").is_some()), (file.is_some(), file.is_some()), "{here} {there}");

Category: quality

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, assert_eq! would name the side that differs; the assertion itself is right, so I'm leaving it, and the rows that shadows the total in Tiles::join (imagery.rs:2164, 2168), for a push needed for another reason, or the release quality pass.

On the review's other two points, the description is updated:

  • Command Search doesn't reach it. search_disabled (chrome.rs:1275) greys a command that has a required parameter and no entry in search_action, and the palette runs only the rows it doesn't grey (chrome.rs:1468). inputs is required and raster.mosaic has no entry, so nothing reaches lib.rs:3399 with it yet. The intro now says so, and Reaches says it the same way.
  • Memory now gives the multiples. They are from the code: cargo xtask memory has table scenarios only (xtask/src/memory.rs, scenarios).

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, crash paths, security problems or performance regressions in the changed code. I could not build or run the tests (no shell here), so the checks below are by reading only. I checked:

  • Tiles::take bounds, which keep the coordinate arithmetic away from overflow.
  • Progress and Cancel: geotiff::write looks at Cancel per strip, and the join loop uses step.
  • Staging: write_plain_staged only builds notes after staging, so nothing is written outside the staged file.
  • IN_BACKGROUND signature, write_and_report callers, and the removed imagery::add (no remaining callers).
  • The ran path: continuation_owner gates it.
  • wasm: the new staged-write helpers aren't cfg-gated, matching the existing cogwrite staged path.

No earlier review threads exist on this PR, so there were no concerns to skip.

Bugs

None found.

Security

None found.

Performance

None found.

Quality

  • Test assertion (crates/engine/src/tests.rs:53812, confidence high): a tuple assert! that repeats file.is_some() gives a poor failure message. Posted inline with a suggested assert_eq!.
  • PR description is inaccurate (confidence medium): it says "Until part 2 nothing in the app's windows asks for it." Command Search does ask. ui-egui/src/lib.rs:3399 sends any IN_BACKGROUND command from the app's generic dispatch to a thread, so a Command Search run of Mosaic Rasters now runs in the background. The "Reaches" section does say the app's dispatch starts it on a thread, so the two sections disagree. Either the intro sentence should change, or the Command Search route should get a test.
  • Nit (confidence high): in imagery.rs:2164 and 2168, the per-input rows shadows the total rows from line 2164. It is harmless, but a different name would read better.

AGENTS.md

  • Memory multiple missing (confidence medium): the mosaic copies its inputs into a new layer, and the rule in "Size for real data" asks the PR to state what the change holds as a multiple of the layer, as cargo xtask memory measures. The Memory section lists what a run holds (inputs, result, encoded file, encoded layer) but gives no multiple. The PR body should add one.

Earlier concerns left alone: 0 (no prior threads).

Change size

Compared with the base branch (merge base e7f6bcf to PR head 07d9050), as recorded in stats.json.

Measure Value
Files changed 6 (added 0, modified 6, removed 0, renamed 0)
Size before 4.0 MB
Size after 4.0 MB
Total size change +15.3 KB
Path Before After Change
crates/engine/src/tests.rs 3.6 MB 3.6 MB +8.4 KB
crates/engine/src/cmd/imagery.rs 163.8 KB 168.8 KB +5.0 KB
crates/engine/src/cmd/raster.rs 77.2 KB 78.6 KB +1.4 KB
crates/engine/src/tests/cancellation.rs 41.6 KB 42.0 KB +447 B
crates/engine/src/cmd/workflows.rs 142.5 KB 142.6 KB +78 B
crates/engine/command-surface.txt 25.5 KB 25.5 KB +5 B

@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Commit 07d9050 from a fork built, but isn't published until a maintainer adds the preview label (once per commit).

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

raster.mosaic now runs through a background workflow. It validates and joins raster inputs with cancellation checks, stages optional GeoTIFF output, and reports notices and errors through the workflow result.

Changes: Background Raster Mosaic

Layer / File(s) Summary
Command and background registration
crates/engine/command-surface.txt, crates/engine/src/cmd/imagery.rs, crates/engine/src/cmd/workflows.rs, crates/engine/src/tests.rs
The command signature and parameters include ran. The background workflow registry dispatches raster.mosaic to its work function. Workflow tests cover ran inputs and use the shared start, background-run, and land flow.
Input validation and mosaic assembly
crates/engine/src/cmd/imagery.rs
The workflow handles layer and file inputs, validates tile grids and bounds, and joins rasters in input order with cancellation checks. It preserves the first raster’s band metadata and copyright and records notices for differing metadata.
Output reporting and workflow validation
crates/engine/src/cmd/raster.rs, crates/engine/src/cmd/imagery.rs, crates/engine/src/tests.rs, crates/engine/src/tests/cancellation.rs
Loss reporting returns formatted notice lines. Staged GeoTIFF writing checks progress, and write_and_report always requests a COG. Tests compare synchronous and threaded mosaics and check cancellation, failures, and staged-file cleanup.

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

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Command as raster.mosaic
  participant Work as mosaic_rasters_work
  participant Inputs as Raster inputs
  participant Tiles as Tiles
  participant Writer as Staged writer
  participant Layer as Result layer
  Command->>Work: dispatch background work
  Work->>Inputs: read layer and file inputs
  Work->>Tiles: validate tiles with Tiles::take
  Work->>Tiles: assemble raster with Tiles::join
  Work->>Writer: stage optional GeoTIFF
  Work->>Layer: return layer result
  Layer->>Writer: commit staged file after addition
Loading

Possibly related issues

  • opengeos/RustGIS#2907: This change adds the background mosaic workflow, cancellation checks, and staged GeoTIFF output described in the issue.
  • opengeos/RustGIS#2641: This change adds raster.mosaic to the background path and updates cancellation test coverage for the command.

Suggested reviewers: giswqs, pick2510

Merge Risk: Minimal · up to 07d90

raster.mosaic now runs as a cancellable background job, and the other entry points keep their behavior. No concrete defect was found in the supplied evidence. The author did not test the app window, the browser build or large-raster timing, but no app window starts this run yet.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the feature (Mosaic Rasters on a background thread with Cancel) and the PR number. The changeset moves raster.mosaic onto the background-work path with cancellation, so the title match…
Description check Passed The description covers the change, its scope, permissions, memory impact, failed-run differences, and tests. It follows the template's Summary and Testing sections. The UI evidence section is handled …
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 names the feature (Mosaic Rasters on a background thread with Cancel) and the PR number. The changeset moves raster.mosaic onto the background-work path with cancellation, so the title matches the main change. It is a single sentence of 12 words and does not use vague terms.

Full details: Description check

Explanation

The description covers the change, its scope, permissions, memory impact, failed-run differences, and tests. It follows the template's Summary and Testing sections. The UI evidence section is handled with 'No visible UI change.' The Related issue section names #2907 only in prose and does not use the template's Closes/Part of line, so the link is weaker than the template asks for. The template's required sections are otherwise present, so the check passes.

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 checks the tiles in rows,
Then joins their colors as it goes.
It pauses when the work must cease,
And stages files with careful ease.
The mosaic lands; the notices appear,
While carrots wait for workers here.

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: 0c43b36f-1e8c-4bad-8d24-3de12175d8da
Commits

Reviewing files that changed from the base of the PR and between ced7c3d and 07d9050.

Files selected for processing: 6

  • crates/engine/command-surface.txt
  • crates/engine/src/cmd/imagery.rs
  • crates/engine/src/cmd/raster.rs
  • crates/engine/src/cmd/workflows.rs
  • crates/engine/src/tests.rs
  • crates/engine/src/tests/cancellation.rs

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

let (dc, dr) = ((pc - c0) as usize, (pr - r0) as usize);
let (rows, width) = (r.height as usize, r.width as usize);
let piece = (MOSAIC_PIECE / width.max(1)).max(1);
for (out, b) in bands.iter_mut().zip(&r.bands) {

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.

Performance (not a defect, so it may wait for the release quality pass): the copy runs on one thread, but the bands are independent parts. Each band writes only its own out buffer and reads only its own r.bands[i], so the band loop could run on rustgis_data::threads workers (scoped, with Builder::spawn_scoped, and one thread below a size threshold) with the same output bit for bit. AGENTS.md's "Use the threads" rule asks for this where practical. The PR does not say whether threading was considered, and its timings are not measured, so this is worth a look once the thread is in place.

Category: performance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed it's worth doing, and left for later, on purpose:

  • This PR only moves the run. The band loop in Tiles::join (crates/engine/src/cmd/imagery.rs, from line 2165) is the one on main (e7f6bcf2, line 2162), with Cancel and progress added. Its cells, answer and file are checked equal to a run in place (a_mosaic_made_on_another_thread_is_the_one_made_here), and threading the copy is a change of another kind, to measure on its own.
  • Cancel and progress are per thread. rustgis_data::progress keeps them in a thread_local! (crates/data/src/progress.rs, line 25), so workers would need the run's state handed to them, and the count long_commands sweeps (crates/engine/src/tests/cancellation.rs) would no longer be in a fixed order.
  • Nothing is measured yet: a single-band mosaic, the common case, has one part, so a gain would have to be shown on multi-band inputs first.

It fits #2907's part 2 or a follow-up of its own, with timings.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs or security issues in the changed code. The main risks I checked (cancel and staging, the ran hand-over, alignment overflow, and the write_and_report and imagery::add callers) look correct. I posted one inline performance note. Confidence in the findings below is high unless stated.

Bugs: None found.

  • Checked write_plain_staged: wrote_geotiff only builds notes and does not write, so the file is written once, through staging.
  • Checked Tiles::take: the u32::MAX bound on the transform offset guards the pc + width addition against overflow.
  • Checked the then and said ordering in the thread closure: no error can occur after std::mem::take(&mut notes), so with_notes is never given an emptied list. Confidence: high.
  • Checked the ran hand-over: continuation_owner requires the same caller, so a plugin cannot take another caller's run.
  • Checked the callers: write_and_report's two callers are updated, the RESULT_BAND string is unchanged, and no stale imagery::add( calls remain.

Security: None found. The ran path is covered by the dispatch-time mosaic_stays_in_project check, and the new tests exercise it.

Performance:

  • The band copy in Tiles::join is single-threaded, though the bands are independent (inline comment on imagery.rs:2170). Confidence: medium. The rule is "where practical", and the PR does not discuss it.
  • Path runs encode the whole result twice: write_plain_staged encodes band 1, then layer_of encodes all bands. This predates the PR, so I did not flag it. Confidence: high.

Quality:

  • Tiles::join shadows the progress total rows inside the loop. The author has already deferred this, so I left it alone.
  • The assert! tuple nit in tests.rs is already raised, and the author answered it. I left it alone.
  • The test's temp directory is removed only on success, so a failing run leaves it behind. Minor.

AGENTS.md: No rule breaks found. Checked against the "Never silent", "Never hold the window", "Size for real data", and "Reach every surface" sections, and the cancellation and command-surface lists were updated.

  • The desktop direct path (lib.rs:3399) will start raster.mosaic on a thread for any call without ran. The PR says nothing sends it there yet, which holds for the Script pane and the Assistant. A stray direct call would fail at start with the same error as before.
  • The memory multiples are stated as from the code, not measured, which the PR says.

Already raised, not re-posted: 1 (the assert! nit on tests.rs, answered by the author).

Change size

Compared with the base branch: the whole PR against main (merge base e7f6bcf to PR head 07d9050).

Measure Value
Files changed 6 (0 added, 6 modified, 0 removed, 0 renamed)
Size before 4.0 MB
Size after 4.0 MB
Total size change +15.3 KB
File Before After Change
crates/engine/src/tests.rs 3.6 MB 3.6 MB +8.4 KB
crates/engine/src/cmd/imagery.rs 163.8 KB 168.8 KB +5.0 KB
crates/engine/src/cmd/raster.rs 77.2 KB 78.5 KB +1.4 KB
crates/engine/src/tests/cancellation.rs 41.6 KB 42.0 KB +447 B
crates/engine/src/cmd/workflows.rs 142.5 KB 142.6 KB +78 B
crates/engine/command-surface.txt 25.5 KB 25.5 KB +5 B

@Mavrick-F
Mavrick-F added this pull request to the merge queue Nov 26, 2026
Merged via the queue into opengeos:main with commit 329ec97 Nov 26, 2026
29 checks passed
MikeFTV pushed a commit to MikeFTV/RustGIS that referenced this pull request Nov 27, 2026
…a thread (opengeos#2907) (opengeos#3039)

* feat(workflows): a Mosaic Rasters section that runs raster.mosaic on a thread (opengeos#2907)

The Workflows pane's Mosaic Rasters section lists the rasters to join in
order (layers, and on the desktop files not on the map), a name, and on
the desktop a GeoTIFF to write or none. It runs through the pane's run,
so the desktop app starts raster.mosaic on a thread of its own with its
progress and Cancel in the pane (the path from opengeos#2936). It is listed in
the Workflows menu, and Command Search now opens it instead of greying
raster.mosaic.

Closes opengeos#2907

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(workflows): another project opening empties the Mosaic list of its layers (opengeos#2907)

The list's layers were dropped by name only, so a raster of the same name in
the newly opened project kept its row and Mosaic joined that layer instead.
The section now remembers the project its layers are from (Session::opened)
and drops them, files kept, when another one opens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(workflows): the Mosaic list offers a name two layers share once, and says so (opengeos#2907)

Commands find a raster by its name, case aside (layer_by_name), so with
"DEM" listed the section offered "dem", which the mosaic read as "DEM"
again. Names are now compared case aside, a name an earlier layer has is
offered once, and the section says only the first can be joined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(workflows): the Mosaic list follows the map shown, and leaves out rasters named as a vector layer (opengeos#2907)

The list's layers stayed when another map of the project was shown, so a
same-named raster there was joined instead; the list is now tied to the
project and its active map. A raster named, case aside, as a vector layer
before it was offered and then refused by the command (layer_by_name finds
the vector layer); it is now left out with the shared-name notice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(workflows): the Mosaic list knows its map by name, and waits while a listed name is shared (opengeos#2907)

Deleting the map shown moved the next one to its index, so the list's
layers stayed and a same-named raster there was joined; maps have names
of their own, so the list is now tied to the shown map's name. A listed
layer whose name another layer comes to have (renamed, or moved before
it) would be read as the first one; Mosaic is now off until one is
renamed, and the shared-name notice names it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: elpidioassinaturas <elpidiofilho@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants