Skip to content

Commit d42f267

Browse files
committed
Delete GeoTransform in assign_index
Closes #28
1 parent 180481b commit d42f267

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

docs/raster_index/design_choices.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,14 @@
66

77
In designing {py:class}`RasterIndex`, we faced a few thorny questions. Below we discuss these considerations, and the approach we've taken.
88
Ultimately, there are no easy answers and tradeoffs to be made.
9+
10+
## Handling the `GeoTransform` attribute
11+
12+
GDAL _chooses_ to track the affine transform using a `GeoTransform` attribute on a `spatial_ref` variable. The `"spatial_ref"` is a
13+
"grid mapping" variable (as termed by the CF-conventions). It also records CRS information. Currently, our design is that
14+
{py:class}`xproj.CRSIndex` controls the CRS information and handles the creation of the `"spatial_ref"` variable, or more generally,
15+
the grid mapping variable. Thus, {py:class}`RasterIndex` _cannot_ keep the `"GeoTransform"` attribute on `"spatial_ref"` up-to-date
16+
because it does not control it.
17+
18+
Thus, {py:func}`assign_index` will delete the `"GeoTransform"` attribute on the grid mapping variable if it is detected, after using it
19+
to construct the affine matrix.

src/rasterix/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ def get_grid_mapping_var(obj: xr.Dataset | xr.DataArray) -> xr.DataArray | None:
2424
return None
2525

2626

27-
def get_affine(obj: xr.Dataset | xr.DataArray, *, x_dim="x", y_dim="y") -> Affine:
27+
def get_affine(
28+
obj: xr.Dataset | xr.DataArray, *, x_dim="x", y_dim="y", clear_transform: bool = False
29+
) -> Affine:
2830
"""
2931
Grabs an affine transform from an Xarray object.
3032
@@ -39,13 +41,17 @@ def get_affine(obj: xr.Dataset | xr.DataArray, *, x_dim="x", y_dim="y") -> Affin
3941
Name of the X dimension coordinate variable.
4042
y_dim: str, optional
4143
Name of the Y dimension coordinate variable.
44+
clear_transform: bool
45+
Whether to delete the ``GeoTransform`` attribute if detected.
4246
4347
Returns
4448
-------
4549
affine: Affine
4650
"""
4751
grid_mapping_var = get_grid_mapping_var(obj)
4852
if grid_mapping_var is not None and (transform := grid_mapping_var.attrs.get("GeoTransform")):
53+
if clear_transform:
54+
del grid_mapping_var.attrs["GeoTransform"]
4955
return Affine.from_gdal(*map(float, transform.split(" ")))
5056
else:
5157
x = obj.coords[x_dim]

tests/test_raster_index.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def test_sel_slice():
8585
transform = Affine.identity()
8686
ds.coords["spatial_ref"] = ((), 0, {"GeoTransform": " ".join(map(str, transform.to_gdal()))})
8787
ds = assign_index(ds)
88+
assert "GeoTransform" not in ds.spatial_ref.attrs
8889
assert ds.xindexes["x"].transform() == transform
8990

9091
actual = ds.sel(x=slice(4), y=slice(3, 5))

0 commit comments

Comments
 (0)