A hillshade, or shaded relief, is a technique to visualize elevation data in order to make it photorealistic for presentation as a map. This capability is part of GDAL and is available in QGIS in two different ways. It is a tool in the Terrain Analysis menu under the Raster menu and it is also an algorithm in the Processing Toolbox.
You will need to download a DEM from https://geospatialpython.googlecode.com/files/dem.zip.
Unzip the file named dem.asc
and place it in your /qgis_data/rasters
directory.
In this recipe, we will load the DEM layer and run the Hillshade
processing algorithm against it. To do this, we need to perform the following steps:
processing
module:import processing
rasterLyr = QgsRasterLayer("/qgis_data/rasters/dem.asc", "Hillshade") rasterLyr.isValid()
Hillshade
algorithm, providing the algorithm name, layer reference, band number, compute edges option, zevenbergen option for smoother terrain, z
-factor elevation exaggeration number, scaling ratio of vertical to horizontal units, azimuth (angle of the light source), altitude (height of the light source), and output image's name:processing.runandload("gdalogr:hillshade", rasterLyr, 1, False, False, 1.0, 1.0, 315.0, 45.0, "/qgis_data/rasters/hillshade.tif")
hillshade.tif
, looks similar to the following image in QGIS. It should be automatically loaded into QGIS via the processing.runandload()
method:The Hillshade
algorithm simulates a light source over an elevation dataset to make it more visually appealing. Most of the time, the only variables in the algorithm you need to alter are the z-factor, azimuth, and altitude to get different effects. However, if the resulting image doesn't look right, you may need to alter the scale. According to the GDAL documentation, if your DEM is in degrees, you should set a scale of 111120
, and if it is in meters, you should set a scale of 370400
. This dataset covers a small area such that a scale of 1 is sufficient. For more information on these values, see the gdaldem documentation at http://www.gdal.org/gdaldem.html.