Skip to content

Commit 941bb02

Browse files
authored
add resample notebook (#452)
* add resample notebook * update notebook and move resample function to xrpstial utils module * fix pep8 code format * update resample notebook * add dask section into resample notebook * add resample function test
1 parent 83df551 commit 941bb02

File tree

3 files changed

+1676
-1
lines changed

3 files changed

+1676
-1
lines changed

examples/resample.ipynb

Lines changed: 1585 additions & 0 deletions
Large diffs are not rendered by default.

xrspatial/tests/test_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from xrspatial.datasets import make_terrain
2+
from xrspatial.utils import resample
3+
4+
5+
def test_resample():
6+
terrain = make_terrain()
7+
terrain_res = resample(terrain, height=50, width=50)
8+
assert terrain_res.shape == (50, 50)
9+
assert terrain_res.chunks == ((50,), (50,))

xrspatial/utils.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import datashader as ds
2+
import datashader.transfer_functions as tf
13
from math import ceil
24
import numba as nb
35
import numpy as np
46
import xarray as xr
5-
import datashader.transfer_functions as tf
67

78
import dask.array as da
89

@@ -272,3 +273,83 @@ def bands_to_img(r, g, b, nodata=1):
272273
a = np.where(np.logical_or(np.isnan(r), r <= nodata), 0, 255)
273274
data[:, :, 3] = a.astype(np.uint8)
274275
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

Comments
 (0)