Finding the least cost path

Least cost path (LCP) analysis is the raster equivalent of network analysis, which is used to find the optimal path between two points in a raster. In this recipe, we'll perform LCP analysis on a digital elevation model (DEM).

Getting ready

You need to download the following DEM and extract the ZIP file to your qgis_data/rasters directory: https://geospatialpython.googlecode.com/svn/lcp.zip

How to do it...

We will load our DEM and two shapefiles consisting of start and end points. Then, we'll use GRASS through the Processing Toolbox to create a cumulative cost layer that assigns a cost to each cell in a raster based on its elevation, the value of the other cells around it, and its distance to and from the end points.

Then, we'll use a SAGA processing algorithm to find the least cost path between two points. Finally, we'll load the output onto the map. To do this, we need to perform the following steps:

  1. First, we'll import the QGIS processing Python library:
    import processing
    
  2. Now, we'll set the paths to the layers, as follows:
    path = "/Users/joellawhead/qgis_data/rasters"/"
    dem = path + "dem.asc"
    start = path + "start-point.shp"
    finish = path + "end-point.shp"
    
  3. We need the DEM's extent as a string for the algorithms:
    demLyr = QgsRasterLayer(dem, "DEM")
    ext = demLyr.extent()
    xmin = ext.xMinimum()
    ymin = ext.yMinimum()
    xmax = ext.xMaximum()
    ymax = ext.xMaximum()
    box = "%s,%s,%s,%s" % (xmin,xmax,ymin,ymax)
    
  4. Using the following code, we will establish the end points as layers:
    a = QgsVectorLayer(start, "Start", "ogr")
    b = QgsVectorLayer(finish, "End", "ogr")
    
  5. Then, we'll create the cumulative cost raster, specifying the algorithm name, cost layer (DEM), start point layer, end point layer, speed or accuracy option, keep null values option, extent of interest, cell size (0 for default), and some additional defaults:
    tmpCost = processing.runalg("grass:r.cost",dem,a,b,
    False,False,box,0,-1,0.0001,None)
    cost = tmpCost["output"]
    
  6. We also need to combine the points into a single layer for the SAGA algorithm:
    tmpMerge = processing.runalg("saga:mergeshapeslayers",start,finish,None)
    merge = tmpMerge["OUT"]
    
  7. Next, we set up the inputs and outputs for the LCP algorithm:
    vLyr = QgsVectorLayer(merge, "Destination Points", "ogr")
    rLyr = QgsRasterLayer(cost, "Accumulated Cost")
    line = path + "path.shp"
    
  8. Then, we run the LCP analysis using the following code:
    results = processing.runalg("saga:leastcostpaths",lyr,rLyr,demLyr,None,line)
    
  9. Finally, we can load the path to view it:
    path = QgsVectorLayer(line, "Least Cost Path", "ogr")
    QgsMapLayerRegistry.instance().addMapLayers([demLyr,  vLyr, path])
    

How it works...

GRASS has an LCP algorithm too, but the SAGA algorithm is easier to use. GRASS does a great job of creating the cost grid. Processing Toolbox algorithms allow you to create temporary files that are deleted when QGIS closes. So, we use temporary files for the intermediate products, including the cost grid and the merged shapefile.

..................Content has been hidden....................

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