2

I want to run the r.reclass tool in python on a raster layer to reclassify 1 value (and leave the rest the way they are).

Here is my best attempt so far, after trying to reference documentation and other examples. Here is my code:

import processing

input_raster = QgsRasterLayer('/Users/ep9k/Desktop/Key-LogEcovaluator/Rasters/AestheticMax.tif', 'raster')
output_raster = '/Users/ep9k/Desktop/OutputReclass.tif'
rules_file = '/Users/ep9k/Desktop/reclassrules.txt'


parameters = {'Input' : input_raster,
            'rules' : rules_file,
            'Output' : output_raster,
            'GRASS_REGION_PARAMETER' : '1277290, 1314314, -14004, 11845',   #this is the exent of my raster layer
            'GRASS_REGION_CELLSIZE_PARAMETER' : 0
            }

processing.runAndLoadResults('grass7:r.reclass', parameters)

I run the script and I don't get any errors, but nothing happens. No output, nothing is reclassified. I think I am missing something obvious here.

Here is how I define my reclassification rules. As far as I can tell from the grass 7 documentation this is correct:

35 = 99

Really, I am reclassifying one value (35 = 99).

I get the help docs by running the following in the console:

processing.algorithmHelp("grass7:r.reclass")

Two of the input parameters are confusing to me. Specifically GRASS_RASTER_FORMAT_OPT and GRASS_RASTER_FORMAT_META. Are these required?

  • 1
    It looks like you're using the older 2.x API under QGIS 3? – ndawson Feb 23 at 6:07
  • How did you define your rules in your rules.txt file? – Joseph Feb 25 at 10:02
  • @Joseph sorry for the slow response. I edited my question with my rules defined. – Erich Purpur Feb 28 at 16:36
1

The raster type likely cannot hold negative values.

Run gdalinfo on AestheticMax.tif and you will likely find it is 8-bit integer.

So the answer is to convert the input raster to signed integer and then run it.

The raster type likely cannot hold negative values.

Run gdalinfo on AestheticMax.tif and you will likely find it is 8-bit integer.

From your link

In fact, the r.reclass program does not generate any new raster map layers (in the interests of disk space conservation). Instead, a reclass table is stored which will be used to reclassify the original raster map layer each time the new (reclassed) map name is requested. As far as the user (and programmer) is concerned, that raster map has been created.

  • I changed -1 to 1000000. After re-reading the documentation I am even more confused. The documentation also says: "r.reclass creates an output map layer". If r.reclass creates reclass table, where is it? How do I use it? Could you provide an example? – Erich Purpur Mar 1 at 13:59
  • well that would not be valid either, if you want to trouble shoot just run your code but with one change for me 35 = 99 and nothing else. No other items in the reclass text aside from 35 = 99 – If you do not know- just GIS Mar 1 at 17:19
  • Why is this not valid? 1000000 is a signed integer. Ok, I did what you suggested. My 'reclass.txt' file is now just "35 = 99". I get the same result. The script runs, there is no output. I am expecting an output to my 'rules_file' variable, which is a path to where my output is saving. – Erich Purpur Mar 1 at 20:59
  • @ErichPurpur Because it is over 255! – If you do not know- just GIS Mar 1 at 21:07
  • @ErichPurpur so not output file at all? If so do the 35 =99 thing again but put the input raster and the output path in C:\temp. – If you do not know- just GIS Mar 1 at 21:17

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.