I've got two locations, A and B, and a DTM covering the region between A and B. Say I want to drive from A to B. I want the shortest route possible, but avoiding regions with slopes higher than, let's say 30 degrees.

Can QGIS calculate such a path?

If needed I can also script using python.


There's a python example in the GDAL/OGR Raster Cookbook (look for 'Least Cost Path analysis'). You'll need to install scikit-image for that.

If using QGIS:-

  • To get your cost raster, generate a slope raster in the usual way
  • then use Raster Calculator to set any slope pixel above a threshold to a really high value like 9999
  • to find your coordinates for the script, use the Coordinate Capture plugin

That will find the route with the least overall slope between A and B. The output is a binary raster.

It won't necessarily be the shortest path distance-wise, but it would represent the easiest route between A and B (for example, for hillwalkers or cyclists)


I'm not sure about one tool to do that, but if you want to stay in vector routing rather than a cost raster surface as per Steven Kay's answer, you can calculate the slope of your graph edges and use them as an attribute when routing. Generally you'd need to:

  1. Split all your edges into acceptably-small pieces* (actual length you choose depends primarily on your resolution of your elevation model).
  2. For the start and end of each edge, find the elevation.
  3. Use the start and end points' elevations to determine the approximate slope of the edge.
  4. Use the edge slope to influence speed (in your case, it might just be make edges with slope > 30° impassable; but Tobler's hiking function is a useful approximation for influencing walking speed, and I've used something else when trying to approximate fuel consumption with respect to slope and speed assuming an average car).

This can be made more complex if you need, by actually considering three dimensional edges that have been draped over the DTM, rather than just splitting your edges and only considering relatively few intermediate points.

Once you have a topological network with slope attributes and/or modified travel time attributes and/or just impasses on roads with slope over your threshold, there are QGIS plugins for determining the shortest path, but I actually recommend pgRouting, as PostGIS will make the preparation work a lot easier (minus any learning curve).

* If you use very long edges, they may pass the crest of a hill, and so your calculation of slope will not be realistic.

  • +1. Nice one. The only thing missing is edges creation – FelixIP Oct 12 '15 at 5:34
  • Thanks for your reply! I think I'll first try the suggestion by Steven Kay. If that doesn't work I'll come back to your suggestion. – joosthoek Oct 12 '15 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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