Skip to content

Commit 9fd5556

Browse files
committed
Remove rioxarray dependency
1 parent 440da19 commit 9fd5556

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ docs = [
5454
"pooch",
5555
"dask-geopandas",
5656
"rasterio",
57-
"rioxarray",
5857
"exactextract",
5958
"sparse",
6059
"netCDF4",

src/rasterix/raster_index.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from rasterix.odc_compat import BoundingBox, bbox_intersection, bbox_union, maybe_int, snap_grid
2323
from rasterix.rioxarray_compat import guess_dims
24+
from rasterix.utils import get_affine
2425

2526
T_Xarray = TypeVar("T_Xarray", "DataArray", "Dataset")
2627

@@ -35,6 +36,10 @@
3536
def assign_index(obj: T_Xarray, *, x_dim: str | None = None, y_dim: str | None = None) -> T_Xarray:
3637
"""Assign a RasterIndex to an Xarray DataArray or Dataset.
3738
39+
By default, the affine transform is guessed by first looking for a ``GeoTransform`` attribute
40+
on a ``"spatial_ref"`` variable. If not present, then the affine is determine from 1D coordinate
41+
variables named ``x_dim`` and ``y_dim`` provided to this function.
42+
3843
Parameters
3944
----------
4045
obj : xarray.DataArray or xarray.Dataset
@@ -52,19 +57,19 @@ def assign_index(obj: T_Xarray, *, x_dim: str | None = None, y_dim: str | None =
5257
Examples
5358
--------
5459
>>> import xarray as xr
55-
>>> import rioxarray # Required for rio accessor
60+
>>> import rioxarray # Required for reading TIFF
5661
>>> da = xr.open_dataset("path/to/raster.tif", engine="rasterio")
5762
>>> indexed_da = assign_index(da)
5863
"""
59-
import rioxarray # noqa
60-
6164
if x_dim is None or y_dim is None:
6265
guessed_x, guessed_y = guess_dims(obj)
6366
x_dim = x_dim or guessed_x
6467
y_dim = y_dim or guessed_y
6568

69+
affine = get_affine(obj, x_dim=x_dim, y_dim=y_dim)
70+
6671
index = RasterIndex.from_transform(
67-
obj.rio.transform(), width=obj.sizes[x_dim], height=obj.sizes[y_dim], x_dim=x_dim, y_dim=y_dim
72+
affine, width=obj.sizes[x_dim], height=obj.sizes[y_dim], x_dim=x_dim, y_dim=y_dim, crs=obj.proj.crs
6873
)
6974
coords = Coordinates.from_xindex(index)
7075
return obj.assign_coords(coords)

src/rasterix/rasterize/rasterio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from rasterio.features import geometry_mask as geometry_mask_rio
1616
from rasterio.features import rasterize as rasterize_rio
1717

18-
from .utils import XAXIS, YAXIS, clip_to_bbox, get_affine, is_in_memory, prepare_for_dask
18+
from ..utils import get_affine
19+
from .utils import XAXIS, YAXIS, clip_to_bbox, is_in_memory, prepare_for_dask
1920

2021
F = TypeVar("F", bound=Callable[..., Any])
2122

@@ -161,7 +162,7 @@ def rasterize(
161162
obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim)
162163

163164
rasterize_kwargs = dict(
164-
all_touched=all_touched, merge_alg=merge_alg, affine=get_affine(obj, xdim=xdim, ydim=ydim), env=env
165+
all_touched=all_touched, merge_alg=merge_alg, affine=get_affine(obj, x_dim=xdim, y_dim=ydim), env=env
165166
)
166167
# FIXME: box.crs == geometries.crs
167168

@@ -325,7 +326,7 @@ def geometry_mask(
325326
obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim)
326327

327328
geometry_mask_kwargs = dict(
328-
all_touched=all_touched, affine=get_affine(obj, xdim=xdim, ydim=ydim), env=env
329+
all_touched=all_touched, affine=get_affine(obj, x_dim=xdim, y_dim=ydim), env=env
329330
)
330331

331332
if is_in_memory(obj=obj, geometries=geometries):

src/rasterix/utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import xarray as xr
2+
from affine import Affine
3+
4+
5+
def get_affine(obj: xr.Dataset | xr.DataArray, *, x_dim="x", y_dim="y") -> Affine:
6+
"""
7+
Grabs an affine transform from an Xarray object.
8+
9+
This method will first look for the ``"GeoTransform"`` attribute on a variable named
10+
``"spatial_ref"``. If not, it will auto-guess the transform from the provided ``x_dim``,
11+
and ``y_dim``.
12+
13+
Parameters
14+
----------
15+
obj: xr.DataArray or xr.Dataset
16+
x_dim: str, optional
17+
Name of the X dimension coordinate variable.
18+
y_dim: str, optional
19+
Name of the Y dimension coordinate variable.
20+
21+
Returns
22+
-------
23+
affine: Affine
24+
"""
25+
spatial_ref = obj.coords.get("spatial_ref")
26+
if spatial_ref is not None and "GeoTransform" in spatial_ref.attrs:
27+
return Affine.from_gdal(*map(float, spatial_ref.attrs["GeoTransform"].split(" ")))
28+
else:
29+
x = obj.coords[x_dim]
30+
y = obj.coords[y_dim]
31+
if x.ndim != 1:
32+
raise ValueError(f"Coordinate variable {x_dim=!r} must be 1D.")
33+
if y.ndim != 1:
34+
raise ValueError(f"Coordinate variable {y_dim=!r} must be 1D.")
35+
dx = (x[1] - x[0]).item()
36+
dy = (y[1] - y[0]).item()
37+
return Affine.translation(
38+
x[0].item() - dx / 2, (y[0] if dy < 0 else y[-1]).item() - dy / 2
39+
) * Affine.scale(dx, dy)

tests/test_exact.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import sparse
1010
import xarray as xr
1111
import xarray.testing as xrt
12+
import xproj # noqa
1213
from exactextract import exact_extract
1314
from hypothesis import example, given, settings
1415
from xarray.tests import raise_if_dask_computes
1516

1617
from rasterix.rasterize.exact import CoverageWeights, coverage, xy_to_raster_source
1718

1819
dataset = xr.tutorial.open_dataset("eraint_uvz").rename({"latitude": "y", "longitude": "x"})
19-
dataset = dataset.rio.write_crs("epsg:4326")
20+
dataset = dataset.proj.assign_crs(spatial_ref="epsg:4326")
2021
world = gpd.read_file(geodatasets.get_path("naturalearth land"))
2122
XSIZE = dataset.x.size
2223
YSIZE = dataset.y.size

tests/test_rasterize.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
import rioxarray # noqa
66
import xarray as xr
7+
import xproj # noqa
78
from xarray.tests import raise_if_dask_computes
89

910
from rasterix.rasterize.rasterio import geometry_mask, rasterize
@@ -20,7 +21,7 @@ def test_rasterize(clip):
2021
snapshot = snapshot.sel(latitude=slice(83.25, None))
2122

2223
ds = xr.tutorial.open_dataset("eraint_uvz")
23-
ds = ds.rio.write_crs("epsg:4326")
24+
ds = ds.proj.assign_crs(spatial_ref="epsg:4326")
2425
world = gpd.read_file(geodatasets.get_path("naturalearth land"))
2526

2627
kwargs = dict(xdim="longitude", ydim="latitude", clip=clip)
@@ -54,7 +55,7 @@ def test_geometry_mask(clip, invert):
5455
snapshot = ~snapshot
5556

5657
ds = xr.tutorial.open_dataset("eraint_uvz")
57-
ds = ds.rio.write_crs("epsg:4326")
58+
ds = ds.proj.assign_crs(spatial_ref="epsg:4326")
5859
world = gpd.read_file(geodatasets.get_path("naturalearth land"))
5960

6061
kwargs = dict(xdim="longitude", ydim="latitude", clip=clip, invert=invert)

0 commit comments

Comments
 (0)