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-array

One georeferenced array for NumPy and JAX. A GeoArray holds the array, its affine transform, CRS, fill value and metadata, and knows which pixel sits where — imported as geoarray.

Where it comes from

A raster is an array plus where its pixels are. Keep the two apart and every function has to carry both; keep them together by subclassing the array type, and the first operation from another array library loses the georeferencing. JAX arrays cannot be subclassed at all.

GeoArray holds its array instead, so one carrier works on NumPy and on JAX. It is a JAX pytree: the data and a float64 transform are leaves, which jit, grad and vmap trace, and the stored dims, coordinates, CRS, fill value and attrs are static. A new transform (another scene, another patch) reuses compiled code, and gradients reach the georeferencing.

A GeoArray: the data (..., H, W) and the (6,) float64 transform are pytree leaves, traced by jit, grad and vmap; the dims, coordinates, CRS, fill value and attrs are static and key the compile cache; g.grid derives the Grid (transform, CRS and (H, W) without pixels), and a PixelWindow is a rectangle of pixels on it

Install

pip install geotoolz-array              # NumPy carriers, Grid, PixelWindow
pip install 'geotoolz-array[jax]'       # + JAX carriers and the pytree
pip install 'geotoolz-array[vector]'    # + footprint as a shapely polygon
pip install 'geotoolz-array[georeader]' # + GeoTensor <-> GeoArray
pip install 'geotoolz-array[xarray]'    # + to_xarray / from_xarray (CF spatial_ref)
ExtraPulls inNeeded for
(base)numpy, pyproj, affine, pipekit, pipekit-arrayGeoArray, GeoSwath, GeoPoints on NumPy data, geoarray.coords, geoarray.types, the dim-level functions, Grid, PixelWindow, FrozenAttrs
[jax]jaxJAX data, GeoArray.to_jax, jit / grad / vmap over carriers
[vector]shapelyfootprint
[georeader]georeader-spacemlgeoarray.interop.georeader (from_georeader, to_georeader)
[xarray]xarrayto_xarray / from_xarray, to_dataset, to_datatree; readable types on DataArrays

A JAX carrier keeps its transform in float64, so it needs jax.config.update("jax_enable_x64", True). The data keeps its own dtype.

Quickstart

Wrap a Sentinel-2 chip in UTM 13N, read its grid, then move it to JAX and jit a function of it.

import jax
import numpy as np

from geoarray import GeoArray, Grid
from geoarray.coords import SpectralAxis

jax.config.update("jax_enable_x64", True)

# Shapes: C = 2 bands (B11, B12), H × W = 600 × 600 pixels of 20 m
chip: GeoArray = GeoArray(
    np.zeros((2, 600, 600), dtype=np.float32),  # (C, H, W) float32
    (20.0, 0.0, 600000.0, 0.0, -20.0, 3550000.0),  # (a, b, c, d, e, f)
    "EPSG:32613",
    fill_value=np.nan,
    dims=("band", "y", "x"),
    coords=(SpectralAxis(names=("B11", "B12"), wavelength_nm=(1610.0, 2190.0)),),
)
b12: GeoArray = chip.sel(band="B12")  # (H, W) float32: dims y x
grid: Grid = chip.grid  # transform, CRS, (600, 600)
grid.bounds  # (600000.0, 3538000.0, 612000.0, 3550000.0)

on_device: GeoArray = chip.to_jax()  # (C, H, W) float32 · transform float64
total = jax.jit(lambda g: g.data.sum())(on_device)  # () float32

Slice it, cut a window by map bounds, and crop patches inside vmap: each result carries the transform of the pixels it holds.

import jax
import jax.numpy as jnp
import numpy as np

from geoarray import GeoArray, PixelWindow, crop, read_window

jax.config.update("jax_enable_x64", True)

# Shapes: C = 2 bands, H × W = 600 × 600 pixels of 20 m
scene: GeoArray = GeoArray(
    np.zeros((2, 600, 600), dtype=np.float32),  # (C, H, W) float32
    (20.0, 0.0, 600000.0, 0.0, -20.0, 3550000.0),  # (a, b, c, d, e, f)
    "EPSG:32613",
    fill_value=np.nan,
    dims=("band", "y", "x"),
).to_jax()

sub: GeoArray = scene[..., 100:356, 200:456]  # (C, 256, 256): the origin moves
window: PixelWindow = scene.grid.window((604000.0, 3542880.0, 609120.0, 3548000.0))
same: GeoArray = read_window(scene, window)  # the same pixels as sub

offsets = jnp.array([[0, 0], [256, 256], [344, 344]])  # (N, 2) int
patches: GeoArray = jax.jit(jax.vmap(lambda o: crop(scene, o, (256, 256))))(offsets)
patches.batch_shape  # (3,): (N, C, 256, 256) data, one (6,) transform per patch
valid = scene.valid_mask()  # (H, W) bool: every band finite and not the fill

Backends and georeader

NumPy and JAX carriers mix eagerly, as NumPy and jnp arrays do. Inside jit, grad or vmap the data are tracers, and NumPy code on them raises GeoArrayTraceError instead of JAX’s generic error.

CallNumPy carrierJAX carrier
np.asarray(g)the data, no copya host copy; raises under a trace; pipekit_ops.guard reports it
combine, stack, concatenate, write_window with mixed inputs—NumPy inputs move to JAX (needs x64)
from_georeader(gt) / to_georeader(g)GeoTensor ↔ GeoArray, sharing memoryto_georeader needs g.to_numpy() first
import numpy as np
from affine import Affine
from georeader.geotensor import GeoTensor

from geoarray import GeoArray
from geoarray.interop.georeader import from_georeader, to_georeader

gt: GeoTensor = GeoTensor(
    np.zeros((2, 600, 600), dtype=np.float32),  # (C, H, W) float32
    Affine(20.0, 0.0, 600000.0, 0.0, -20.0, 3550000.0),
    "EPSG:32613",
    fill_value_default=np.nan,
)
g: GeoArray = from_georeader(gt)  # (C, H, W) float32, the same memory
back: GeoTensor = to_georeader(g[..., :256, :256])  # (C, 256, 256), origin kept

What’s inside

NameUse it to…Guide
GeoArraycarry data with its stored dims, coordinates, transform, CRS, fill value and attrs; sel / isel by name, grid, with_data, to_jax, to_numpy, from_coordsConcepts
GeoSwath, GeoPoints, pad_points, to_geoswathcarry swaths in sensor geometry (lat/lon leaves), point sets (xy, z, time, valid) and model fields on a lat-lon gridTutorial
geoarray.coordssay what a dim means: SpectralAxis, VariableAxis, VerticalAxis, TimeAxis, PixelGrid, …Reference
geoarray.typesname common shapes (GeoImage, GeoMapSeries, GeoTracks, …) and check them with check_dimsReference
isel, sel, reduce, map_over, concat, combine, …pipekit-array’s dim-level functions, on every named carrier; a pipekit-array KernelOp runs on carriers tooConcepts
to_pixelmap coordinates to fractional pixels (functax’s kernel, re-exported); H(x) itself is geoops.geom.interp_atWhen to jit
to_xarray, from_xarray, to_dataset, to_datatreecross to xarray losslessly ([xarray])Cross to xarray
Griddescribe a pixel grid without data: bounds, res, shift, crop, from_bounds, isclose, get_configReference
PixelWindowname a rectangle of pixels, rasterio’s Window without rasterio; Grid.window / Grid.bounds_of convert to and from map boundsReference
g[...], iselslice with the transform following; select bands with their per-band attrsReference
map, shift_origin, squeeze, expand_dimschange the data on the same grid, move the georeferencing, add or drop leading axesReference
crop, read_window, write_window, padcut, read past the edges, write and pad pixels; crop inside jit / vmapReference
valid_mask, pixel_coords, footprintfind the pixels that hold data, their map coordinates, the outline ([vector])Reference
stack, concatenatejoin carriers that share a grid (stack(gs, "time"); concatenate is concat by position)Reference
FrozenAttrskeep metadata hashable, so it can key a compiled functionReference
RasterSource, AsyncRasterSourcedescribe a lazy reader: its grid, and reads that return a GeoArray (read_window, load); a GeoArray is oneReference
from_georeader, to_georeaderbridge to georeader’s GeoTensor, zero-copy, and (source_from_georeader / source_to_georeader) its lazy readers ([georeader])Reference
as_transform, as_crscoerce an affine.Affine, six numbers, an EPSG code or WKTReference

Next steps