Skip the source reads when you rerun a patcher on the same data — while
iterating on an operator, say. geopatcher.run.PatchCache is a
content-addressed on-disk cache: the second process reads only the
field’s domain metadata, never the pixels.
Cache a split¶
import numpy as np
import rasterio
from geoarray import GeoArray
import geopatcher as gp
from geopatcher.run import IndexedPatchView, PatchCache
field: gp.RasterField = gp.RasterField(
GeoArray(
np.random.default_rng(0).random((1, 256, 256), dtype=np.float32), # (1, 256, 256) float32
transform=rasterio.Affine(10, 0, 500_000, 0, -10, 4_300_000),
crs="EPSG:32611",
)
)
patcher: gp.SpatialPatcher = gp.SpatialPatcher(
geometry=gp.spatial.geometry.Rectangular(size=(64, 64)),
sampler=gp.spatial.sampler.RegularStride(step=(64, 64)),
window=gp.spatial.window.Boxcar(),
aggregation=gp.spatial.aggregation.OverlapAdd(),
)
cache: PatchCache = PatchCache(
"./.geopatcher_cache",
max_bytes=20 * 2**30, # 20 GiB, least recently used first out
field_id="synthetic-scene", # in-memory fields need a stable name
)
run1: list[gp.Patch] = list(patcher.split(field, cache=cache)) # 16 reads, 16 entries written
run2: list[gp.Patch] = list(patcher.split(field, cache=cache)) # 16 hits, zero source reads
print(cache.stats()) # {'hits': 16, 'misses': 16, 'bytes': …, 'entries': 16}
view: IndexedPatchView = IndexedPatchView(patcher, field, cache=cache) # random access, same cacheIt composes with journal= and prefetch=, and with
patcher.patch_at(field, anchor, cache=cache).
What the key covers¶
Each entry is keyed by sha256(field_id ‖ geometry and window config ‖ anchor).
field_id covers everything the field reads:
The source. A reader’s file paths (real path, mtime and size of every file), a
url, or theencoding["source"]file of anxr.open_dataset/rioxarray.open_rasterioarray.The domain. CRS, transform, shape and dtype, or a digest of the grid coordinates for
XarrayField.The reader’s band selection and boundless fill.
The adapter’s own
cache_id().CogFieldadds its store, path and IFD;ReprojectingRasterFielditsdst_crs,resolutionandresampling.
A custom Field can define cache_id() -> str. It is trusted, not
checked: it must change whenever the patches could. Raise
geopatcher.run.UnstableIdentityError rather than return an identity
that can change.
Pitfalls¶
In-memory fields (
GeoArray- orDataArray-backed) have no stable identity: passfield_id=, or the split raisesValueError. Do the same for a file-backedDataArrayyou changed in memory.Objects overwritten in place. A plain
urlcarries no version.CogFieldsends oneHEADper split (ETag, else size and last-modified); for other URL fields, put a version incache_id()or callcache.clear().Some carriers cannot be cached. Hits are bit-identical: a
GeoArraycomes back with its transform, CRS, fill and attrs, aDataArraywith its dims, coords, attrs and encoding. A carrier that cannot round-trip (aGeoDataFrame, an object array, adatetimeattribute) raisesTypeError; dropcache=for that field.Only real reads are stored.
on_error="mask"placeholders are not written. A damaged entry is a miss and is rewritten; writes are atomic.The size cap is per instance. With several processes writing one directory,
max_bytesapplies per writer. An entry larger thanmax_bytesis not stored, with aRuntimeWarning.Format changes orphan entries. Keys include a cache format version; after an upgrade that bumps it, old entries are never hit again. Call
cache.clear()to reclaim the space.Entries from before the switch to GeoArray (written with georeader
GeoTensorchips) still decode, asGeoArray. A field whose key changed with the switch (a georeader reader now behindsource_from_georeader, whose CRS text differs) misses once and is rewritten.