xarray-contrib/xarray-spatial
Spatial analysis algorithms for xarray implemented in numba
Fast spatial analysis library for xarray with 100+ algorithms implemented in numba
Raster data flows through spatial analysis pipelines, typically starting with elevation or satellite imagery, applying transformations via numba-accelerated kernels, and outputting processed xarray DataArrays.
Under the hood, the system uses 1 feedback loop, 3 data pools, 2 control points to manage its runtime behavior.
Structural Verdict
A 12-component library with 12 connections. 271 files analyzed. Well-connected — clear data flow between components.
How Data Flows Through the System
Raster data flows through spatial analysis pipelines, typically starting with elevation or satellite imagery, applying transformations via numba-accelerated kernels, and outputting processed xarray DataArrays.
- Data Input — Load raster data as xarray DataArrays from GeoTIFF or other formats
- Backend Selection — Detect and select optimal computation backend (NumPy/Dask/CuPy/RTX) based on data size and hardware
- Algorithm Execution — Apply spatial analysis functions using numba-compiled kernels for performance
- Result Generation — Return processed xarray DataArrays with preserved coordinate systems and metadata
System Behavior
How the system actually operates at runtime — where data accumulates, what loops, what waits, and what controls what.
Data Pools
DEM data used across hydrological analysis functions
D8 flow direction grids feeding downstream hydrological calculations
Performance metrics and timing data accumulated across test runs
Feedback Loops
- Backend Performance Adaptation (auto-scale, balancing) — Trigger: Function call with data size detection. Action: Select optimal backend based on array size and hardware availability. Exit: Backend selected for current operation.
Delays & Async Processing
- Numba JIT Compilation (async-processing, ~First call compilation time) — Initial function calls are slower due to compilation overhead
- Dask Graph Construction (batch-window, ~Variable based on chunk size) — Lazy evaluation delays computation until .compute() is called
Control Points
- Backend Selection Override (env-var) — Controls: Forces specific backend regardless of automatic detection
- Numba JIT Toggle (runtime-toggle) — Controls: Enables/disables JIT compilation for debugging. Default: True
Technology Stack
JIT compilation for performance acceleration
Multi-dimensional labeled array data structures
Core numerical computing backend
Parallel and distributed computing support
GPU acceleration for CUDA-enabled hardware
Scientific computing utilities and algorithms
Geometric operations and vector data handling
Geospatial data manipulation and analysis
Raster data I/O and coordinate reference systems
Unit testing framework
Key Components
- convolution (module) — Provides kernel-based convolution operations with custom kernel support
xrspatial/convolution.py - focal (module) — Implements focal statistics operations like mean, sum, and hotspot detection
xrspatial/focal.py - flow_direction (function) — Computes D8 flow direction from elevation data for hydrological modeling
xrspatial/flow_direction.py - flow_accumulation (function) — Calculates accumulated flow based on flow direction grids
xrspatial/flow_accumulation.py - polygonize (function) — Converts raster regions to vector polygons with multiple output format support
xrspatial/polygonize.py - multispectral (module) — Implements vegetation indices like NDVI, EVI, ARVI for satellite imagery analysis
xrspatial/multispectral.py - proximity (module) — Calculates proximity, allocation, and direction analysis with multiple distance metrics
xrspatial/proximity.py - rasterize (function) — Converts vector geometries to raster format with burn-in value support
xrspatial/rasterize.py - classify (module) — Provides data classification methods including natural breaks and quantiles
xrspatial/classify.py - utils (module) — Contains backend detection, array conversion, and common utility functions
xrspatial/utils.py - geotiff (module) — Handles GeoTIFF file I/O with coordinate system and metadata support
xrspatial/geotiff/__init__.py - hillshade (function) — Generates terrain shading visualization from elevation data
xrspatial/hillshade.py
Configuration
codecov.yml (yaml)
coverage.status.patch(string, unknown) — default: offcoverage.status.project.default.target(string, unknown) — default: autocoverage.status.project.default.threshold(string, unknown) — default: 0.5%comment.require_changes(string, unknown) — default: yesignore(array, unknown) — default: ./setup.py,xrspatial/tests/*,xrspatial/experimental/*,xrspatial/gpu_rtx/*,xrspatial/datasets/*,xrspatial/__init__.py,xrspatial/__main__.py
xrspatial/diagnostics.py (python-dataclass)
code(str, unknown)message(str, unknown)suggestion(str, unknown)
xrspatial/diagnostics.py (python-dataclass)
issues(List[DiagnosticIssue], unknown) — default: field(default_factory=list)horizontal_unit_type(Optional[str], unknown) — default: Nonevertical_unit_type(Optional[str], unknown) — default: Noneresolution(Optional[tuple], unknown) — default: None
xrspatial/geotiff/_geotags.py (python-dataclass)
transform(GeoTransform, unknown) — default: field(default_factory=GeoTransform)model_type(int, unknown) — default: 0raster_type(int, unknown) — default: RASTER_PIXEL_IS_AREA
Science Pipeline
- Load elevation data — xarray.open_dataarray or from_array conversion [(ny, nx) or chunked equivalent → (ny, nx)]
xrspatial/flow_direction.py - Compute flow direction — Apply D8 algorithm with numba kernel on elevation grid [(ny, nx) → (ny, nx)]
xrspatial/flow_direction.py - Calculate flow accumulation — Trace upstream contributions using flow direction matrix [(ny, nx) → (ny, nx)]
xrspatial/flow_accumulation.py - Generate derived products — Apply focal statistics, hillshading, or classification algorithms [(ny, nx) → (ny, nx) or vector geometries]
xrspatial/focal.py
Assumptions & Constraints
- [warning] Assumes 2D elevation grid but no explicit shape validation before applying D8 algorithm (shape)
- [info] Expects NIR and red band values to be in reflectance range [0,1] or DN values but no bounds checking (value-range)
- [warning] Assumes elevation data is numeric but may fail silently with non-numeric dtypes (dtype)
- [critical] CuPy backend assumes data fits in GPU memory without explicit memory checks (device)
- [warning] Assumes kernel has odd dimensions for proper centering but doesn't validate (format)
Explore the interactive analysis
See the full architecture map, data flow, and code patterns visualization.
Analyze on CodeSeaRelated Library Repositories
Frequently Asked Questions
What is xarray-spatial used for?
Fast spatial analysis library for xarray with 100+ algorithms implemented in numba xarray-contrib/xarray-spatial is a 12-component library written in Python. Well-connected — clear data flow between components. The codebase contains 271 files.
How is xarray-spatial architected?
xarray-spatial is organized into 4 architecture layers: Algorithm Modules, Backend Dispatch, Data I/O, Core Infrastructure. Well-connected — clear data flow between components. This layered structure enables tight integration between components.
How does data flow through xarray-spatial?
Data moves through 4 stages: Data Input → Backend Selection → Algorithm Execution → Result Generation. Raster data flows through spatial analysis pipelines, typically starting with elevation or satellite imagery, applying transformations via numba-accelerated kernels, and outputting processed xarray DataArrays. This pipeline design keeps the data transformation process straightforward.
What technologies does xarray-spatial use?
The core stack includes numba (JIT compilation for performance acceleration), xarray (Multi-dimensional labeled array data structures), numpy (Core numerical computing backend), dask (Parallel and distributed computing support), cupy (GPU acceleration for CUDA-enabled hardware), scipy (Scientific computing utilities and algorithms), and 4 more. This broad technology surface reflects a mature project with many integration points.
What system dynamics does xarray-spatial have?
xarray-spatial exhibits 3 data pools (Elevation Grids, Flow Direction Arrays), 1 feedback loop, 2 control points, 2 delays. The feedback loops handle auto-scale. These runtime behaviors shape how the system responds to load, failures, and configuration changes.
What design patterns does xarray-spatial use?
4 design patterns detected: Backend Dispatch Pattern, Numba JIT Compilation, XArray DataArray Interface, Modular Algorithm Organization.
Analyzed on March 31, 2026 by CodeSea. Written by Karolina Sarna.