Skip to content

fix(engine): give a tool a layer's pixels, keep a turned GeoPDF's layers within the page's samples (#3442 part 5a) - #3584

Open
elpidioassinaturas wants to merge 2 commits into
opengeos:mainfrom
elpidioassinaturas:feat/3442-geopdf-layers-part5a
Open

elpidioassinaturas wants to merge 2 commits into
opengeos:mainfrom
elpidioassinaturas:feat/3442-geopdf-layers-part5a

Conversation

@elpidioassinaturas

@elpidioassinaturas elpidioassinaturas commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Part of #3442 (part 5a of 5).

Points left from the reviews of parts 2 to 4, each with a test written first.

A tool reads the layer, not its whole file (T25)

A raster layer that is part of its file reached a geoprocessing tool as the file: the path on disk or the embedded bytes. A suite tool then read the file whole: a PDF layer gave the tool the whole of page 1, and a PDF opened at page 2 gave it page 1 too.

MapInputs::raster now gives such a layer's pixels instead: no path and no embedded file when the layer has a subdataset. This is not limited to PDFs. It covers any layer with a subdataset: a PDF's layer or page, a NetCDF or HDF variable read through GDAL, a Zarr slice. Each of those reaches a suite tool as a GeoTIFF of its pixels, as a layer with no file already does. A layer that is a whole file is still given as the file.

Test: a_layer_that_is_part_of_its_file_reaches_a_tool_as_its_pixels, covering PDF layers and pages from a file and from an upload, and a NetCDF variable.

A turned GeoPDF's layers stay within the page's samples (T19)

A GeoPDF whose map is turned on the page is placed north up by placed_by_world_file, which turns it within all of max_samples(). The page's layers are drawn in shares of those samples, but each was turned within all of them. A 45° turn spreads a layer over about twice its area, so the layers together could take about twice what a page may hold.

Raster::placed_by_world_file_within takes the limit. Each PDF layer is turned within its share, and a page read as one picture within the limit it was drawn in.

Test: layers_of_a_turned_page_are_turned_within_the_samples_a_page_has. Without the fix, the layers take 53,824 samples of 30,000.

One copy of the file for all the drawings (T17)

Each layer is drawn from a copy of the PDF whose catalog an update at its end rewrites, and every drawing copied the whole file again: about 65 copies of a 64-layer PDF. with_only now rewrites only the update at the end of one copy, kept between drawings. The copy is reserved with try_reserve_exact, so an allocator refusal becomes an error; it is not counted by the memory planner (memory::planned), as the copies before were not.

Test: each_drawing_rewrites_the_update_at_the_end_of_one_copy.

  • Size for real data: while a PDF's layers are read, memory holds the caller's bytes and the one copy, about 2× the PDF's size (up to 2 GB for the 1 GB a PDF may have), besides the drawings. Before, each drawing made its own copy and dropped it, so the peak was about the same, but a 64-layer PDF was copied about 65 times. Counting the copy in memory::planned is a change of its own, left for later.
  • Surfaces: PDF pages are read only where the pdf feature is on: the desktop app, the CLI and its MCP server, and the Python package. The web app and mobile don't read PDFs, so they aren't reached.
  • Undoing it: go back to copying the file in each drawing: with_only returns bytes.to_vec() with the update, and draw_with drops its kept copy.

Smaller fixes

  • T26: a file is now shared only through a layer whose id no other layer has. When two layers have one id (a project edited by hand or corrupted), an id read back can't tell them apart, so a file of theirs is written whole, and the layer that shares it gets the right file whatever the order. embedded_same still names a layer by its id, so projects saved before open as they did. Test: a_shared_file_is_read_back_though_two_layers_have_one_id, in both orders.
  • T21: the placing notes said once for all the layers are now taken in a loop that keeps the first layer's notes, instead of map(take).last().

Tests

Local runs on the touched crates (map, data with pdf, engine), with fmt and clippy -D warnings, all pass, except portable_projects::reload::wrong_geometry_missing_file_and_missing_chosen_table_leave_the_layer_unchanged, which fails the same way on main on this machine. cloud_optimised_geotiffs_are_read_in_place failed once under the full parallel run and passed on its own and on the next full run.

Next

🤖 Generated with Claude Code

…ers within the page's samples (opengeos#3442 part 5a)

A raster layer with a subdataset (a PDF layer or page, a NetCDF variable)
reaches a geoprocessing tool as its pixels, not as its whole file. Each
layer of a turned GeoPDF is turned within its share of the samples. The
layers are drawn from one copy of the file whose update is rewritten,
and a shared embedded file is read back from the first layer of its id.

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

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes add caller-supplied sample limits to rotated raster placement and apply those limits during PDF layer rendering. They also reuse a PDF update buffer, restrict file inputs for subdataset layers, and preserve embedded files for layers with duplicate IDs.

Changes: PDF Raster Rendering

Layer / File(s) Summary
Sample-limited raster placement
crates/data/src/raster/mod.rs
The existing world-file placement method delegates with the default sample limit. A new method accepts a caller-supplied limit for rotated or skewed rasters.
PDF layer rendering and buffer updates
crates/data/src/raster/pdf.rs
PDF rendering divides the sample limit among layer content and passes each share to viewport placement. PDF updates reuse a mutable buffer, and placement notes are appended once. Tests cover buffer reuse and rotated GeoPDF placement.

Raster Layer File Inputs

Layer / File(s) Summary
Embedded file serialization with duplicate IDs
crates/map/src/raster.rs
Serialization shares embedded files only for layer IDs that occur once. Layers with duplicate IDs retain their files. A regression test checks restored files in either input order.
Raster inputs for tools
crates/engine/src/cmd/gp.rs
Raster pixels remain available for whole-file and subdataset layers. File paths and embedded bytes are provided only for whole-file layers. Tests cover PDF, NetCDF, path-backed, and embedded inputs.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant PdfRendering
  participant place_by_viewport
  participant placed_by_world_file_within
  participant north_up
  PdfRendering->>place_by_viewport: layer raster and sample share
  place_by_viewport->>placed_by_world_file_within: world file and sample limit
  placed_by_world_file_within->>north_up: rotated raster and sample limit
Loading

Possibly related issues

Possibly related PRs

  • opengeos/RustGIS#3486: Both changes cover GeoPDF optional content layers as separate rasters. This change also adjusts the inputs supplied for those subdataset layers.

Suggested reviewers: mavrick-f, flight1973, giswqs

Merge Risk: Minimal · up to 46754

This change gives tools a layer's pixels when the layer is only part of its file. It keeps turned GeoPDF layers within the page's sample limit, reuses one PDF copy between drawings, and fixes file sharing when layer IDs collide. No concrete merge-blocking problem was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two main changes: a tool receives a layer's pixels for subdataset layers, and a turned GeoPDF's layers stay within the page's sample limit. It also cites part 5a of #3442. It is sp…
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.
Description check Passed The description follows the template's Summary, Testing, and Related issue sections, and it states a clear, specific purpose. It explains three fixes (T25, T19, T17) and two smaller ones (T26, T21), e…
Full details: Title check

Explanation

The title names the two main changes: a tool receives a layer's pixels for subdataset layers, and a turned GeoPDF's layers stay within the page's sample limit. It also cites part 5a of #3442. It is specific, and the changeset covers both points. The title does not mention the smaller changes (single PDF copy reuse, duplicate-ID sharing, placing notes), which is acceptable because the title need not cover every detail.

Full details: Description check

Explanation

The description follows the template's Summary, Testing, and Related issue sections, and it states a clear, specific purpose. It explains three fixes (T25, T19, T17) and two smaller ones (T26, T21), each with a named test. The Testing section lists the local runs and one known failing test that also fails on main. The UI evidence section is missing. The description does not say 'No visible UI change.' or add screenshots, so the required UI evidence line is absent. The Related issue section is also left as the template placeholder, although the description says 'Part of #3442'. The description is otherwise mostly complete, so the check passes, but the author should fill in the UI evidence and Related issue sections.

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 raster’s turn,
And keeps each sample in its share.
The PDF buffer gets reused,
While layer files stay matched with care.
For duplicate names, bytes remain,
Then bunny hops through fields again.

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: 2c2fb29e-c6b6-4ded-8aaa-f4f722089f9f
Commits

Reviewing files that changed from the base of the PR and between c9d31c3 and 467546f.

Files selected for processing: 1

  • crates/map/src/raster.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/map/src/raster.rs Resolved
Comment thread crates/data/src/raster/pdf.rs Resolved
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Code review

Bugs

  • crates/map/src/raster.rs:1929: the first-wins change to shared_files::deserialize attaches the wrong file when a hand-edited project has duplicate layer ids. For the saved list [(1, Y), (1, X), (2, X)], layer 2 now silently gets Y, where the old last-wins code gave X. The new test covers only the ordering that first-wins gets right. Confidence: high on the reasoning, medium on how likely the case is in practice. Inline comment posted; a fix should key the shared file by its unique position in the saved list.

Security

  • None found.

Performance

  • crates/data/src/raster/pdf.rs: the per-drawing copy of the PDF is now one reused copy, down from about 65 copies. Peak memory is about the same as before. Confidence: high.

Quality

  • The placed-notes loop at pdf.rs (the said block) works, but get_or_insert ignores its result and the loop consumes every layer's notes. A one-line first read would say the intent more directly. Nit, not posted.
  • The shared_files test covers one ordering only (see the bug above).

AGENTS.md

  • The PR body gives no memory multiple for the new persistent PDF copy, and no statement of which surfaces the change reaches or how it can be undone. Inline comment posted on pdf.rs:381. Confidence: medium.

Checked, no finding

  • Subdataset layers (gp.rs MapInputs::raster): the tool gets pixels, and stage in geolibre writes them as GeoTIFF when there is no file. The tiled path is unaffected because it only applies to GeoTIFF layers. Other Inputs implementations (analysis tests, geolibre tests) pass path: None already.
  • PDF read routes: the file and upload routes both go through layers_limited and place_by_viewport, so they stay consistent.
  • Sample accounting: north_up checks ow*oh*nbands against max, so the per-layer share = max / (n+1) matches scale_for's four-band accounting.
  • Not verified: I could not build or run the tests here (no shell). PdfData::from(Arc<Vec<u8>>) is used without a local hayro source to confirm it exists.

Prior concerns: none; no prior review threads existed, so nothing was left alone.

Change size

Compared with the base branch: the whole PR against main (merge base c89014f to PR head c9d31c3).

Measure Value
Files changed 4 (added 0, modified 4, removed 0, renamed 0)
Size before 838.2 KB
Size after 846.1 KB
Total size change +7.9 KB
File Before After Change
crates/data/src/raster/pdf.rs 68.6 KB 72.1 KB +3.5 KB
crates/engine/src/cmd/gp.rs 376.3 KB 379.1 KB +2.7 KB
crates/map/src/raster.rs 228.2 KB 229.5 KB +1.3 KB
crates/data/src/raster/mod.rs 165.1 KB 165.5 KB +402 B

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3584/
Web app https://preview.rustgis.app/pr-3584/app/
Commit 467546f

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).

Two layers with one id (a project edited by hand or corrupted) can't be
told apart when read, so a file of theirs is written whole and a layer
sharing it gets the right file in either order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread crates/data/src/raster/pdf.rs Resolved
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Code review

Bugs

None found. I checked:

  • Shared files with duplicate ids (T26): serialize and deserialize agree in both orders. A referenced id is always unique, so the read-side map cannot pick the wrong layer.
  • Raster input for subdataset layers (gp.rs raster()): tiled is only set for layers with no subdataset (crates/map/src/raster.rs:681), so tiled_inputs never replaces a subdataset layer's pixels with the file's first raster.
  • with_only buffer reuse: the truncate restores identical bytes and offsets, and an error returns before any mutation. The unit test covers this.
  • Sample budget: north_up counts samples including bands, which matches the share budget.
  • Twins: every PDF read route (read_layers, read_limited, the path and upload routes) goes through layers_limited or picture, so all get the change. In the engine, only MapInputs implements the raster path that changed. The other Inputs impls are tests or defaults.

Confidence: high.

Security

None found.

Performance

Nothing beyond the copy lifetime noted under AGENTS.md below.

Quality

  • layers_limited: keeping only the first layer's placement notes is correct today, because every layer shares the viewports and the same share. If that ever stops being true, a later layer's note would be dropped without a message. Low confidence, and it is a nit, so I did not post it.

AGENTS.md

  • The copy of the PDF (pdf.rs:381) is allocated outside memory::planned and memory::Holding. try_reserve_exact does not refuse on overcommit Linux, so the PR body's "refused instead of aborting" is not a memory check. It also stays alive through the picture fallback, which does not need it. Posted inline. Medium confidence.

Prior reviews

I left 2 earlier concerns alone, both already answered: the size-multiple disclosure on pdf.rs and the duplicate-id sharing on map/src/raster.rs. I checked the second answer against the code and it holds.

Not run: I had no shell, so nothing was built or tested. cargo xtask schema should be a no-op, because the schema shape is unchanged and only the duplicate-id save output differs.

Change size

Compared with the base branch: the whole PR against main (merge base c89014f to PR head 467546f).

Measure Value
Files changed 4 (0 added, 4 modified, 0 removed, 0 renamed)
Size before 838.2 KB
Size after 846.5 KB
Total size change +8.3 KB
File Before After Change
crates/data/src/raster/pdf.rs 68.6 KB 72.1 KB +3.5 KB
crates/engine/src/cmd/gp.rs 376.3 KB 379.1 KB +2.7 KB
crates/map/src/raster.rs 228.2 KB 229.9 KB +1.7 KB
crates/data/src/raster/mod.rs 165.1 KB 165.5 KB +402 B

This branch has not been deployed

No deployments
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.

2 participants