Skip to content

Commit

Permalink
Swap xarray.open_rasterio for rioxarray.open_rasterio #62
Browse files Browse the repository at this point in the history
xarray just dropped the deprecated open_rasterio method, so
this swaps in rioxarray.open_rasterio when calling to_xarray.
This is a potentially breaking change because the two methods
stored slightly different metadata in the resulting dataset
(e.g. ds.crs must now be accessed through ds.rio.crs).

In the process of getting pre-commit checks to pass, we also
had to update all the outdated, broken hooks and make a few
changes to get mypy passing.
  • Loading branch information
aazuspan committed Apr 23, 2023
1 parent 2efa4e4 commit ff35fb7
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
repos:
- repo: https://github.com/psf/black
rev: 21.7b0
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
rev: v1.2.0
hooks:
- id: mypy
args: [--disallow-untyped-defs, --no-implicit-optional, --warn-unused-ignores, --warn-redundant-casts, --warn-unreachable]
exclude: test
additional_dependencies: [types-requests, xarray]

- repo: https://github.com/pycqa/isort
rev: 5.8.0
rev: 5.12.0
hooks:
- id: isort
args: [--profile, black]
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
requirements = [
"rasterio",
"xarray",
"rioxarray",
"earthengine-api",
"tqdm",
"requests",
Expand Down
2 changes: 1 addition & 1 deletion test/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_to_xarray():
collection = ee.ImageCollection(test_list)
ds = collection.wx.to_xarray(region=region, scale=20, crs=crs, progress=False)

assert crs in ds.crs
assert crs == ds.rio.crs
assert all(
ds.time.values
== [
Expand Down
2 changes: 1 addition & 1 deletion wxee/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ee # type: ignore
import rasterio # type: ignore
import xarray as xr
from urllib3.exceptions import ProtocolError # type: ignore
from urllib3.exceptions import ProtocolError

from wxee import constants
from wxee.accessors import wx_accessor
Expand Down
5 changes: 3 additions & 2 deletions wxee/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import joblib # type: ignore
import rasterio # type: ignore
import requests
import rioxarray # type: ignore
import xarray as xr
from requests.adapters import HTTPAdapter
from tqdm.auto import tqdm # type: ignore
from urllib3.util.retry import Retry # type: ignore
from urllib3.util.retry import Retry


def Initialize(**kwargs: Any) -> None:
Expand Down Expand Up @@ -157,7 +158,7 @@ def _dataarray_from_file(file: str, masked: bool, nodata: int) -> xr.DataArray:
The file name must follow the format "{dimension}.{coordinate}.{variable}.{extension}".
"""
da = xr.open_rasterio(file)
da = rioxarray.open_rasterio(file)
dim, coord, var = _parse_filename(file)

da = da.expand_dims({dim: [coord]}).rename(var).squeeze("band").drop_vars("band")
Expand Down
6 changes: 3 additions & 3 deletions wxee/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def rgb(
>>> ds.wx.rgb(bands=["B8", "B4", "B3"], stretch=0.85, interactive=True, aspect=1.2)
"""
if bands:
if bands is not None:
if len(bands) != 3:
raise ValueError(f"Bands must be a list with exactly 3 names.")
else:
bands = list(self._obj.var())[:3]
bands = list(self._obj.var())[:3] # type: ignore

# Raise a different error if the bands were identified implicitly to avoid confusion
if len(bands) != 3:
if len(bands) != 3: # type: ignore
raise ValueError(
f"The Dataset must contain at least 3 data variables for RGB plotting."
)
Expand Down

0 comments on commit ff35fb7

Please sign in to comment.