2

I am using QGIS, and I would like to write a script to help processing the data.

To do so, I have to reclassify heights values from a raster. I have written the following code, where retab is the path to the file containing the reclassification rules (see later) :

##Layer1=raster


from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
import processing

retab = "E:\\Divers\\Qgis\\Scripts\\tab_reclass.txt"
#processing.runalg('saga:reclassifygridvalues', input, method, old, new, soperator, min, max, rnew, roperator, retab, toperator, nodataopt, nodata, otheropt, others, result)

Layerout=processing.runalg('saga:reclassifygridvalues', Layer1, 2, 0, 0, 0, 0, 0, 0, retab, 0, 0, 0, 0, 0, 0, 0)

The code gives the following error :

Error: Wrong parameter value: E:\Divers\Qgis\Scripts\tab_reclass.txt

My rules are the following :

minimum maximum new
0   0.5 1
0.5 2   2
2   1000    3

I could not find a clear example on how to use the table rules, so I guess that it is where the error comes from.

If possible, I would prefer using the rules from the script rather than from the external file.

7
  • Is your rules file tab-delimited?
    – Kazuhito
    Jun 7 '17 at 20:18
  • @Kazuhito yes it is
    – Kantan
    Jun 8 '17 at 6:56
  • Mmm, then it should work. Could be an overkill, but how about preparing .mtab file? It is an associated file created by SAGA GUI Lookup Table | Save. Not sure if it helps, though.
    – Kazuhito
    Jun 8 '17 at 7:35
  • I don't get it, what's the use of that file? how would you use that in a Qgis environment?
    – Kantan
    Jun 8 '17 at 8:31
  • I understand your suspicion. It is a definition (xml) file SAGA uses to read table, but I cannot say it works for your case.
    – Kazuhito
    Jun 8 '17 at 9:24
2
+50

The following should hopefully work:

##Layer1=raster

Layerout=processing.runalg('saga:reclassifygridvalues', Layer1, 2, 0, 0, 0, 0, 0, 0, 0, '0,0.5,1,0.5,2,2,2,1000,3', 0, 0, 0, 0, 0, None)

Notes:

  • No output is produced as I'm assuming you're going to process it further (but you can replace runalg with runandload to see what the output looks like.

  • You can avoid using a lookup table by typing the 'minimum,maximum,new' values in a string. So if you have 3 rows, you would type it continuously (e.g. 'minimum,maximum,new,minimum,maximum,new,minimum,maximum,new'.

2
  • 2
    Sorry for the delay, but good news, it works! Thanks for the notes too! For my application, I needed to get rid of the other values too, so here is my final command : Layerout=processing.runandload('saga:reclassifygridvalues', Layer1, 2, 0, 0, 0, 0, 0, 0, 0, '0,0.5,1,0.5,2,2,2,1000,3', 0, 1, -99999, 1, -99999, none)
    – Kantan
    Jun 14 '17 at 15:42
  • @Kantan - Most welcome and nice one! Glad you got it to work :)
    – Joseph
    Jun 15 '17 at 9:13

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.