Wednesday 16 October 2019

Error running Grass module in Qgis python console


I am working under Qgis 2.16.0 and i am trying to make a script using the grass module r.water.oulet. This module has 5 parameters :


1 - Name of input raster 2 - coordinates of outlet point (x,y) 3 - area of the Grass region (xmin, xmax, ymin, ymax) 4 - Celle size of Grass Region 5- Output file


When i use the module in Qgis I don't need to set the area of Grass region. But if d'ont set it in python command :


processing.runalg('grass:r.water.outlet', "D:\Developpement\Debits_cartes_alea\Donnees_test\flow_direction.tif",936400.018,6432568.853,None,0.0,"D:\Developpement\Debits_cartes_alea\Donnees_test\1sortie.tif")

I have this error message : "Error: Wrong parameter value: None"



So I try to set the required coordinates like this :


processing.runalg('grass:r.water.outlet', "D:\Developpement\Debits_cartes_alea\Donnees_test\flow_direction.tif",936400.018,6432568.853,['933963.950608,940746.141038,6431270.66542,6434790.6058'],0.0,"D:\Developpement\Debits_cartes_alea\Donnees_test\1sortie.tif")

Then I have this error message : "Error: Wrong parameter value: ['933963.950608,940746.141038,6431270.66542,6434790.6058']"


Does that come from the syntax or is ther the problem with the values (note that I try with the same values in module and it's work !)



Answer



Usually when I run processes, I define the extent instead of leaving it blank (which often results in errors like you have shown). So you could use something like the following:


# Define input and output paths
path = "D:/Developpement/Debits_cartes_alea/Donnees_test/flow_direction.tif"
output = "D:/Developpement/Debits_cartes_alea/Donnees_test/1sortie.tif"


# Get raster data
fileInfo = QFileInfo(path)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)

# Define the minimum extent of the region
extent = rlayer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()

ymin = extent.yMinimum()
ymax = extent.yMaximum()

import processing
processing.runalg('grass:r.water.outlet', path,
936400.018,6432568.853,
"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),
0.0,
output)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...