
QGIS Python Programming Cookbook, Second Edition
By :

Contours provide an effective visualization of terrain data by tracing isolines along the same elevation to form a loop at set intervals in the dataset. Similar to the hillshade capability in QGIS, the Contour tool is provided by GDAL, both as a menu option under the Raster menu in the Extraction category and as a Processing Toolbox algorithm.
This recipe uses the DEM from https://github.com/GeospatialPython/Learn/raw/master/dem.zip, which is used in the other recipes as well.
Unzip the file named dem.asc
and place it in your /qgis_data/rasters
directory.
In this recipe, we will load and validate the DEM layer, add it to the map, and then produce and load the contour vector as a layer. To do this, we need to perform the following steps:
Start QGIS.
From the Plugins menu, select Python Console.
Import the processing
module.
import processing
Load and validate the DEM:
rasterLyr = QgsRasterLayer("/qgis_data/rasters...