Using QGIS 3.2 what is the best way to add slope and/or % grade to a county-wide street layer using the following data:

Streets (polylines) = 35k records (10mb)

Contours (polylines) = 500k records (over 1gb)

DEM layer (raster) = 100mb

Attempted this method but it uses out of date tools and has other issues.

  • 1
    Using which QGIS? Also, what have you tried on your own, aside or based on the linked tutorial?
    – Erik
    Commented Apr 29, 2019 at 12:44
  • QGIS 3.2 tried reading through gis.stackexchange.com/questions/32580/… and tried a few of the Grass tools without success.
    – sirgeo
    Commented Apr 29, 2019 at 13:18

1 Answer 1


Here's a simple method for adding slope values and/or percent grade to a street layer as attributes.

  1. Densify the roads to add vertices. It's up to you how far apart you want the vertices to be (see note below).
    • Slope values will be calculated from the elevation at the endpoints of each line segment, so the more vertices you add in this step, the more accurate the calculated slope values will be.
  2. Use the Drape (set Z value from raster) tool to add elevation from the DEM to the vertices. This converts the 2-D road network to three dimensions.
    • Z values are stored as the Z coordinate of each vertex. You can access Z values (eg in the Field Calculator) with the expression $z, just as you would access the X and Y values with $x and $y.
  3. Explode lines to split each line segment into a separate line feature.
  4. Use the Field Calculator to calculate slope value and/or percent grade for each feature.

Note: This method may require some trial and error. If the algorithms take a long time to run, it may be more convenient to test these steps on a subset of your data, until you figure out what vertex density will give you the results you want

  • the Drape tool is only adding one column called "Cat" for some reason. Is there a setting I am missing here? i.imgur.com/zbdOx0E.png
    – sirgeo
    Commented Apr 30, 2019 at 4:06
  • 1
    As I explained in the bullet point under step 2, Drape doesn't add the Z values to the attribute table. It adds them to the geometry. You can add Z values to the attribute table by adding a new field with the field calculator and the expression $z.
    – csk
    Commented Apr 30, 2019 at 17:04
  • field calculator says $z is invalid... while using abs(z(start_point($geometry)) - z(end_point($geometry)))/$length*100 does seem to produce the slope value. Is there a way to force v.drape to display the z values in new fields?
    – sirgeo
    Commented May 1, 2019 at 13:50
  • 1
    Oh, right. The $z function only works for points, because a line doesn't have a single Z value. Each vertex has its own Z value. You've already figured out how to extract the Z value of the start and end points. Just use the Field Calculator to create "Z_start" and "Z_end" fields, using z(start_point($geometry)) and z(end_point($geometry)).
    – csk
    Commented May 1, 2019 at 16:02
  • There is a QGis plugin to do this, named "Road slope calculator".
    – MLourdes
    Commented Jun 2, 2023 at 13:17

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.