xrspatial.convolution.calc_cellsize#

xrspatial.convolution.calc_cellsize(raster)[source]#

Calculates cell size of an array based on its attributes. Supported units are: meter, kelometer, foot, and mile. Cellsize will be converted to meters.

Parameters

raster (xarray.DataArray) – 2D array of input values.

Returns

  • cellsize (tuple) – Tuple of (cellsize_x, cellsize_y).

  • Where cellsize_x is the size of cells in x-direction,

  • and cellsize_y is the size of cells in y-direction.

Examples

>>> import numpy as np
>>> import xarray as xr
>>> h, w = 100, 200
>>> data = np.ones((h, w))
>>> from xrspatial.convolution import calc_cellsize
>>> # cellsize that already specified as an attribute of input raster
>>> raster_1 = xr.DataArray(data, attrs={'res': (0.5, 0.5)})
>>> calc_cellsize(raster_1)
(0.5, 0.5)
>>> # if no unit specified, default to meters
>>> raster_2 = xr.DataArray(data, dims=['y', 'x'])
>>> raster_2['y'] = np.linspace(1, h, h)
>>> raster_2['x'] = np.linspace(1, w, w)
>>> calc_cellsize(raster_2)
(1.0, 1.0)
# convert cellsize to meters
>>> raster_3 = xr.DataArray(
...     data, dims=['y', 'x'], attrs={'unit': 'km'})
>>> raster_3['y'] = np.linspace(1, h, h)
>>> raster_3['x'] = np.linspace(1, w, w)
>>> calc_cellsize(raster_3)
>>> (1000.0, 1000.0)