2121
2222from rasterix .odc_compat import BoundingBox , bbox_intersection , bbox_union , maybe_int , snap_grid
2323from rasterix .rioxarray_compat import guess_dims
24+ from rasterix .utils import get_affine
2425
2526T_Xarray = TypeVar ("T_Xarray" , "DataArray" , "Dataset" )
2627
3536def 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 CF "grid mapping" variable (commonly ``"spatial_ref"``). If not present, then the affine is determined 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
41- The object to assign the index to. Must have a rio accessor with a transform.
46+ The object to assign the index to.
4247 x_dim : str, optional
4348 Name of the x dimension. If None, will be automatically detected.
4449 y_dim : str, optional
@@ -49,22 +54,38 @@ def assign_index(obj: T_Xarray, *, x_dim: str | None = None, y_dim: str | None =
4954 xarray.DataArray or xarray.Dataset
5055 The input object with RasterIndex coordinates assigned.
5156
57+ Notes
58+ -----
59+ The "grid mapping" variable is determined following the CF conventions:
60+
61+ - If a DataArray is provided, we look for an attribute named ``"grid_mapping"``.
62+ - For a Dataset, we pull the first detected ``"grid_mapping"`` attribute when iterating over data variables.
63+
64+ The value of this attribute is a variable name containing projection information (commonly ``"spatial_ref"``).
65+ We then look for a ``"GeoTransform"`` attribute on this variable (following GDAL
66+ convention).
67+
68+ References
69+ ----------
70+ - `CF conventions document <http://cfconventions.org/Data/cf-conventions/cf-conventions-1.11/cf-conventions.html#grid-mappings-and-projections>`_.
71+ - `GDAL docs on GeoTransform <https://gdal.org/en/stable/tutorials/geotransforms_tut.html>`_.
72+
5273 Examples
5374 --------
5475 >>> import xarray as xr
55- >>> import rioxarray # Required for rio accessor
76+ >>> import rioxarray # Required for reading TIFF
5677 >>> da = xr.open_dataset("path/to/raster.tif", engine="rasterio")
5778 >>> indexed_da = assign_index(da)
5879 """
59- import rioxarray # noqa
60-
6180 if x_dim is None or y_dim is None :
6281 guessed_x , guessed_y = guess_dims (obj )
6382 x_dim = x_dim or guessed_x
6483 y_dim = y_dim or guessed_y
6584
85+ affine = get_affine (obj , x_dim = x_dim , y_dim = y_dim )
86+
6687 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
88+ affine , width = obj .sizes [x_dim ], height = obj .sizes [y_dim ], x_dim = x_dim , y_dim = y_dim , crs = obj . proj . crs
6889 )
6990 coords = Coordinates .from_xindex (index )
7091 return obj .assign_coords (coords )
0 commit comments