Skip to content

Key STAC cache by AOI and fetch parameters (fixes silent cross-AOI collision) - #26

Merged
NewGraphEnvironment merged 6 commits into
mainfrom
25-dft-stac-fetch-cache-key-omits-aoi-secon
Jul 7, 2026
Merged

Key STAC cache by AOI and fetch parameters (fixes silent cross-AOI collision)#26
NewGraphEnvironment merged 6 commits into
mainfrom
25-dft-stac-fetch-cache-key-omits-aoi-secon

Conversation

@NewGraphEnvironment

Copy link
Copy Markdown
Owner

Summary

  • Fix silent cross-AOI cache collision in dft_stac_fetch(): cache files were keyed by source + year only, so a second AOI silently received the first AOI's raster masked to its own extent. Cache filenames now embed a hash of the AOI geometry (as WKB) and all fetch-affecting parameters (res, crs, dt, aggregation, resampling, stac_url, collection, asset): <year>_<key>.nc.
  • force = TRUE now overwrites the cached NetCDF (write_ncdf(..., overwrite = TRUE)) instead of erroring with "File already exists".
  • Docs updated (cache-keying note, @param force), NEWS entry, released as v0.2.3. Existing caches simply re-fetch; dft_cache_clear() reclaims orphaned old-format files.

Fixes #25

Related Issues

  • Relates to NewGraphEnvironment/sred-2025-2026#16

Test plan

  • devtools::test(): 192 pass, 0 fail (5 new local cache-key tests — determinism, geometry/parameter sensitivity, integer-vs-double res, sf-attribute immunity)
  • lintr clean on touched files (one pre-existing vignette lint, untouched)
  • E2E against live Planetary Computer STAC: two AOIs ~64 km apart → two distinct cache files, each raster matching its own AOI extent; re-run hits cache; force = TRUE overwrites without error
  • /code-check clean on each commit

Notes

Design details (WKB geometry hashing to avoid PROJ-version CRS drift, hashing post-resolution config values, why the issue's optional extent check was skipped) are in planning/archive/2026-07-issue-25-cache-key-aoi/.

🤖 Generated with Claude Code

NewGraphEnvironment and others added 6 commits July 6, 2026 17:00
The cache filename was <year>.nc under a per-source directory — no AOI
component. Two calls with the same source/year but different AOIs collided:
the second silently got the first AOI's raster masked to its own extent.

Add internal stac_cache_key(): rlang::hash() over the AOI geometry as WKB
plus res (coerced to double), target CRS, dt, aggregation, resampling, and
post-resolution stac_url/collection/asset. Filename becomes <year>_<key>.nc;
year stays out of the hash so all years of one call share a readable suffix.
Existing caches simply refetch; dft_cache_clear() reclaims the old files.

Relates to #25

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
force = TRUE routed to the fetch branch but write_ncdf errored on the
existing file ("File already exists"), so force could never bypass a stale
cache without a manual unlink. Pass overwrite = TRUE instead; unlike a bare
unlink() this still fails loudly if the file is locked (Windows). Document
in @param force that a previously returned raster backed by the same file
may pick up the rewritten contents.

Fixes #25

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Relates to #25

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@NewGraphEnvironment
NewGraphEnvironment merged commit f9509a7 into main Jul 7, 2026
1 check passed
@NewGraphEnvironment
NewGraphEnvironment deleted the 25-dft-stac-fetch-cache-key-omits-aoi-secon branch July 7, 2026 04:05
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.

dft_stac_fetch cache key omits AOI -> second area silently gets first area's raster

1 participant