Note
Go to the end to download the full example code.
NumPy interfacing#
This example demonstrates NumPy interfacing with rasters on Rasters
. See Masked-array NumPy interface for more details.
We open a raster.
import geoutils as gu
filename_rast = gu.examples.get_path("exploradores_aster_dem")
rast = gu.Raster(filename_rast)
rast.plot(cmap="terrain")
The NumPy interface allows to use almost any NumPy function directly on the raster.
import numpy as np
# Get the x and y gradient as 1D arrays
gradient_y, gradient_x = np.gradient(rast)
# Estimate the orientation in degrees casting to 2D
aspect = np.arctan2(-gradient_x, gradient_y)
aspect = (aspect * 180 / np.pi) + np.pi
# We copy the new array into a raster
asp = rast.copy(new_array=aspect)
asp.plot(cmap="twilight", cbar_title="Aspect (degrees)")
Important
For rigorous slope and aspect calculation (matching that of GDAL), check-out our sister package xDEM.
We use NumPy logical operations to isolate the terrain oriented South and above three thousand meters. The rasters will be logically cast to a
Mask
.
We plot the mask.
Total running time of the script: (0 minutes 3.833 seconds)