File tree Expand file tree Collapse file tree 5 files changed +99
-0
lines changed
Expand file tree Collapse file tree 5 files changed +99
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,12 @@ Why use :func:`rioxarray.open_rasterio` instead of `xarray.open_rasterio`?
26265. It adds the coordinate axis CF metadata.
27276. It loads raster metadata into the attributes.
2828
29+ rioxarray 0.4+ enables passing `engine="rasterio" ` to ``xarray.open_dataset `` and ``xarray.open_mfdataset `` for xarray 0.18+:
30+
31+ .. code-block :: python
32+
33+ ds = xr.open_dataset(" my.tif" , engine = " rasterio" )
34+
2935
3036 .. toctree ::
3137 :maxdepth: 1
Original file line number Diff line number Diff line change 66- DEP: Python 3.6+ (issue #215)
77- DEP: xarray 0.17+ (needed for issue #282)
88- REF: Store `grid_mapping ` in `encoding ` instead of `attrs ` (issue #282)
9+ - ENH: enable `engine="rasterio" ` via xarray backend API (issue #197 pull #281)
910
10110.3.2
1112-----
Original file line number Diff line number Diff line change 1+ import os .path
2+
3+ import xarray as xr
4+
5+ from . import _io
6+
7+ CAN_OPEN_EXTS = {
8+ "asc" ,
9+ "geotif" ,
10+ "geotiff" ,
11+ "img" ,
12+ "j2k" ,
13+ "jp2" ,
14+ "jpg" ,
15+ "jpeg" ,
16+ "png" ,
17+ "tif" ,
18+ "tiff" ,
19+ "vrt" ,
20+ }
21+
22+
23+ class RasterioBackend (xr .backends .common .BackendEntrypoint ):
24+ """
25+ .. versionadded:: 0.4
26+ """
27+
28+ def open_dataset (
29+ self ,
30+ filename_or_obj ,
31+ drop_variables = None ,
32+ mask_and_scale = True ,
33+ parse_coordinates = None ,
34+ lock = None ,
35+ masked = False ,
36+ variable = None ,
37+ group = None ,
38+ default_name = "band_data" ,
39+ open_kwargs = None ,
40+ ):
41+ if open_kwargs is None :
42+ open_kwargs = {}
43+ ds = _io .open_rasterio (
44+ filename_or_obj ,
45+ mask_and_scale = mask_and_scale ,
46+ parse_coordinates = parse_coordinates ,
47+ lock = lock ,
48+ masked = masked ,
49+ variable = variable ,
50+ group = group ,
51+ default_name = default_name ,
52+ ** open_kwargs ,
53+ )
54+ if isinstance (ds , xr .DataArray ):
55+ ds = ds .to_dataset ()
56+ return ds
57+
58+ def guess_can_open (self , filename_or_obj ):
59+ try :
60+ _ , ext = os .path .splitext (filename_or_obj )
61+ except TypeError :
62+ return False
63+ return ext [1 :].lower () in CAN_OPEN_EXTS
Original file line number Diff line number Diff line change @@ -71,4 +71,7 @@ def get_version():
7171 version = get_version (),
7272 zip_safe = False ,
7373 python_requires = ">=3.7" ,
74+ entry_points = {
75+ "xarray.backends" : ["rasterio=rioxarray.xarray_plugin:RasterioBackend" ]
76+ },
7477)
Original file line number Diff line number Diff line change 1+ import os .path
2+
3+ import pytest
4+
5+ from test .conftest import TEST_INPUT_DATA_DIR
6+
7+ # FIXME: change to the next xarray version after release
8+ xr = pytest .importorskip ("xarray" , minversion = "0.17.1.dev0" )
9+
10+
11+ def test_xarray_open_dataset ():
12+ cog_file = os .path .join (TEST_INPUT_DATA_DIR , "cog.tif" )
13+
14+ ds = xr .open_dataset (cog_file , engine = "rasterio" )
15+
16+ assert isinstance (ds , xr .Dataset )
17+ assert "band_data" in ds .data_vars
18+ assert ds .data_vars ["band_data" ].shape == (1 , 500 , 500 )
19+ assert "spatial_ref" not in ds .data_vars
20+ assert "spatial_ref" in ds .coords
21+ assert "grid_mapping" not in ds .data_vars ["band_data" ].attrs
22+ assert "grid_mapping" in ds .data_vars ["band_data" ].encoding
23+
24+ ds = xr .open_dataset (cog_file )
25+
26+ assert isinstance (ds , xr .Dataset )
You can’t perform that action at this time.
0 commit comments