Skip to content

feat: Add RasterLayer.from_xarray for a single DataArray - #1358

Open
kylebarron wants to merge 11 commits into
kyle/raster-affine-levelsfrom
kyle/raster-from-xarray
Open

kylebarron wants to merge 11 commits into
kyle/raster-affine-levelsfrom
kyle/raster-from-xarray

Conversation

@kylebarron

Copy link
Copy Markdown
Member

Note

This PR was written by Claude (Claude Code), not by @kylebarron.

Second of the stack toward xarray support. Stacked on #1357.

What I am changing

RasterLayer.from_xarray(da, render_tile=...) shows a 2-D (y, x) or 3-D (band, y, x) xarray.DataArray on a regular grid as one affine level. Each tile is read from the array in Python as the browser asks for it, so the array may be in memory, dask-backed, or lazily opened from Zarr.

  • The grid comes from the 1-D cell-center coordinates (with a regularity check keyed to the coordinate dtype's precision), or transform=.
  • The CRS comes from crs=, else a list of finders tried in order: GeoZarr proj:code / proj:wkt2 / proj:projjson attrs, then a longitude/latitude name or degrees_east/degrees_north heuristic giving EPSG:4326. Adding a metadata model later is adding one function to that list. CF grid_mapping is a follow-up PR.
  • 0 to 360 longitudes are rolled to -180 to 180 lazily when there is a coordinate and no explicit transform; a region that straddles 180 is left alone.
  • Tiles follow the data's dask or Zarr chunks (a whole number of chunks, at least 256 px; 512 px when there are no chunks or a whole-chunk tile would exceed 2048 px), or tile_size=.
  • A default min_zoom stops zoomed-out views from requesting every tile of a single-level array; min_zoom= overrides.
  • Loading: load_async for arrays without dask (native async on Zarr), with a permanent fallback to a thread the first time a backend raises NotImplementedError; a thread for dask arrays. Nothing blocks the event loop.
  • New extra lonboard[xarray] (xarray, affine); xarray, zarr and dask added to the dev group so CI runs the tests. The lock was regenerated with uv 0.13.8 and only adds packages.
  • The xarray docs inventory is added to mkdocs.yml so the API docs resolve xarray.DataArray.

How I did it

Everything lives in lonboard/layer/_raster_xarray.py as small functions over a levels: list[xr.DataArray], so the next PR adds multiscale levels without changing tile fetching. from_xarray on RasterLayer is a thin static method. affine and xarray are imported lazily; no new core dependency.

How you can test it

  • uv run --frozen pytest tests/test_raster_xarray.py tests/test_raster.py (48 + 9 tests: dims, transforms north-up and south-up, irregular and float32 coordinates, each CRS finder and the no-hit error, the roll and its two guards, tile sizes from dask, Zarr and no chunks, min_zoom default and override, fetch_tile windows at interior and edge tiles, band dim kept, the loader's three paths).
  • PORT=18765 uv run --frozen pytest tests/ui/test_raster.py (browser: an in-memory lon/lat array renders).
  • uv run --frozen --group docs mkdocs build --strict.

🤖 Written by Claude Code

🤖 Generated with Claude Code

@kylebarron
kylebarron added this pull request to stack #1361 November 13, 2026 18:55
@github-actions github-actions Bot added the feat label Nov 13, 2026
@kylebarron
kylebarron force-pushed the kyle/raster-from-xarray branch from ca754c7 to c119908 Compare November 16, 2026 20:42
kylebarron and others added 11 commits November 16, 2026 16:03
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Also accept float32 coordinates at their own precision, leave a region
that straddles 180 unrolled, and reject (lon, lat) dim order with a hint.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@kylebarron
kylebarron force-pushed the kyle/raster-from-xarray branch from c119908 to 3bdb2ca Compare November 16, 2026 21:04

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant