xrspatial.slope.slope

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

Returns slope of input aggregate in degrees.

Parameters
  • agg (xr.DataArray) – 2D array of elevation data.

  • name (str, default='slope') – Name of output DataArray.

Returns

slope_agg – 2D array of slope values. All other input attributes are preserved.

Return type

xr.DataArray of same type as agg

References

Examples

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

from xrspatial import generate_terrain, slope

# Generate Example Terrain
W = 500
H = 300

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
)

# 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 Slope Aggregate Array
slope_agg = slope(agg = terrain_agg, name = 'Slope')

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

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

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

(png, hires.png, pdf)

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

(png, hires.png, pdf)

>>> print(terrain_agg[200:203, 200:202])
<xarray.DataArray 'Elevation' (lat: 3, lon: 2)>
array([[1264.02296597, 1261.947921  ],
       [1285.37105519, 1282.48079719],
       [1306.02339636, 1303.4069579 ]])
Coordinates:
* lon      (lon) float64 -3.96e+06 -3.88e+06
* lat      (lat) float64 6.733e+06 6.867e+06 7e+06
Attributes:
    res:            (80000.0, 133333.3333333333)
    Description:    Example Terrain
    units:          km
    Max Elevation:  4000

>>> print(slope_agg[200:203, 200:202])
<xarray.DataArray 'Slope' (lat: 3, lon: 2)>
array([[0.00757718, 0.00726441],
       [0.00893266, 0.00916095],
       [0.00773291, 0.00699103]], dtype=float32)
Coordinates:
* lon      (lon) float64 -3.96e+06 -3.88e+06
* lat      (lat) float64 6.733e+06 6.867e+06 7e+06
Attributes:
    res:            (80000.0, 133333.3333333333)
    Description:    Example Slope
    units:          deg
    Max Elevation:  4000