Skip to content

Commit f4cd46a

Browse files
committed
Delete GeoTransform in assign_index
Closes #28
1 parent 413e95c commit f4cd46a

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
@@ -46,3 +46,14 @@ The downside here is that CRS information is duplicated in two places explicitly
4646
### Don't like it?
4747

4848
We chose this approach to enable experimentation. It is entirely possible to experiment with other approaches. Please reach out if you have opinions on this topic.
49+
50+
## Handling the `GeoTransform` attribute
51+
52+
GDAL _chooses_ to track the affine transform using a `GeoTransform` attribute on a `spatial_ref` variable. The `"spatial_ref"` is a
53+
"grid mapping" variable (as termed by the CF-conventions). It also records CRS information. Currently, our design is that
54+
{py:class}`xproj.CRSIndex` controls the CRS information and handles the creation of the `"spatial_ref"` variable, or more generally,
55+
the grid mapping variable. Thus, {py:class}`RasterIndex` _cannot_ keep the `"GeoTransform"` attribute on `"spatial_ref"` up-to-date
56+
because it does not control it.
57+
58+
Thus, {py:func}`assign_index` will delete the `"GeoTransform"` attribute on the grid mapping variable if it is detected, after using it
59+
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)