From 76a6e8282456dd281fd0385fa2615ccc48dd5057 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Wed, 13 Sep 2023 21:47:31 -0700 Subject: [PATCH 1/2] Add alpha dimension --- geoviews/util.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/geoviews/util.py b/geoviews/util.py index 550c354f..67dc92c8 100644 --- a/geoviews/util.py +++ b/geoviews/util.py @@ -745,6 +745,8 @@ def from_xarray(da, crs=None, apply_transform=False, nan_nodata=False, **kwargs) from .element.geo import RGB, HvRGB el = RGB if 'crs' in kwargs else HvRGB vdims = el.vdims[:bands] + if bands == 4: + vdims.append("A") el = el(data, [x, y], vdims, **kwargs) if hasattr(el.data, 'attrs'): el.data.attrs = da.attrs From 1702d7204a0ec66b99c08d1fc7a53e49b8769973 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Wed, 13 Sep 2023 21:59:57 -0700 Subject: [PATCH 2/2] Add test --- geoviews/tests/assets/test_alpha.tif | Bin 0 -> 422 bytes geoviews/tests/test_util.py | 13 +++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 geoviews/tests/assets/test_alpha.tif diff --git a/geoviews/tests/assets/test_alpha.tif b/geoviews/tests/assets/test_alpha.tif new file mode 100644 index 0000000000000000000000000000000000000000..1cbffeb3dbe629a4e25bde2a44236c01005e1800 GIT binary patch literal 422 zcmebD)MDUZU|-n#0L7jGF*8&gq>c?qjUXcnSno8TgeX)T zq(=-%jW|?|08m^8$($f0HNikONZ&gk=4<9*UW$Ov>L2r_J`xZ^EWV9(HyaXr#0-X5X~ zHz{$8@s_ud=z*-f42z(-9u6>-V6zz589e4_2@+seu^}rvdzYCp-WE literal 0 HcmV?d00001 diff --git a/geoviews/tests/test_util.py b/geoviews/tests/test_util.py index 4cd0c9fc..579cd237 100644 --- a/geoviews/tests/test_util.py +++ b/geoviews/tests/test_util.py @@ -1,3 +1,5 @@ +from pathlib import Path + import cartopy.crs as ccrs import pytest @@ -9,6 +11,7 @@ except ImportError: rxr = None +ASSETS_DIR = Path(__file__).parent / "assets" @pytest.mark.parametrize( "raw_crs", @@ -45,3 +48,13 @@ def test_from_xarray(): assert isinstance(output, gv.RGB) assert sorted(map(str, output.kdims)) == ["x", "y"] assert isinstance(output.crs, ccrs.CRS) + + +@pytest.mark.skipif(rxr is None, reason="Needs rioxarray to be installed") +def test_from_xarray_with_alpha(): + file = str(ASSETS_DIR / "test_alpha.tif") + output = from_xarray(rxr.open_rasterio(file)) + + assert isinstance(output, gv.RGB) + assert sorted(map(str, output.kdims)) == ["x", "y"] + assert set(map(str, output.vdims)) == set(["R", "G", "B", "A"])