Adding elevation data to line vertices using a digital elevation model

If you have a transportation route through some terrain, it is useful to know the elevation profile of that route. This operation can be accomplished using the points that make up the line along the route to query a DEM and to assign elevation values to that point. In this recipe, we'll do exactly that.

Getting ready

You will need an elevation grid and a route. You can download this dataset from https://geospatialpython.googlecode.com/svn/path.zip.

Unzip the path directory containing a shapefile and the elevation grid. Place the whole path directory in your qgis_data/rasters directory.

How to do it...

We will need two processing algorithms to complete this recipe. We will load the raster and vector layers, convert the line feature to points, and then use these points to query the raster. The resulting point dataset will serve as the elevation profile for the route. 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. Set up the filenames as variables, so they can be used throughout the script:
    pth = "/qgis_data/rasters/path/"
    rasterPth = pth + "elevation.asc"
    vectorPth = pth + "path.shp"
    pointsPth = pth + "points.shp"
    elvPointsPth = pth + "elvPoints.shp"
    
  5. Load and validate the source layers:
    rasterLyr = QgsRasterLayer(rasterPth, "Elevation")
    rasterLyr.isValid()
    vectorLyr = QgsVectorLayer(vectorPth, "Path", "ogr")
    vectorLyr.isValid()
    
  6. Add the layers to the map:
    QgsMapLayerRegistry.instance().addMapLayers([vectorLyr, rasterLyr])
    
  7. Create an intermediate point dataset from the line using a SAGA algorithm in the Processing Toolbox:
    processing.runalg("saga:convertlinestopoints", vectorLyr, False, 1, pointsPth)
    
  8. Finally, use another processing algorithm from SAGA to create the final dataset with the grid values assigned to the points:
    processing.runandload("saga:addgridvaluestopoints", pointsPth, rasterPth, 0, elvPointsPth)
    

How it works...

The following image saved from QGIS shows the DEM, route line, and elevation points with elevation labels, all displayed on the map, with some styling:

How it works...

It is necessary to convert the lines to points because a line feature can only have one set of attributes. You can perform the same operation with a polygon as well.

There's more...

Instead of running two algorithms, we can build a processing script that combines these two algorithms into one interface and then added it to the toolbox. In the Processing Toolbox, there is a category called Scripts, which has a tool called Create new script. Double-clicking on this tool will bring up an editor that lets you build your own processing scripts. Depending on your platform, you may need to install or configure SAGA to use this algorithm. You can find binary packages for Linux at http://sourceforge.net/p/saga-gis/wiki/Binary%20Packages/.

Also, on Linux, you may need to change the following option:

  1. In the Processing menu, select Options….
  2. In the Options dialog, open the Providers tree menu and then open the Saga tree menu.
  3. Uncheck the Use 2.0.8 syntax option.
..................Content has been hidden....................

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