Fields — geopatcher.fields¶
Field adapters for every substrate other than a geoarray.RasterSource
(RasterField, at the root), and the domain types samplers plan over.
The extras-gated adapters resolve lazily, so importing the module never
needs an optional dependency.
Raster readers¶
geopatcher.fields.AsyncRasterField
dataclass
¶
Async mirror of RasterField over a geoarray.AsyncRasterSource.
select is a coroutine (reader.aread_window); otherwise the
surface is identical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reader
|
AsyncRasterSource
|
Any |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/raster.py
geopatcher.fields.ReprojectingRasterField
dataclass
¶
Wrap a geoarray.RasterSource, presenting a reprojected destination grid.
The domain reports the destination CRS / transform / shape, so
samplers place anchors on the reprojected grid; each select warps
the corresponding source pixels into that chip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reader
|
RasterSource
|
Any |
required |
dst_crs
|
Any
|
Destination CRS the domain (and every chip) is expressed
in — |
required |
resolution
|
float | tuple[float, float] | None
|
Destination pixel size in |
None
|
resampling
|
str
|
Resampling method — |
'bilinear'
|
The domain shape keeps the source's leading (band / time) dims —
(*reader.shape[:-2], height, width) — so chips and the merged
output agree on rank.
Each select first crops the source to the chip's footprint (plus a
small pixel margin for the resampling kernel, scaled up when the
destination is coarser than the source) and warps only that crop, so
per-chip cost scales with the chip, not the whole source.
Note
Chips are warped independently, so a stitched result can differ
slightly from one full-image warp of the same source — GDAL's
approximate transformer linearises per warp call, which shifts
sample positions by a small fraction of a pixel (with bilinear
resampling, differences stay below one ramp step on a
unit-increment ramp). The source crop itself does not change the
result: a chip is identical to warping the uncropped source onto
the same chip grid. Warp the whole source once (geoops.geom's
reprojection) when bit-exactness against a full warp matters.
Raises:
| Type | Description |
|---|---|
TypeError
|
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/reproject.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
cache_id()
¶
PatchCache identity: the source plus the warp parameters.
Folds in the source reader's identity (None for an in-memory
source — PatchCache then requires an explicit field_id),
dst_crs, resolution and resampling, so two
reprojections of one source never share cache entries.
Examples:
>>> reader = source_from_georeader(RasterioReader("a.tif"))
>>> ReprojectingRasterField(reader, "EPSG:3857").cache_id()
'{"dst_crs": "EPSG:3857", "resampling": "bilinear", ...}'
>>> ReprojectingRasterField(scene, "EPSG:3857").cache_id()
'{"dst_crs": "EPSG:3857", ..., "source": null}'
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/reproject.py
select(window)
¶
Warp the source into window of the destination grid.
Returns:
| Type | Description |
|---|---|
GeoArray
|
|
GeoArray
|
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/reproject.py
with_data(array)
¶
array ((..., H, W) on the destination grid) as a GeoArray.
Cloud-Optimized GeoTIFFs ([cog])¶
CogField is geotoolz-cloud's CogSource as a
Field: tiles a batch of windows touches are fetched once, in concurrent
groups, and parallel_map / AsyncSpatialPatcher use the batched and
async paths automatically.
geopatcher.fields.CogField
¶
Bases: CogSource
A tiled COG Field with batched range-fetch reads.
Open with :meth:open (await :meth:aopen inside an event loop);
every argument is geocloud.cog.CogSource.open's. Patches are
geoarray.GeoArray chips (bands, h, w) with the window's transform,
the CRS and the COG's nodata as fill_value. The field's domain is
geocloud.cog.CogDomain (a RasterDomain: grid and shape). The
field pickles by URL, so it ships to process-pool workers.
Examples:
Patch a remote scene; parallel_map reads each chunk of patches
with one batched fetch::
field = CogField.open("s3://bucket/scene.tif")
patches = list(patcher.split(field))
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/cog.py
aselect(window)
async
¶
Async twin of :meth:select (the AsyncField spelling).
aselect_many(windows)
async
¶
cache_id()
¶
PatchCache identity: the object read, its version and the IFD.
CogSource.identity (the URL — or the explicit store's
configuration plus path — and the IFD) plus
CogSource.object_version (one HEAD per split: the ETag,
else size + last-modified), so an object overwritten in place
invalidates its cache entries.
Raises:
| Type | Description |
|---|---|
UnstableIdentityError
|
The explicit |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/cog.py
select(window)
¶
select_many(windows)
¶
Read every window, fetching each overlapping tile once (read_windows).
with_data(array)
¶
Wrap an operator output on the full grid as a geoarray.GeoArray.
Carries the COG's grid (transform, CRS), attrs and nodata, by the
rule every raster with_data follows (RasterField.with_data):
the COG's nodata while the output carries its values and its dtype
holds it, NaN for a new float quantity, else no fill.
Raises:
| Type | Description |
|---|---|
TypeError
|
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/cog.py
Gridded N-D data ([grid], [xarray-raster], [dask])¶
geopatcher.fields.XarrayField
dataclass
¶
Wrap an xarray.DataArray as a Field[GridDomain].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
Any
|
The underlying |
required |
domain is built on first access and cached; treat da as
immutable once wrapped.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/xarray.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
coords_per_patch(patches)
¶
xrpatcher.get_coords() equivalent — one coord Dataset per patch.
For each patch, run self.da.isel(**patch.indices).coords.to_dataset()
so the caller gets the coord values aligned with the patch slice
without re-reading data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patches
|
Iterable[Any]
|
Iterable of |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
|
list[Any]
|
containing only the coord variables (no data variables). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a patch's |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/xarray.py
placeholder(indexer, data)
¶
data as the DataArray select would return, without reading.
Used for the on_error="mask" placeholder. The chip's coords
come from the (lazy) slice; when its shape differs from data
(a padded window past the edge) data is returned bare.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/xarray.py
select(indexer)
¶
Read a sliced xarray.DataArray patch.
Returns the bare DataArray (not another XarrayField) so the
result is np.asarray-able and feeds straight into the spatial
aggregations. Mirrors RasterField.select → GeoArray: select
returns the natural data payload, not another field wrapper. Use
XarrayField(da_sliced) if you need to keep treating the slice
as a sub-Field.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/xarray.py
time_coord(name='time')
¶
Return the 1-D time coordinate as a NumPy array.
Helper for the coordinate-aware temporal patcher path. Resolves the
coordinate by name (defaults to "time") and materialises its
values so callers don't repeat ds[name].values boilerplate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the time-like coordinate. Defaults to |
'time'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
ndarray
|
unit xarray exposes for that coord). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
TypeError
|
If the coordinate is |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/xarray.py
geopatcher.fields.RioXarrayField
dataclass
¶
Wrap a rioxarray-flavoured xarray.DataArray as a raster Field.
The domain view exposes the pixel grid (a geoarray.Grid from the
rasterio affine and the CRS) and the array shape — the RasterDomain
protocol — so it takes the same raster patching path as RasterField.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
Any
|
An |
required |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/rio_xarray.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
placeholder(window, data)
¶
data as a chip of window: a DataArray on its grid, no read.
Used for the on_error="mask" placeholder, so a failed read
has the carrier and georeferencing of a successful select.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/rio_xarray.py
select(window)
¶
Return the window as a georeferenced xarray.DataArray.
The chip carries an explicit window transform
(rasterio.windows.transform(window, domain.transform)) and,
when the source has spatial coordinates, coordinates rebuilt from
it — so chip.rio.transform() / chip.rio.bounds() are exact
whether the source is coordinate-bearing or write_transform-only.
Windows that extend past the raster are read boundless, like
RasterField: the in-range part is read and the rest padded to
the requested size with rio.nodata (0 when unset), so the
chip shape always equals the window shape. The returned chip is
materialised (.compute()), also for dask-backed sources.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/rio_xarray.py
geopatcher.fields.DaskField
dataclass
¶
Wrap a dask-backed xarray.DataArray as a lazy grid Field.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/dask.py
from_zarr(store, *, var=None, **kwargs)
classmethod
¶
Open one variable of a zarr store with xarray as a DaskField.
xr.open_zarr returns a Dataset; the field wraps one of its
data variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
Any
|
Anything |
required |
var
|
str | None
|
Data variable to wrap. May be omitted when the store holds exactly one data variable. |
None
|
**kwargs
|
Any
|
Forwarded to |
{}
|
Raises:
| Type | Description |
|---|---|
KeyError
|
|
ValueError
|
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/dask.py
select(indexer)
¶
Read one patch as a materialised xarray.DataArray.
Returns the computed slice (not another DaskField), so the
payload is np.asarray-able and feeds the spatial aggregations
directly — mirroring XarrayField.select. Coordinates come along
with isel; when the source is georeferenced through rioxarray
(it carries a grid-mapping coordinate) the chip also gets an
explicit window transform, since rio.transform() on a
coordinate-free slice would otherwise report the full-array affine.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/dask.py
Vector features and points ([vector], [point])¶
geopatcher.fields.GeoPandasField
dataclass
¶
Wrap a geopandas.GeoDataFrame as a Field[VectorDomain].
If every geometry is a shapely.Point, callers can ask for a
PointDomain view via the as_points=True flag — that branch
builds a cKDTree on the point coordinates so KNNGraph /
RadiusGraph queries are cheap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
Any
|
The underlying |
required |
as_points
|
bool
|
If |
False
|
domain (including its spatial index / cKDTree) is built on
first access and cached; treat gdf as immutable once wrapped.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/geopandas.py
select(indexer)
¶
Return the sub-field at indexer — positional indexing only.
indexer is a boolean mask, an integer position array, or a
positional slice, all resolved via gdf.iloc. Row labels are
never consulted: samplers emit positions into the domain
(PointDomain.coords / VectorDomain.geometry order), so a
label fallback would silently read the wrong rows whenever the
index is not a RangeIndex.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/geopandas.py
with_data(array)
¶
Attach one value per row as a _value column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
Any
|
A 1-D array with exactly one entry per row of |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/geopandas.py
geopatcher.fields.XvecField
dataclass
¶
Wrap a vector data cube (xarray + xvec) as a Field[PointDomain].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Any
|
An |
required |
geometry_dim
|
str
|
Name of the geometry dimension / coordinate.
Defaults to |
'geometry'
|
domain (including its cKDTree) is built on first access and
cached; treat ds as immutable once wrapped.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/fields/xvec.py
Domains¶
geopatcher.fields.RasterDomain
¶
Bases: Protocol
A raster domain: a geoarray.Grid plus the full array shape.
The samplers and geometries plan windows on grid (transform, CRS,
(H, W)); the aggregations size their output from shape
((..., H, W), leading band / time axes included). Every
geoarray.RasterSource satisfies it, so RasterField.domain is the
source itself and costs no read; so do geocloud.cog.CogDomain and the
reprojected / rioxarray domain views. A bare geoarray.Grid is also
accepted wherever a raster domain is (its shape is (H, W)), to plan
anchors before any data exists.
Attributes:
| Name | Type | Description |
|---|---|---|
grid |
Grid
|
The pixel grid. |
shape |
tuple[int, ...]
|
|
Example
import numpy as np from geoarray import GeoArray scene = GeoArray( ... np.zeros((4, 64, 64), np.float32), ... (10.0, 0.0, 500000.0, 0.0, -10.0, 4600000.0), ... "EPSG:32630", ... ) isinstance(scene, RasterDomain), scene.grid.shape (True, (64, 64))
Source code in packages/geotoolz-patcher/src/geopatcher/_src/domains.py
geopatcher.fields.GridDomain
dataclass
¶
A dense, labeled N-D grid — the xarray.DataArray shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
dict[str, ndarray]
|
|
required |
crs
|
Any | None
|
CRS of the spatial dims (lat/lon, x/y). Optional — non-georeferenced cubes are fine. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
shape |
tuple[int, ...]
|
|
bounds |
dict[str, tuple[Any, Any]]
|
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/domains.py
geopatcher.fields.VectorDomain
dataclass
¶
Vector geometries — polygons / lines indexed by an STRtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
Any
|
A |
required |
sindex
|
Any
|
The |
required |
crs
|
Any
|
The series' CRS. |
required |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/domains.py
geopatcher.fields.PointDomain
dataclass
¶
Scattered points — coordinates + a cKDTree for nearest-neighbor lookups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
|
required |
kdtree
|
Any
|
A |
required |
crs
|
Any | None
|
Coordinate reference system, or |
None
|
interp
|
str
|
Interpolation used by |
'nearest'
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/domains.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
sample(raster)
¶
Extract raster values at this domain's points.
Uses the domain's interp mode: "nearest" reads the pixel
containing each point, "bilinear" interpolates between the
four surrounding pixel centres (clamped at the raster edge).
Points falling outside the raster extent return NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster
|
Any
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
ndarray
|
|