Skip to content

Commit 1da6ebc

Browse files
committed
added band_to_img utils func
1 parent 9e39037 commit 1da6ebc

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

xrspatial/utils.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
import numba as nb
33
import numpy as np
44
import xarray as xr
5+
import datashader.transfer_functions as tf
56

67
from numba import cuda
78

89
try:
910
import cupy
1011
if cupy.result_type is np.result_type:
11-
# Workaround until cupy release of https://github.com/cupy/cupy/pull/2249
12+
# hack until cupy release of https://github.com/cupy/cupy/pull/2249
1213
# Without this, cupy.histogram raises an error that cupy.result_type
1314
# is not defined.
1415
cupy.result_type = lambda *args: np.result_type(
1516
*[arg.dtype if isinstance(arg, cupy.ndarray) else arg
1617
for arg in args]
1718
)
18-
except:
19+
except ImportError:
1920
cupy = None
2021

2122

@@ -100,7 +101,7 @@ def get_dataarray_resolution(agg: xr.DataArray):
100101
cellsize_y = cellsize
101102
else:
102103
cellsize_x, cellsize_y = calc_res(agg)
103-
104+
104105
return cellsize_x, cellsize_y
105106

106107

@@ -160,3 +161,14 @@ def height_implied_by_aspect_ratio(W, X, Y):
160161
plot_height = height_implied_by_aspect_ratio(plot_width, x_range, y_range)
161162
"""
162163
return int((W * (Y[1] - Y[0])) / (X[1] - X[0]))
164+
165+
166+
def bands_to_img(r, g, b, nodata=1):
167+
h, w = r.shape
168+
data = np.zeros((h, w, 4), dtype=np.uint8)
169+
data[:, :, 0] = (r).astype(np.uint8)
170+
data[:, :, 1] = (g).astype(np.uint8)
171+
data[:, :, 2] = (b).astype(np.uint8)
172+
a = np.where(np.logical_or(np.isnan(r), r <= nodata), 0, 255)
173+
data[:, :, 3] = a.astype(np.uint8)
174+
return tf.Image.fromarray(data, 'RGBA')

0 commit comments

Comments
 (0)