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 support for rioxarray rasterio and update tests #905

Merged
merged 5 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
9 changes: 7 additions & 2 deletions hvplot/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,13 @@ def _process_symmetric(self, symmetric, clim, check_symmetric_max):
def _process_crs(self, data, crs):
"""Given crs as proj4 string, data.attr, or cartopy.crs return cartopy.crs
"""
# get the proj string: either the value of data.attrs[crs] or crs itself
_crs = getattr(data, 'attrs', {}).get(crs or 'crs', crs)
if hasattr(data, 'rio') and data.rio.crs is not None:
# if data is a rioxarray
_crs = data.rio.crs.to_wkt()
else:
# get the proj string: either the value of data.attrs[crs] or crs itself
_crs = getattr(data, 'attrs', {}).get(crs or 'crs', crs)

try:
return process_crs(_crs)
except ValueError:
Expand Down
29 changes: 20 additions & 9 deletions hvplot/tests/testgeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@
import pandas as pd
import holoviews as hv

from hvplot.util import proj_to_cartopy


class TestGeo(TestCase):

def setUp(self):
if sys.platform == "win32":
raise SkipTest("Skip geo tests on windows for now")
try:
import xarray as xr
import xarray as xr # noqa
import rasterio # noqa
import geoviews # noqa
import cartopy.crs as ccrs
import cartopy.crs as ccrs # noqa
import rioxarray as rxr
except:
raise SkipTest('xarray, rasterio, geoviews, or cartopy not available')
raise SkipTest('xarray, rasterio, geoviews, cartopy, or rioxarray not available')
import hvplot.xarray # noqa
import hvplot.pandas # noqa
self.da = xr.open_rasterio(
self.da = rxr.open_rasterio(
pathlib.Path(__file__).parent / 'data' / 'RGB-red.byte.tif'
).sel(band=1)
self.crs = ccrs.epsg(self.da.crs.split('epsg:')[1])
).isel(band=0)
self.crs = proj_to_cartopy(self.da.spatial_ref.attrs['crs_wkt'])

def assertCRS(self, plot, proj='utm'):
import cartopy
Expand All @@ -46,9 +49,12 @@ def setUp(self):
if sys.platform == "win32":
raise SkipTest("Skip CRS inference on Windows")
super().setUp()

def test_plot_with_crs_as_proj_string(self):
plot = self.da.hvplot.image('x', 'y', crs=self.da.crs)
da = self.da.copy()
da.rio._crs = False # To not treat it as a rioxarray

plot = self.da.hvplot.image('x', 'y', crs="epsg:32618")
self.assertCRS(plot)

def test_plot_with_geo_as_true_crs_undefined(self):
Expand All @@ -64,19 +70,24 @@ def test_plot_with_crs_as_object(self):

def test_plot_with_crs_as_attr_str(self):
da = self.da.copy()
da.rio._crs = False # To not treat it as a rioxarray
da.attrs = {'bar': self.crs}
plot = da.hvplot.image('x', 'y', crs='bar')
self.assertCRS(plot)

def test_plot_with_crs_as_nonexistent_attr_str(self):
da = self.da.copy()
da.rio._crs = False # To not treat it as a rioxarray

# Used to test crs='foo' but this is parsed under-the-hood
# by PROJ (projinfo) which matches a geographic projection named
# 'Amersfoort'
with self.assertRaisesRegex(ValueError, "'name_of_some_invalid_projection' must be"):
self.da.hvplot.image('x', 'y', crs='name_of_some_invalid_projection')
da.hvplot.image('x', 'y', crs='name_of_some_invalid_projection')

def test_plot_with_geo_as_true_crs_no_crs_on_data_returns_default(self):
da = self.da.copy()
da.rio._crs = False # To not treat it as a rioxarray
da.attrs = {'bar': self.crs}
plot = da.hvplot.image('x', 'y', geo=True)
self.assertCRS(plot, 'eqc')
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def get_setup_version(reponame):
'pooch',
'scipy',
'ipywidgets',
'rioxarray',
hoxbro marked this conversation as resolved.
Show resolved Hide resolved
],
'examples': _examples,
'doc': _examples + [
Expand Down