Skip to content

Commit

Permalink
Add remove_crs helper method and use it in tests
Browse files Browse the repository at this point in the history
It's not longer just `crs` key in attrs
  • Loading branch information
Kirill888 committed Apr 1, 2020
1 parent b4e4604 commit 597dc72
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
15 changes: 15 additions & 0 deletions datacube/testutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import uuid
import numpy as np
import xarray as xr
from datetime import datetime
from collections.abc import Sequence, Mapping
import pathlib
Expand Down Expand Up @@ -444,3 +445,17 @@ def mk_sample_xr_dataset(crs="EPSG:3578",
geobox = GeoBox(w, h, transform, crs)

return Datacube.create_storage(t_coords, geobox, [Measurement(name=name, dtype=dtype, nodata=nodata, units=units)])


def remove_crs(xx):
xx = xx.reset_coords(['spatial_ref'], drop=True)

xx.attrs.pop('crs', None)
for x in xx.coords.values():
x.attrs.pop('crs', None)

if isinstance(xx, xr.Dataset):
for x in xx.data_vars.values():
x.attrs.pop('crs', None)

return xx
5 changes: 2 additions & 3 deletions tests/test_utils_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from datacube.testutils import (
mk_test_image,
gen_tiff_dataset,
remove_crs,
)
from datacube.testutils.io import native_load, rio_slurp_xarray, rio_slurp
from datacube.utils.cog import write_cog, to_cog, _write_cog
Expand Down Expand Up @@ -159,9 +160,7 @@ def test_cog_no_crs(tmpdir, with_dask):
pp = Path(str(tmpdir))

xx, ds = gen_test_data(pp, dask=with_dask)
del xx.attrs['crs']
for dim in xx.dims:
del xx[dim].attrs['crs']
xx = remove_crs(xx)

with pytest.raises(ValueError):
write_cog(xx, ":mem:")
Expand Down
7 changes: 2 additions & 5 deletions tests/test_utils_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
pick_uri, uri_resolve,
normalise_path, default_base_dir)
from datacube.utils.io import check_write_path
from datacube.testutils import mk_sample_product
from datacube.testutils import mk_sample_product, remove_crs


def test_stats_dates():
Expand Down Expand Up @@ -214,10 +214,7 @@ def test_write_geotiff_str_crs(tmpdir, odc_style_xr_dataset):

assert (written_data == odc_style_xr_dataset['B10']).all()

del odc_style_xr_dataset.attrs['crs']
del odc_style_xr_dataset.B10.attrs['crs']
for dim in odc_style_xr_dataset.B10.dims:
del odc_style_xr_dataset[dim].attrs['crs']
odc_style_xr_dataset = remove_crs(odc_style_xr_dataset)
with pytest.raises(ValueError):
with pytest.warns(DeprecationWarning):
write_geotiff(filename, odc_style_xr_dataset)
Expand Down
8 changes: 3 additions & 5 deletions tests/test_xarray_extension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from datacube.testutils.geom import epsg4326, epsg3857
from datacube.testutils import mk_sample_xr_dataset
from datacube.testutils import mk_sample_xr_dataset, remove_crs
from datacube.utils.xarray_geoextensions import (
_norm_crs,
_xarray_affine,
Expand All @@ -23,10 +23,8 @@ def test_xr_extension(odc_style_xr_dataset):
(sx, zz0, tx, zz1, sy, ty) = xx.affine[:6]
assert (zz0, zz1) == (0, 0)

xx.attrs['crs'] = None
xx.B10.attrs['crs'] = None
for dim in xx.B10.dims:
xx[dim].attrs['crs'] = None
xx = remove_crs(xx)

assert _xarray_geobox(xx) is None
assert _xarray_extent(xx) is None

Expand Down

0 comments on commit 597dc72

Please sign in to comment.