Creating vector contours from elevation data

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.

Getting ready

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.

How to do it...

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:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. Import the processing module.
    import processing
    
  4. Load and validate the DEM:
    rasterLyr = QgsRasterLayer("/qgis_data/rasters/dem.asc", "DEM")
    rasterLyr.isValid()
    
  5. Add the DEM to the map using the mapLayerRegistry method:
    QgsMapLayerRegistry.instance().addMapLayers([rasterLyr])
    
  6. Run the contour algorithm and draw the results on top of the DEM layer, specifying the algorithm name, layer reference, interval between contour lines in map units, name of the vector data attribute field that will contain the elevation value, any extra parameters, and output filename:
    processing.runandload("gdalogr:contour", rasterLyr, 50.0, "Elv", None, "/qgis_data/rasters/contours.shp")
    
  7. Verify that the output in QGIS looks similar to the following screenshot:
    How to do it...

    This recipe overlays the resulting elevation contours over the DEM as a way to convert elevation data into a vector data set.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset