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.
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.
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:
processing
module:import processing
pth = "/qgis_data/rasters/path/" rasterPth = pth + "elevation.asc" vectorPth = pth + "path.shp" pointsPth = pth + "points.shp" elvPointsPth = pth + "elvPoints.shp"
rasterLyr = QgsRasterLayer(rasterPth, "Elevation") rasterLyr.isValid() vectorLyr = QgsVectorLayer(vectorPth, "Path", "ogr") vectorLyr.isValid()
QgsMapLayerRegistry.instance().addMapLayers([vectorLyr, rasterLyr])
processing.runalg("saga:convertlinestopoints", vectorLyr, False, 1, pointsPth)
processing.runandload("saga:addgridvaluestopoints", pointsPth, rasterPth, 0, elvPointsPth)
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:
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.
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: