Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Remove xarray crs attribute in rio.write_crs (issue #488) #490

Merged
merged 13 commits into from
Mar 30, 2022
4 changes: 4 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ History
Latest
-------

0.10.3
------
- BUG: Remove xarray crs attribute in rio.write_crs (issue #488)

0.10.2
-------
- BUG: Lazy load colormap through _manager.acquire() in merge (issue #479)
Expand Down
5 changes: 3 additions & 2 deletions rioxarray/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,9 @@ def open_rasterio(
# always (0, 0, 1) per definition (see
# https://github.com/sgillies/affine)
result.rio.write_transform(_rio_transform(riods), inplace=True)
if riods.crs:
result.rio.write_crs(riods.crs, inplace=True)
rio_crs = riods.crs or result.rio.crs
if rio_crs:
result.rio.write_crs(rio_crs, inplace=True)
if has_gcps:
result.rio.write_gcps(*riods.gcps, inplace=True)

Expand Down
3 changes: 3 additions & 0 deletions rioxarray/rioxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ def write_crs(self, input_crs=None, grid_mapping_name=None, inplace=False):
)
data_obj.coords[grid_mapping_name].rio.set_attrs(grid_map_attrs, inplace=True)

# remove old crs if exists
data_obj.attrs.pop("crs", None)

return data_obj.rio.write_grid_mapping(
grid_mapping_name=grid_mapping_name, inplace=True
)
Expand Down
3 changes: 0 additions & 3 deletions test/integration/test_integration_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def test_merge_arrays(squeeze):
with open_rasterio(dem_test) as rds:
rds.attrs = {
"_FillValue": rds.rio.nodata,
"crs": rds.attrs["crs"],
}
arrays = [
rds.isel(x=slice(100), y=slice(100)),
Expand Down Expand Up @@ -140,7 +139,6 @@ def test_merge_arrays__res():
with open_rasterio(dem_test, masked=True) as rds:
rds.attrs = {
"_FillValue": rds.rio.nodata,
"crs": rds.attrs["crs"],
}
arrays = [
rds.isel(x=slice(100), y=slice(100)),
Expand Down Expand Up @@ -173,7 +171,6 @@ def test_merge_arrays__res():
assert merged.rio.crs == rds.rio.crs
assert_almost_equal(merged.attrs.pop("_FillValue"), rds.attrs.pop("_FillValue"))
compare_attrs = dict(rds.attrs)
compare_attrs.pop("crs")
assert merged.attrs == compare_attrs
assert merged.encoding["grid_mapping"] == "spatial_ref"
assert_almost_equal(nansum(merged), 13760565)
Expand Down
19 changes: 15 additions & 4 deletions test/integration/test_integration_rioxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,18 +892,22 @@ def test_reproject__grid_mapping(modis_reproject):
) as mda, modis_reproject["open"](modis_reproject["compare"], **mask_args) as mdc:

# remove 'crs' attribute and add grid mapping
# keep a copy of the crs before setting the spatial_ref to 0 as that will
# set `rio.crs` to None.
mda_rio_crs = mda.rio.crs
mda.coords["spatial_ref"] = 0
mda.coords["spatial_ref"].attrs["spatial_ref"] = CRS.from_user_input(
_get_attr(mda, "crs")
mda_rio_crs
).wkt
_mod_attr(mda, "grid_mapping", val="spatial_ref")
_del_attr(mda, "crs")
# keep a copy of the crs before setting the spatial_ref to 0 as that will
# set `rio.crs` to None.
mdc_rio_crs = mdc.rio.crs
mdc.coords["spatial_ref"] = 0
mdc.coords["spatial_ref"].attrs["spatial_ref"] = CRS.from_user_input(
_get_attr(mdc, "crs")
mdc_rio_crs
).wkt
_mod_attr(mdc, "grid_mapping", val="spatial_ref")
_del_attr(mdc, "crs")

# reproject
mds_repr = mda.rio.reproject(modis_reproject["to_proj"])
Expand Down Expand Up @@ -2025,6 +2029,13 @@ def test_get_crs_dataset():
assert test_ds.rio.crs.to_epsg() == 4326


def test_crs_is_removed():
test_ds = xarray.Dataset(attrs=dict(crs="+init=epsg:4326"))
test_ds = test_ds.rio.write_crs(4326)

assert "crs" not in test_ds.attrs


def test_write_crs_cf():
test_da = xarray.DataArray(1)
test_da = test_da.rio.write_crs(4326)
Expand Down