Skip to content

Commit

Permalink
DEP: Deprecate rasterio backend
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed Sep 22, 2021
1 parent 7a65d59 commit 88bd1a4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
12 changes: 6 additions & 6 deletions doc/user-guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ GeoTIFFs and other gridded raster datasets can be opened using `rasterio`_, if
rasterio is installed. Here is an example of how to use
:py:func:`open_rasterio` to read one of rasterio's `test files`_:

.. deprecated:: 0.20

Deprecated in favor of rioxarray.
For information about transitioning, see:
https://corteva.github.io/rioxarray/stable/getting_started/getting_started.html

.. ipython::
:verbatim:

Expand Down Expand Up @@ -769,12 +775,6 @@ coordinates defined in the file's projection provided by the ``crs`` attribute.
See :ref:`/examples/visualization_gallery.ipynb#Parsing-rasterio-geocoordinates`
for an example of how to convert these to longitudes and latitudes.

.. warning::

This feature has been added in xarray v0.9.6 and should still be
considered experimental. Please report any bugs you may find
on xarray's github repository.


Additionally, you can use `rioxarray`_ for reading in GeoTiff, netCDF or other
GDAL readable raster data using `rasterio`_ as well as for exporting to a geoTIFF.
Expand Down
13 changes: 13 additions & 0 deletions xarray/backends/rasterio_.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ def open_rasterio(
):
"""Open a file with rasterio (experimental).
.. deprecated:: 0.20
Deprecated in favor of rioxarray.
For information about transitioning, see:
https://corteva.github.io/rioxarray/stable/getting_started/getting_started.html
This should work with any file that rasterio can open (most often:
geoTIFF). The x and y coordinates are generated automatically from the
file's geoinformation, shifted to the center of each pixel (see
Expand Down Expand Up @@ -252,6 +258,13 @@ def open_rasterio(
data : DataArray
The newly created DataArray.
"""
warnings.warn(
"open_rasterio is Deprecated in favor of rioxarray. "
"For information about transitioning, see: "
"https://corteva.github.io/rioxarray/stable/getting_started/getting_started.html",
DeprecationWarning,
stacklevel=2,
)
import rasterio
from rasterio.vrt import WarpedVRT

Expand Down
54 changes: 34 additions & 20 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4215,15 +4215,15 @@ class TestRasterio:
def test_serialization(self):
with create_tmp_geotiff(additional_attrs={}) as (tmp_file, expected):
# Write it to a netcdf and read again (roundtrip)
with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
with create_tmp_file(suffix=".nc") as tmp_nc_file:
rioda.to_netcdf(tmp_nc_file)
with xr.open_dataarray(tmp_nc_file) as ncds:
assert_identical(rioda, ncds)

def test_utm(self):
with create_tmp_geotiff() as (tmp_file, expected):
with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
assert_allclose(rioda, expected)
assert rioda.attrs["scales"] == (1.0, 1.0, 1.0)
assert rioda.attrs["offsets"] == (0.0, 0.0, 0.0)
Expand All @@ -4239,7 +4239,9 @@ def test_utm(self):
)

# Check no parse coords
with xr.open_rasterio(tmp_file, parse_coordinates=False) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(
tmp_file, parse_coordinates=False
) as rioda:
assert "x" not in rioda.coords
assert "y" not in rioda.coords

Expand All @@ -4251,7 +4253,7 @@ def test_non_rectilinear(self):
transform=from_origin(0, 3, 1, 1).rotation(45), crs=None
) as (tmp_file, _):
# Default is to not parse coords
with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
assert "x" not in rioda.coords
assert "y" not in rioda.coords
assert "crs" not in rioda.attrs
Expand All @@ -4266,7 +4268,9 @@ def test_non_rectilinear(self):

# See if a warning is raised if we force it
with pytest.warns(Warning, match="transformation isn't rectilinear"):
with xr.open_rasterio(tmp_file, parse_coordinates=True) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(
tmp_file, parse_coordinates=True
) as rioda:
assert "x" not in rioda.coords
assert "y" not in rioda.coords

Expand All @@ -4279,7 +4283,7 @@ def test_platecarree(self):
crs="+proj=latlong",
open_kwargs={"nodata": -9765},
) as (tmp_file, expected):
with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
assert_allclose(rioda, expected)
assert rioda.attrs["scales"] == (1.0,)
assert rioda.attrs["offsets"] == (0.0,)
Expand Down Expand Up @@ -4327,7 +4331,7 @@ def test_notransform(self):
"x": [0.5, 1.5, 2.5, 3.5],
},
)
with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
assert_allclose(rioda, expected)
assert rioda.attrs["scales"] == (1.0, 1.0, 1.0)
assert rioda.attrs["offsets"] == (0.0, 0.0, 0.0)
Expand All @@ -4342,7 +4346,9 @@ def test_indexing(self):
with create_tmp_geotiff(
8, 10, 3, transform_args=[1, 2, 0.5, 2.0], crs="+proj=latlong"
) as (tmp_file, expected):
with xr.open_rasterio(tmp_file, cache=False) as actual:
with pytest.warns(DeprecationWarning), xr.open_rasterio(
tmp_file, cache=False
) as actual:

# tests
# assert_allclose checks all data + coordinates
Expand Down Expand Up @@ -4458,7 +4464,7 @@ def test_caching(self):
8, 10, 3, transform_args=[1, 2, 0.5, 2.0], crs="+proj=latlong"
) as (tmp_file, expected):
# Cache is the default
with xr.open_rasterio(tmp_file) as actual:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as actual:

# This should cache everything
assert_allclose(actual, expected)
Expand All @@ -4474,7 +4480,9 @@ def test_chunks(self):
8, 10, 3, transform_args=[1, 2, 0.5, 2.0], crs="+proj=latlong"
) as (tmp_file, expected):
# Chunk at open time
with xr.open_rasterio(tmp_file, chunks=(1, 2, 2)) as actual:
with pytest.warns(DeprecationWarning), xr.open_rasterio(
tmp_file, chunks=(1, 2, 2)
) as actual:

import dask.array as da

Expand All @@ -4496,7 +4504,7 @@ def test_chunks(self):
def test_pickle_rasterio(self):
# regression test for https://github.com/pydata/xarray/issues/2121
with create_tmp_geotiff() as (tmp_file, expected):
with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
temp = pickle.dumps(rioda)
with pickle.loads(temp) as actual:
assert_equal(actual, rioda)
Expand Down Expand Up @@ -4548,7 +4556,7 @@ def test_ENVI_tags(self):
}
expected = DataArray(data, dims=("band", "y", "x"), coords=coords)

with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
assert_allclose(rioda, expected)
assert isinstance(rioda.attrs["crs"], str)
assert isinstance(rioda.attrs["res"], tuple)
Expand All @@ -4563,7 +4571,7 @@ def test_ENVI_tags(self):
def test_geotiff_tags(self):
# Create a geotiff file with some tags
with create_tmp_geotiff() as (tmp_file, _):
with xr.open_rasterio(tmp_file) as rioda:
with pytest.warns(DeprecationWarning), xr.open_rasterio(tmp_file) as rioda:
assert isinstance(rioda.attrs["AREA_OR_POINT"], str)

@requires_dask
Expand All @@ -4578,7 +4586,9 @@ def test_no_mftime(self):
8, 10, 3, transform_args=[1, 2, 0.5, 2.0], crs="+proj=latlong"
) as (tmp_file, expected):
with mock.patch("os.path.getmtime", side_effect=OSError):
with xr.open_rasterio(tmp_file, chunks=(1, 2, 2)) as actual:
with pytest.warns(DeprecationWarning), xr.open_rasterio(
tmp_file, chunks=(1, 2, 2)
) as actual:
import dask.array as da

assert isinstance(actual.data, da.Array)
Expand All @@ -4589,10 +4599,12 @@ def test_http_url(self):
# more examples urls here
# http://download.osgeo.org/geotiff/samples/
url = "http://download.osgeo.org/geotiff/samples/made_up/ntf_nord.tif"
with xr.open_rasterio(url) as actual:
with pytest.warns(DeprecationWarning), xr.open_rasterio(url) as actual:
assert actual.shape == (1, 512, 512)
# make sure chunking works
with xr.open_rasterio(url, chunks=(1, 256, 256)) as actual:
with pytest.warns(DeprecationWarning), xr.open_rasterio(
url, chunks=(1, 256, 256)
) as actual:
import dask.array as da

assert isinstance(actual.data, da.Array)
Expand All @@ -4604,7 +4616,9 @@ def test_rasterio_environment(self):
# Should fail with error since suffix not allowed
with pytest.raises(Exception):
with rasterio.Env(GDAL_SKIP="GTiff"):
with xr.open_rasterio(tmp_file) as actual:
with pytest.warns(DeprecationWarning), xr.open_rasterio(
tmp_file
) as actual:
assert_allclose(actual, expected)

@pytest.mark.xfail(reason="rasterio 1.1.1 is broken. GH3573")
Expand All @@ -4621,7 +4635,7 @@ def test_rasterio_vrt(self):
# Value of single pixel in center of image
lon, lat = vrt.xy(vrt.width // 2, vrt.height // 2)
expected_val = next(vrt.sample([(lon, lat)]))
with xr.open_rasterio(vrt) as da:
with pytest.warns(DeprecationWarning), xr.open_rasterio(vrt) as da:
actual_shape = (da.sizes["x"], da.sizes["y"])
actual_crs = da.crs
actual_res = da.res
Expand Down Expand Up @@ -4675,7 +4689,7 @@ def test_rasterio_vrt_with_src_crs(self):
with rasterio.open(tmp_file) as src:
assert src.crs is None
with rasterio.vrt.WarpedVRT(src, src_crs=src_crs) as vrt:
with xr.open_rasterio(vrt) as da:
with pytest.warns(DeprecationWarning), xr.open_rasterio(vrt) as da:
assert da.crs == src_crs

@network
Expand All @@ -4695,7 +4709,7 @@ def test_rasterio_vrt_network(self):
# Value of single pixel in center of image
lon, lat = vrt.xy(vrt.width // 2, vrt.height // 2)
expected_val = next(vrt.sample([(lon, lat)]))
with xr.open_rasterio(vrt) as da:
with pytest.warns(DeprecationWarning), xr.open_rasterio(vrt) as da:
actual_shape = da.sizes["x"], da.sizes["y"]
actual_res = da.res
actual_val = da.sel(dict(x=lon, y=lat), method="nearest").data
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_dask_distributed_zarr_integration_test(loop, consolidated, compute) ->
def test_dask_distributed_rasterio_integration_test(loop) -> None:
with create_tmp_geotiff() as (tmp_file, expected):
with cluster() as (s, [a, b]):
with Client(s["address"], loop=loop):
with pytest.warns(DeprecationWarning), Client(s["address"], loop=loop):
da_tiff = xr.open_rasterio(tmp_file, chunks={"band": 1})
assert isinstance(da_tiff.data, da.Array)
actual = da_tiff.compute()
Expand Down
14 changes: 7 additions & 7 deletions xarray/tests/test_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def test_download_rasterio_from_github_load_without_cache(
self, tmp_path, monkeypatch
):
cache_dir = tmp_path / tutorial._default_cache_dir_name

arr_nocache = tutorial.open_rasterio(
"RGB.byte", cache=False, cache_dir=cache_dir
).load()
arr_cache = tutorial.open_rasterio(
"RGB.byte", cache=True, cache_dir=cache_dir
).load()
with pytest.warns(DeprecationWarning):
arr_nocache = tutorial.open_rasterio(
"RGB.byte", cache=False, cache_dir=cache_dir
).load()
arr_cache = tutorial.open_rasterio(
"RGB.byte", cache=True, cache_dir=cache_dir
).load()
assert_identical(arr_cache, arr_nocache)

0 comments on commit 88bd1a4

Please sign in to comment.