Thursday 15 August 2019

qgis - Using r.mapcalculator in Python console?


I'm trying to use the r.mapcalculator algorithim in a python script, but I'm having problems in using the r.mapcalculator to tranform a raster layer with float values into integers.


Using the processing toolbox of QGIS, the algorithm works flawlessly. However, using the python code below, the values don't get rounded, instead, I only get NaN values.


processing.runalg("grass:r.mapcalculator","D:/OneDrive/reclassified maps/teste.tif", None, None, None, None, None, "round(A)", None, None, "D:/OneDrive/reclassified maps/teste_arredondado.tif")

EDIT: Just solved the problem. The last None parameters cannot be "None". So, for the GRASS_REGION_PARAMETER I used the extent() comand to create a string.


ext = teste.extent()
a = str(ext.xMinimum())

b = str(ext.xMaximum())
c = str(ext.yMinimum())
d = str(ext.yMaximum())
size = a + ',' + b + ',' + c + ',' + d

The last parameter (GRASS_REGION_CELLSIZE_PARAMETER) just input 0. The final form is


processing.runalg("grass:r.mapcalculator", 
teste, None, None, None, None, None,
"round(A)", size, '0',
"D:/OneDrive/Doutoramento/GIS/Aveiro Coelho (2005)/reclassified maps/teste_arredondado.tif")



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...