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

Add very minimal xarray plugin for engine="rasterio" #281

Merged
merged 28 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1834d61
Add minimal xarray plugin for `engine="gdal"`
alexamici Apr 5, 2021
eca38dc
Dask and distributed switched from `master` to `main`
alexamici Apr 5, 2021
1d34780
Add a test intended to work with xarray `master`
alexamici Apr 5, 2021
1370432
Code style
alexamici Apr 5, 2021
d5e5601
Test xarray plugin integration for newer versions
alexamici Apr 5, 2021
12eb9e1
Move `spatial_ref` out of coords to make it CF-compliant
alexamici Apr 5, 2021
848cbd4
Code style (flake8)
alexamici Apr 5, 2021
6928b5e
Test with and without guessing the extension
alexamici Apr 5, 2021
13362fc
Add CAN_OPEN_EXT set of extensions and keep bands as dimension
alexamici Apr 5, 2021
d39034f
Enable all `open_rasterio` in `open_dataset` as `backend_kwargs` and …
alexamici Apr 5, 2021
12f88a2
Quick & dirty implementation of `decode_coords="all"`
alexamici Apr 6, 2021
0b2cc76
Code style
alexamici Apr 6, 2021
da1f0a1
Fix typo
alexamici Apr 6, 2021
07cc1be
Minimal xarray plugin to enable `xr.open_dataset("cog.tif", engine="r…
alexamici Apr 6, 2021
581997e
Code style
alexamici Apr 6, 2021
073db2f
Added entry in the history
alexamici Apr 6, 2021
1e139a5
Implement PR feedback by @snowman2
alexamici Apr 6, 2021
274a112
Merge branch 'master' into xarray-plugin
alexamici Apr 6, 2021
36961c7
rebase and integrate with `grid_mapping` in `encoding`
alexamici Apr 6, 2021
730d19b
Try add minimal user documentation
alexamici Apr 6, 2021
951c4d9
version added
alexamici Apr 6, 2021
511d99e
Code style
alexamici Apr 6, 2021
25ab9bd
Add open_kwargs to backend
alexamici Apr 7, 2021
cf3c3f9
Apply suggestions from code review
alexamici Apr 7, 2021
b66db56
Merge remote-tracking branch 'origin/xarray-plugin' into xarray-plugin
alexamici Apr 7, 2021
37adc6e
Add a comment on xarray miversion
alexamici Apr 7, 2021
a1c8dfc
Implement feedback
alexamici Apr 7, 2021
18d7bd8
Declare minimal xarray version.
alexamici Apr 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Latest
- DEP: Python 3.6+ (issue #215)
- DEP: xarray 0.17+ (needed for issue #282)
- REF: Store `grid_mapping` in `encoding` instead of `attrs` (issue #282)
- ENH: enable `engine="rasterio"` via xarray backend API (issue #197 pull #281)

0.3.2
-----
Expand Down
55 changes: 55 additions & 0 deletions rioxarray/xarray_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os.path

import xarray as xr

from . import _io

CAN_OPEN_EXTS = {
"asc",
"geotif",
"geotiff",
"img",
"j2k",
"jp2",
"jpg",
"jpeg",
"png",
"tif",
"tiff",
"vrt",
}


class RasterioBackend(xr.backends.common.BackendEntrypoint):
alexamici marked this conversation as resolved.
Show resolved Hide resolved
def open_dataset(
self,
filename_or_obj,
drop_variables=None,
mask_and_scale=True,
parse_coordinates=None,
lock=None,
masked=False,
variable=None,
group=None,
default_name="band_data",
snowman2 marked this conversation as resolved.
Show resolved Hide resolved
snowman2 marked this conversation as resolved.
Show resolved Hide resolved
):
ds = _io.open_rasterio(
filename_or_obj,
mask_and_scale=mask_and_scale,
parse_coordinates=parse_coordinates,
lock=lock,
masked=masked,
variable=variable,
group=group,
default_name=default_name,
)
if isinstance(ds, xr.DataArray):
ds = ds.to_dataset()
return ds

def guess_can_open(self, filename_or_obj):
try:
_, ext = os.path.splitext(filename_or_obj)
except TypeError:
return False
return ext[1:].lower() in CAN_OPEN_EXTS
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ def get_version():
version=get_version(),
zip_safe=False,
python_requires=">=3.7",
entry_points={
"xarray.backends": ["rasterio=rioxarray.xarray_plugin:RasterioBackend"]
},
)
25 changes: 25 additions & 0 deletions test/integration/test_integration_xarray_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os.path

import pytest

from test.conftest import TEST_INPUT_DATA_DIR

xr = pytest.importorskip("xarray", minversion="0.17.1.dev0")
snowman2 marked this conversation as resolved.
Show resolved Hide resolved


def test_xarray_open_dataset():
snowman2 marked this conversation as resolved.
Show resolved Hide resolved
cog_file = os.path.join(TEST_INPUT_DATA_DIR, "cog.tif")

ds = xr.open_dataset(cog_file, engine="rasterio")

assert isinstance(ds, xr.Dataset)
assert "band_data" in ds.data_vars
assert ds.data_vars["band_data"].shape == (1, 500, 500)
assert "spatial_ref" not in ds.data_vars
assert "spatial_ref" in ds.coords
assert "grid_mapping" not in ds.data_vars["band_data"].attrs
assert "grid_mapping" in ds.data_vars["band_data"].encoding

ds = xr.open_dataset(cog_file)

assert isinstance(ds, xr.Dataset)