Skip to content

Fixes #790: add .xrs xarray accessors#868

Merged
brendancol merged 3 commits intomasterfrom
fixes-790-xarray-accessors
Feb 24, 2026
Merged

Fixes #790: add .xrs xarray accessors#868
brendancol merged 3 commits intomasterfrom
fixes-790-xarray-accessors

Conversation

@brendancol
Copy link
Contributor

Summary

  • Adds .xrs DataArray and Dataset accessors so spatial operations can be called directly via tab-completion, aligning with the xarray ecosystem pattern (rioxarray .rio, etc.)
  • Registers accessors automatically on import xrspatial — no extra setup needed
  • Updates all user guide and example notebooks to use the accessor pattern, replacing per-function imports with the more convenient raster.xrs.slope() style

New files

  • xrspatial/accessor.pyXrsSpatialDataArrayAccessor and XrsSpatialDatasetAccessor classes with lazy imports in every method body to avoid loading numba/cupy/dask at registration time
  • xrspatial/tests/test_accessor.py — 21 tests covering registration, tab-completion, DataArray single/multi-input, and Dataset single/multi-input equivalence

API examples

import xrspatial

# Surface
raster.xrs.slope()
raster.xrs.hillshade(azimuth=315)

# Classification
raster.xrs.equal_interval(k=5)

# Multispectral — self is the NIR band
nir.xrs.ndvi(red)
nir.xrs.evi(red, blue)

# Dataset — single-input applies to each variable
ds.xrs.slope()

# Dataset — multispectral with band mapping
ds.xrs.ndvi(nir='band_8', red='band_4')

Notebooks updated

All 11 user guide and example notebooks converted to accessor style:
1_Surface, 2_Proximity, 3_Zonal, 4_Focal, 5_Classification, 6_Remote_Sensing, 7_Pathfinding, 9_Cost_Distance, zonal_crosstab, pharmacy-deserts, classification-methods

Test plan

  • pytest xrspatial/tests/test_accessor.py -v — 21/21 passed
  • pytest xrspatial/tests/ -v — 635 passed, 12 skipped, 0 failures
  • Smoke test covering accessor patterns used in notebooks
  • Verify notebooks run end-to-end (requires example data)

Closes #790

Register DataArray and Dataset accessors so spatial operations can be
called directly via tab-completion (e.g. raster.xrs.slope()). Update
all user guide and example notebooks to use the accessor pattern,
removing per-function imports in favor of a single `import xrspatial`.
@brendancol brendancol merged commit ebf3944 into master Feb 24, 2026
10 checks passed
@brendancol brendancol mentioned this pull request Feb 24, 2026
@brendancol brendancol deleted the fixes-790-xarray-accessors branch February 25, 2026 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coordinating on new Xarray accessor functionalities for rasters and DEMs

1 participant