|
2 | 2 | import numba as nb |
3 | 3 | import numpy as np |
4 | 4 | import xarray as xr |
| 5 | +import datashader.transfer_functions as tf |
5 | 6 |
|
6 | 7 | from numba import cuda |
7 | 8 |
|
8 | 9 | try: |
9 | 10 | import cupy |
10 | 11 | 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 |
12 | 13 | # Without this, cupy.histogram raises an error that cupy.result_type |
13 | 14 | # is not defined. |
14 | 15 | cupy.result_type = lambda *args: np.result_type( |
15 | 16 | *[arg.dtype if isinstance(arg, cupy.ndarray) else arg |
16 | 17 | for arg in args] |
17 | 18 | ) |
18 | | -except: |
| 19 | +except ImportError: |
19 | 20 | cupy = None |
20 | 21 |
|
21 | 22 |
|
@@ -100,7 +101,7 @@ def get_dataarray_resolution(agg: xr.DataArray): |
100 | 101 | cellsize_y = cellsize |
101 | 102 | else: |
102 | 103 | cellsize_x, cellsize_y = calc_res(agg) |
103 | | - |
| 104 | + |
104 | 105 | return cellsize_x, cellsize_y |
105 | 106 |
|
106 | 107 |
|
@@ -160,3 +161,14 @@ def height_implied_by_aspect_ratio(W, X, Y): |
160 | 161 | plot_height = height_implied_by_aspect_ratio(plot_width, x_range, y_range) |
161 | 162 | """ |
162 | 163 | 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