Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

geotoolz-patcher

Split a geospatial field into local patches, run an operator per patch, and stitch the outputs back into a global result — along four independently composable axes.

Where it comes from

A model trained on 256 × 256 chips cannot see a whole Sentinel-2 tile at once, and a 1 TB output does not fit in memory. So you cut the scene into overlapping pieces, run on each, and glue the answers back without seams. Every project writes that loop again, with its own edge cases.

geopatcher splits the loop into four choices you make separately: the Geometry (the shape of a patch), the Sampler (where patches go), the Window (how each cell is weighted) and the Aggregation (how local outputs merge). Any Field goes in — a raster, an xarray grid, GeoPandas polygons, xvec points — and any per-patch callable runs. It is the patcher of the geostack; see how the packages interlock.

SpatialPatcher: a Field is split along the Geometry, Sampler, Window and Aggregation axes, your operator runs on each Patch, and the aggregation merges the patches into a stitched field on the domain's grid

Install

pip install geotoolz-patcher                       # base — RasterField only
pip install 'geotoolz-patcher[patch-full]'         # every Field adapter and runner
ExtraPulls inNeeded for
[grid]xarraygeopatcher.fields.XarrayField
[vector]geopandas, shapelygeopatcher.fields.GeoPandasField
[point]xvecgeopatcher.fields.XvecField
[xarray-raster]rioxarray, xarraygeopatcher.fields.RioXarrayField
[streaming]zarr ≥ 3spatial.aggregation.OverlapAdd(streaming=True)
[dask]dask[bag], xarraygeopatcher.fields.DaskField, geopatcher.run.to_delayed
[jax]geotoolz-array[jax]JAX batches: PatchBatch.patches.to_jax() after geopatcher.run.batch_split
[cog]geotoolz-cloud[cog]geopatcher.fields.CogField
[patch-full]all of the aboveeverything except [pipekit]
[pipekit]pipekit-opsgeopatcher.integrations.pipekit (GridSampler, ApplyToChips, MergePatches)

pipekit-ops is not on PyPI yet: install the [pipekit] extra from a clone with uv sync --extra pipekit. A missing extra fails at the point of use with an ImportError that names it.

Quickstart

Cut a 512 × 512 raster into 128-pixel patches with 32 pixels of overlap, double each patch and stitch the result with Hann-feathered seams.

import numpy as np
import rasterio
from geoarray import GeoArray

import geopatcher as gp

# 1. Wrap a GeoArray (or any lazy geoarray.RasterSource) as a Field.
arr: np.ndarray = np.outer(np.linspace(0, 1, 512), np.linspace(0, 1, 512)).astype(np.float32)[None]  # (1, 512, 512) float32
field: gp.RasterField = gp.RasterField(
    GeoArray(arr, rasterio.Affine(10, 0, 500_000, 0, -10, 4_300_000), "EPSG:32611")
)

# 2. Compose the four axes.
patcher: gp.SpatialPatcher = gp.SpatialPatcher(
    geometry=gp.spatial.geometry.Rectangular(size=(128, 128)),
    sampler=gp.spatial.sampler.RegularStride(step=(96, 96)),        # 32 px overlap
    window=gp.spatial.window.Hann(),                                # feather the seams
    aggregation=gp.spatial.aggregation.OverlapAdd(),
)

# 3. Split → operate per patch → merge.
patches: list[gp.Patch] = list(patcher.split(field))               # 25 × GeoArray (1, 128, 128) float32
out: list[gp.Patch] = [p.with_data(np.asarray(p.data) * 2.0) for p in patches]  # 25 × (1, 128, 128) float32
stitched: np.ndarray = patcher.merge(out, field.domain)             # (1, 512, 512) float64

patch.anchor is the patch origin in pixels ((0, 0), (0, 96), …). merge returns a bare array; patcher.merge_to_field(out, field) returns a GeoArray with the source’s transform and CRS. The Hann taper never reaches zero, so every covered pixel — the scene’s edges included — comes back as data; see Window convention.

What’s inside

NamespaceWhat’s thereRead
geopatcherSpatialPatcher, AsyncSpatialPatcher, TemporalPatcher, SpatioTemporalPatcher; the Patch carriers; Field / Domain; RasterFieldConcepts · Core API
geopatcher.spatialthe four axes — geometry, sampler, window, aggregationBehaviour · API
geopatcher.temporalthe same four axes along time, plus stencilsTemporal patching · API
geopatcher.fieldsXarrayField, RioXarrayField, DaskField, GeoPandasField, XvecField, CogField, ReprojectingRasterField, the domain typesFields · API
geopatcher.matchedco-registered multi-source patching (MatchedField, MatchedSpatialPatcher, …)API
geopatcher.runparallel_map, prefetch_iterable, Dask and JAX bridges, PatchCache, IndexedPatchViewCache reads · API
geopatcher.observePatcherHook, PatchJournal, PatchErrorRecord, strict modeHooks · Journal
geopatcher.configaxis_envelope / from_config round-tripsAPI
geopatcher.integrations.pipekitGridSampler, ApplyToChips, MergePatches (re-exported as geoops.patch_ops)API

Cloud-Optimized GeoTIFF reads come from geotoolz-cloud; geopatcher.fields.CogField is their Field.

Is this the right tool?

Your jobUse
The operator works on the whole scene in memorythe operator directly — no patcher
Independent local work (inference, filters)SpatialPatcher + spatial.aggregation.OverlapAdd with a Hann or Tukey window
The output is bigger than RAMOverlapAdd(streaming=True) — Stream to disk
The operator needs a global statistic (scene mean / std)patcher.two_pass — Global statistics
Windows along time, or space × time cubesTemporalPatcher / SpatioTemporalPatcher — Temporal patching

Advanced — patching as a geotoolz operator

The [pipekit] bridge makes split → operate → merge one more Sequential, so a geotoolz operator runs tile by tile with no loop.

import numpy as np
import rasterio
from geoarray import GeoArray

import geopatcher as gp
import geoops as gz
from geoops.patch_ops import ApplyToChips, GridSampler, MergePatches

rng: np.random.Generator = np.random.default_rng(0)
scene: GeoArray = GeoArray(
    rng.integers(1, 10_000, size=(4, 512, 512), dtype=np.uint16),  # (4, 512, 512) uint16
    transform=rasterio.Affine(10, 0, 500_000, 0, -10, 4_300_000),
    crs="EPSG:32611",
    fill_value=0,
    attrs={"band_names": ["blue", "green", "red", "nir"]},
)
field: gp.RasterField = gp.RasterField(scene)
patcher: gp.SpatialPatcher = gp.SpatialPatcher(
    geometry=gp.spatial.geometry.Rectangular(size=(128, 128)),
    sampler=gp.spatial.sampler.RegularStride(step=(96, 96)),
    window=gp.spatial.window.Hann(),
    aggregation=gp.spatial.aggregation.OverlapAdd(),
)

ndvi: gz.Sequential = gz.DNToReflectance(scale=1e-4) | gz.NDVI(nir="nir", red="red")
tiled: gz.Sequential = gz.Sequential([
    GridSampler(patcher=patcher),          # field → 25 Patches     (4, 128, 128) uint16
    ApplyToChips(operator=ndvi),           # (4, 128, 128) uint16 → (128, 128) float64
    MergePatches(aggregation=gp.spatial.aggregation.OverlapAdd(), domain=field.domain),
])
ndvi_map: GeoArray = tiled(field)          # (512, 512) float64 · NaN = no data

The domain fixes the grid and the patches fix the bands, so NDVI’s one-band chips merge into one (H, W) map on the scene’s transform and CRS. The stack quickstart runs the same pipeline on real Sentinel-2 data.

Next steps