xrspatial.viewshed.viewshed#

xrspatial.viewshed.viewshed(raster: xarray.core.dataarray.DataArray, x: Union[int, float], y: Union[int, float], observer_elev: float = 0, target_elev: float = 0) xarray.core.dataarray.DataArray[source]#

Calculate viewshed of a raster (the visible cells in the raster) for the given viewpoint (observer) location.

Parameters
  • raster (xr.DataArray) – Input raster image.

  • x (int, float) – x-coordinate in data space of observer location.

  • y (int, float) – y-coordinate in data space of observer location.

  • observer_elev (float) – Observer elevation above the terrain.

  • target_elev (float) – Target elevation offset above the terrain.

Returns

viewshed – A cell x in the visibility grid is recorded as follows: If it is invisible, then x is set to INVISIBLE. If it is visible, then x is set to the vertical angle w.r.t the viewpoint.

Return type

xarray.DataArray

Examples

>>> import numpy as np
>>> import xarray as xr
>>> from xrspatial import viewshed
>>> data = np.array([
...     [0, 0, 1, 0, 0],
...     [1, 3, 0, 0, 0],
...     [10, 2, 5, 2, -1],
...     [11, 1, 2, 9, 0]])
>>> terrain = xr.DataArray(data, dims=['y', 'x'])
>>> h, w = data.shape
>>> terrain['y'] = np.linspace(1, h, h)
>>> terrain['x'] = np.linspace(1, w, w)
>>> terrain
<xarray.DataArray (y: 4, x: 5)>
array([[ 0,  0,  1,  0,  0],
       [ 1,  3,  0,  0,  0],
       [10,  2,  5,  2, -1],
       [11,  1,  2,  9,  0]])
Coordinates:
  * y        (y) float64 1.0 2.0 3.0 4.0
  * x        (x) float64 1.0 2.0 3.0 4.0 5.0
>>> viewshed(terrain, x=3, y=2)
<xarray.DataArray (y: 4, x: 5)>
array([[ -1.        ,  90.        , 135.        ,  90.        , -1.],
       [ -1.        , 161.56505118, 180.        ,  90.        , 90.],
       [167.39561735, 144.73561032, 168.69006753, 144.73561032, -1.],
       [165.57993189,  -1.        ,  -1.        , 166.0472636 , -1.]])
Coordinates:
  * x        (x) float64 1.0 2.0 3.0 4.0 5.0
  * y        (y) float64 1.0 2.0 3.0 4.0