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

teste, None, None, None, None, None, 
"round(A)", size, '0', 
"D:/OneDrive/Doutoramento/GIS/Aveiro Coelho (2005)/reclassified maps/teste_arredondado.tif")
  • I understood that were the last two "None" that prevent it from the algorithm to work. The last should be '0'. However, the previous ask for the raster extent, and I'm having trouble in how to capture the extent from the previous layer, someone can help me? – narra89 Jan 9 '15 at 15:30

Examining the content of the file .../.qgis2/processing/processing.log after running the processing toolbox command gives you the result:

ALGORITHM|Fri Jan 09 2015   

And if you read Using processing algorithms from the console, you can get help with:

import processing
ALGORITHM: r.mapcalculator - Calculate new raster map from a r.mapcalc expression.
amap <parameters>
bmap <parameters>
cmap <parameters>
dmap <parameters>
emap <parameters>
fmap <parameters>
formula <parameters>
outfile <outputs>

In your formula, there is no GRASS_REGION_PARAMETER nor GRASS_REGION_CELLSIZE_PARAMETER, fundamental in GRASS GIS (GRASS GIS: r.mapcalc)

  • 1
    Thanks =) I just solved the problem now using the extent comand to produce a string for the region parameter. ext = teste.extent() a = str(ext.xMinimum()) b = str(ext.xMaximum()) c = str(ext.yMinimum()) d = str(ext.yMaximum()) size = a + ',' + b + ',' + c + ',' + d – narra89 Jan 9 '15 at 17:57

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.