|
| 1 | +import datashader as ds |
| 2 | +import datashader.transfer_functions as tf |
1 | 3 | from math import ceil |
2 | 4 | import numba as nb |
3 | 5 | import numpy as np |
4 | 6 | import xarray as xr |
5 | | -import datashader.transfer_functions as tf |
6 | 7 |
|
7 | 8 | import dask.array as da |
8 | 9 |
|
@@ -272,3 +273,83 @@ def bands_to_img(r, g, b, nodata=1): |
272 | 273 | a = np.where(np.logical_or(np.isnan(r), r <= nodata), 0, 255) |
273 | 274 | data[:, :, 3] = a.astype(np.uint8) |
274 | 275 | return tf.Image.fromarray(data, 'RGBA') |
| 276 | + |
| 277 | + |
| 278 | +def resample( |
| 279 | + raster, |
| 280 | + layer=None, |
| 281 | + height=512, |
| 282 | + width=512, |
| 283 | + x_range=None, |
| 284 | + y_range=None, |
| 285 | + agg='mean', |
| 286 | + interpolate='linear', |
| 287 | + max_mem=None): |
| 288 | + """ |
| 289 | + Resample a xarray.DataArray by canvas size and bounds. |
| 290 | +
|
| 291 | + Handles 2D or 3D xarray.DataArray, assuming that the last two |
| 292 | + array dimensions are the y-axis and x-axis that are to be |
| 293 | + resampled. If a 3D array is supplied a layer may be specified |
| 294 | + to resample to select the layer along the first dimension to |
| 295 | + resample. |
| 296 | +
|
| 297 | + If there are memory constraints they may be defined using the |
| 298 | + max_mem parameter, which determines how large the chunks in |
| 299 | + memory may be. |
| 300 | +
|
| 301 | + Parameters |
| 302 | + ---------- |
| 303 | + raster : xarray.DataArray |
| 304 | + 2D or 3D labeled data array. |
| 305 | + layer : float, optional |
| 306 | + For a 3D array, value along the z dimension. |
| 307 | + height : int, default=512 |
| 308 | + Height of the output aggregate in pixels. |
| 309 | + width : int, default=512 |
| 310 | + Width of the output aggregate in pixels. |
| 311 | + x_range : tuple of int, optional |
| 312 | + A tuple representing the bounds inclusive space ``[min, max]`` |
| 313 | + along the x-axis. |
| 314 | + y_range : tuple of int, optional |
| 315 | + A tuple representing the bounds inclusive space ``[min, max]`` |
| 316 | + along the y-axis. |
| 317 | + agg : Reduction, default=mean |
| 318 | + Resampling mode when downsampling raster. The supported |
| 319 | + options include: first, last, mean, mode, var, std, min, |
| 320 | + The agg can be specified as either a string name or as a |
| 321 | + reduction function, but note that the function object will |
| 322 | + be used only to extract the agg type (mean, max, etc.) and |
| 323 | + the optional column name; the hardcoded raster code |
| 324 | + supports only a fixed set of reductions and ignores the |
| 325 | + actual code of the provided agg. |
| 326 | + interpolate : str, default=linear |
| 327 | + Method to use for interpolation between specified values. |
| 328 | + ``nearest`` means to use a single value for the whole |
| 329 | + triangle, and ``linear`` means to do bilinear interpolation |
| 330 | + of the pixels within each triangle (a weighted average of the |
| 331 | + vertex values). |
| 332 | + max_mem : int, optional |
| 333 | + The maximum number of bytes that should be loaded into memory |
| 334 | + during the regridding operation. |
| 335 | +
|
| 336 | + References |
| 337 | + ---------- |
| 338 | + - https://datashader.org/_modules/datashader/core.html#Canvas |
| 339 | + """ |
| 340 | + cvs = ds.Canvas( |
| 341 | + plot_height=height, |
| 342 | + plot_width=width, |
| 343 | + x_range=x_range, |
| 344 | + y_range=y_range, |
| 345 | + ) |
| 346 | + |
| 347 | + out = cvs.raster( |
| 348 | + raster, |
| 349 | + layer=layer, |
| 350 | + agg=agg, |
| 351 | + interpolate=interpolate, |
| 352 | + max_mem=max_mem, |
| 353 | + ) |
| 354 | + |
| 355 | + return out |
0 commit comments