I have one DEM raster layer with elevation values and one vector layer of the roads; same size, extension and CRS.

I want to do what this guy did here:


But instead of using slope I want to use simple elevation.

I converted my raster into polygons and tried to intersect (Vector > Geoprocessing > Intersection) it with the roads layer, but apparently is too much for Qgis to calculate, since it keeps crashing even before 2% of the processing. I would like to know another approach for doing this?

What I need in the end it's a table/data.frame that relates the "lines"(roads) and the elevation. Because I will use this in glm model.

  • If you wish to also ask about ArcGIS then you can always do that as a separate question.
    – PolyGeo
    Jun 22, 2017 at 12:13
  • 1
    Try using a spatial join.
    – csk
    Jun 23, 2017 at 21:18

2 Answers 2


(I would suggest not to follow the approach in the linked blog post.)

You should be able to use more straightforward tool, Profiles from lines in QGIS Processing Toolbox | SAGA | Terrain Analysis - Profiles.

It will return new point vector layer with elevation values at each pixel along the designated line (road). Saving this attribute table into csv format will make it easy to handle the data in R for further analysis.

  • That is very useful, thank you. I ended up using mean elevation for the whole polygon.
    – TFA
    May 1, 2018 at 11:23
  • @TFA Thanks! Do you mind if I add a tag SAGA to your question? It was not in your original question, though. (...and potentially other tags like elevation?)
    – Kazuhito
    May 1, 2018 at 11:46
  • Sure. Go ahead.
    – TFA
    May 2, 2018 at 14:23
  • @GabrielC. Drape will write a z-values from a raster to the geometries of 3D or 2.5D features. A 3D line, comprised of xyz coords, will have as many elevation values as it has vertices. With 'add geometry attributes' used for lines, you thus wont get an elevation value, only length... If you do the same for a point layer, you would get x, y, and z-coords! Which should yield the same as if you extract vertices and then use these to sample a DEM raster.
    – Kay
    Feb 7, 2019 at 16:40
  • @Kay You're right. I read the doc again.
    – Gabriel
    Feb 7, 2019 at 16:52

The blog post example is completely wrong (see the comments box of the blog!).. With the recent QGIS version (3.4.) I can do it easily with the tool "Extract vertices", which gives you all line vertices an the cummulated distances from the start vertex.

Then you sample a DEM raster, using the vertices' point layer with the tool "Sample raster values", resulting in a new point layer with the raster (elevation) values joined to the single vertices/features.

As next step you need the segment distances and the elevation differences for calculating the grades.

With the field calculator you can calculate the single segment distances, with something along the lines

attribute( get_feature_by_id(
'vertices_layer',   @row_number) ,  'id') = attribute( get_feature_by_id('vertices_layer',  @row_number-1) ,  'id')
THEN attribute( get_feature_by_id('vertices_layer',  @row_number) ,  'distance') - attribute( get_feature_by_id('vertices_layer',   @row_number-1) ,  'distance')

The condition tests if segments between consecutive vertices belong to the same feature, as given by the id. Using the generic variable @row_number, we assume that the points/vertices are ordered as in the original lines.

The elevation difference you get, by putting something like the below to the field calculator:

 attribute( get_feature_by_id('vertices_layer',   @row_number+1) ,  'rvalue_1') -  attribute( get_feature_by_id('vertices_layer',   @row_number) ,  'rvalue_1')

Again, you'd need to make a CASE WHEN condition, as above, testing if the consecutive vertices belong to the same line feature!

With the vertex distance and the elevation difference you can calculate the segments' grade. This values you use in an aggregate calculation to get the average grades for the line features (aggregating over segments with mean function and grouping by id). Then you need to join these values back to the lines layer.

If you only want the elevation, you stop after sampling the DEM raster and aggregate the elevations, then join back by id.. But what's that good for? Average elevation of a line feature?


I dug a little deeper into this, and obviously everything is way easier - just use tool "explode lines", then "drape (set z-values from raster)" using your DEM as raster. The elevations you get from the segments with the field calculator by expression:

z( end_point( $geometry))


z( start_point( $geometry))

Then you could aggregate over the segments with the line ids as grouping variable, to yield an aggregate value for each original line.

If you want the grades (%) you could use the field calculator:

  (z( end_point( $geometry))-z( start_point( $geometry)) ) / $length * 100

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.