Contours provides an effective visualization of terrain data by tracing a line 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 as well as a Processing Toolbox algorithm.
This recipe uses the DEM from https://geospatialpython.googlecode.com/files/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:
processing
module.import processing
rasterLyr = QgsRasterLayer("/qgis_data/rasters/dem.asc", "DEM") rasterLyr.isValid()
mapLayerRegistry
method:QgsMapLayerRegistry.instance().addMapLayers([rasterLyr])
processing.runandload("gdalogr:contour", rasterLyr, 50.0, "Elv", None, "/qgis_data/rasters/contours.shp")
The contour algorithm creates a vector dataset, that is a shapefile. The layer attribute table contains the elevation values for each line. Depending on the resolution of the elevation dataset, you may need to change the contour interval to stop the contours from becoming too crowded or too sparse at your desired map resolution. Usually, autogenerated contours like this are a starting point, and you must manually edit the result to make it visually appealing. You may want to smoothen lines or remove unnecessary small loops.