2

I would like to extract flow accumulation and flow direction layers from a DEM calling the grass r.watershed module from a QGIS-python script using the processing class. I obtain just an empty map, while, using the processing GUI I get the map I was expecting. Here is my code:

from PyQt4.QtCore import *
import processing

# "mydem" is the loaded dem layer
dem=QgsMapLayerRegistry.instance().mapLayersByName("mydem")[0]
processing.runalg("grass:r.watershed", dem, None, None, None, None, 10, None, None, 300, None, 1, None, None, None, 40, "C:\\Users\\flc\\Desktop\\QGIS_Plugins\\layers\\acc.tif", "C:\\Users\\flc\\Desktop\\QGIS_Plugins\\layers\\dir.tif", None, None, None, None, None, None)

The 6th argument is the threshold. It is not needed for the computation of flow accumulation and flow direction, but in my trials with the GUI (the processing GUI, not the GRASS GUI) I found that the algorithm works when it is specified. However, I tried a lot of combinations of parameters. Do you have any idea about how could it work?

1 Answer 1

4

Using the following code worked for me:

from PyQt4.QtCore import *
import processing

# "mydem" is the loaded dem layer
dem=QgsMapLayerRegistry.instance().mapLayersByName("mydem")[0]

fileName = dem.source()
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)

# Need to define the extent of the region
extent = rlayer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

processing.runalg("grass:r.watershed", dem, None, None, None, None, 10, 0, 1, 300, False, True, False, False, "%f,%f,%f,%f"% (xmin, xmax, ymin, ymax), 40, "C:\\Users\\flc\\Desktop\\QGIS_Plugins\\layers\\acc.tif", "C:\\Users\\flc\\Desktop\\QGIS_Plugins\\layers\\dir.tif", None, None, None, None, None, None)

Running this algorithm from the Python Console contains boolean parameters which must be defined by True or False. You can check this by typing:

import processing
processing.alghelp("grass:r.watershed")

The output will tell you the parameters used:

ALGORITHM: r.watershed - Watershed basin analysis program.
    elevation <ParameterRaster>
    depression <ParameterRaster>
    flow <ParameterRaster>
    disturbed.land <ParameterRaster>
    blocking <ParameterRaster>
    threshold <ParameterNumber>
    max.slope.length <ParameterNumber>
    convergence <ParameterNumber>
    memory <ParameterNumber>
    -f <ParameterBoolean>
    -4 <ParameterBoolean>
    -m <ParameterBoolean>
    -a <ParameterBoolean>
    GRASS_REGION_PARAMETER <ParameterExtent>
    GRASS_REGION_CELLSIZE_PARAMETER <ParameterNumber>
    accumulation <OutputRaster>
    drainage <OutputRaster>
    basin <OutputRaster>
    stream <OutputRaster>
    half.basin <OutputRaster>
    visual <OutputRaster>
    length.slope <OutputRaster>
    slope.steepness <OutputRaster>

There's a couple of other things such as the the max.slope.length parameter which requires a minimum value of 1 and the GRASS_REGION_PARAMETER which needs to be defined.

6
  • Still I obtain empty rasters. It is like if the region of the mapset does not correspond with the extent of the input. But I supposed that in this implementation the algorithm does not work with the region. Do you have any suggestion? Feb 26, 2016 at 12:55
  • @francescolc - Apologies but I do not. I used this extent method on other GRASS functions which seemed to work fine but haven't really used r.watershed. Hopefully others can advise =)
    – Joseph
    Feb 26, 2016 at 13:04
  • In the OGR log messages I found "data source is not valid". So I tried again the GUI processing algorithm and during the execution I saw this inital task accomplished by the algorithm: "r.external -r input="C:\Users\flc\Desktop\Varie\dem_plastico\dem6.tif" band=1 output=tmp14564935220515 --overwrite -o". It is possible that GRASS tools in python-processing need to be converted? Feb 26, 2016 at 13:58
  • @francescolc - Don't think so as they're already available in python-processing and most tools work fine with producing an output(s). This is interesting, wonder where the problem lies...
    – Joseph
    Feb 26, 2016 at 14:21
  • 1
    I found that just formatting better the extent string it works. In particular I have deleted blank spaces between coordinate values. This is how it works: "%f,%f,%f,%f"% (xmin, xmax, ymin, ymax). I think this could be relevant for others. However the right answer is yours :) Thank you Mar 1, 2016 at 15:38

Your Answer

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

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