xrspatial.aspect.aspect

xrspatial.aspect.aspect(agg: xarray.core.dataarray.DataArray, name: Optional[str] = 'aspect') xarray.core.dataarray.DataArray[source]

Calculates the aspect value of an elevation aggregate.

Calculates, for all cells in the array, the downward slope direction of each cell based on the elevation of its neighbors in a 3x3 grid. The value is measured clockwise in degrees with 0 and 360 at due north. Flat areas are given a value of -1. Values along the edges are not calculated.

Parameters
  • agg (xarray.DataArray) – 2D NumPy, CuPy, NumPy-backed Dask, or Cupy-backed Dask array of elevation values.

  • name (str, default='aspect') – Name of ouput DataArray.

Returns

aspect_agg – 2D aggregate array of calculated aspect values. All other input attributes are preserved.

Return type

xarray.DataArray of the same type as agg

References

Examples

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

from xrspatial import generate_terrain, aspect

# Generate Example Terrain
W = 800
H = 600

template_terrain = xr.DataArray(np.zeros((H, W)))
x_range=(-20e6, 20e6)
y_range=(-20e6, 20e6)

terrain_agg = generate_terrain(
    template_terrain, x_range=x_range, y_range=y_range, seed=1, zfactor=1000
)

# Edit Attributes
terrain_agg = terrain_agg.assign_attrs(
    {
        'Description': 'Example Terrain',
        'units': 'km',
        'Max Elevation': '4000',
    }
)

terrain_agg = terrain_agg.rename({'x': 'lon', 'y': 'lat'})
terrain_agg = terrain_agg.rename('Elevation')

# Create Aspect Aggregate Array
aspect_agg = aspect(agg = terrain_agg, name = 'Aspect')

# Edit Attributes
aspect_agg = aspect_agg.assign_attrs(
    {
        'Description': 'Example Aspect',
        'units': 'deg',
    }
)

# Plot Terrain
terrain_agg.plot(cmap = 'terrain', aspect = 2, size = 4)
plt.title("Terrain")
plt.ylabel("latitude")
plt.xlabel("longitude")

# Plot Aspect
aspect_agg.plot(aspect = 2, size = 4)
plt.title("Aspect")
plt.ylabel("latitude")
plt.xlabel("longitude")
../../_images/xrspatial-aspect-aspect-1_00.png

(png, hires.png, pdf)

../../_images/xrspatial-aspect-aspect-1_01.png

(png, hires.png, pdf)

>>> print(terrain_agg[200:203, 200:202])
<xarray.DataArray 'Elevation' (lat: 3, lon: 2)>
array([[707.57051795, 704.3194383 ],
       [706.36271613, 705.4514285 ],
       [699.46372883, 703.7514251 ]])
Coordinates:
* lon      (lon) float64 -9.975e+06 -9.925e+06
* lat      (lat) float64 -6.633e+06 -6.567e+06 -6.5e+06
Attributes:
    res:            (50000.0, 66666.66666666667)
    Description:    Example Terrain
    units:          km
    Max Elevation:  4000
>>> print(aspect_agg[200:203, 200:202])
<xarray.DataArray 'Aspect' (lat: 3, lon: 2)>
array([[155.07530658, 146.26526699],
       [194.81685088, 136.55836607],
       [203.14170549, 187.97760934]])
Coordinates:
* lon      (lon) float64 -9.975e+06 -9.925e+06
* lat      (lat) float64 -6.633e+06 -6.567e+06 -6.5e+06
Attributes:
    res:            (50000.0, 66666.66666666667)
    Description:    Example Aspect
    units:          deg
    Max Elevation:  4000