5

Following up on Save and load the classes definition table in the QGIS reclassify by table tool, I decided to run the qgis.reclassifybytable algorithm using PyQGIS to reclassify an aspect raster.

Here's the code:

import numpy as np
import processing

l = np.hstack([np.arange(1,5,1),1])
t = np.hstack([np.arange(-90,360,90)[1:]])
t = np.repeat(t,2)
t = np.hstack([0,t,360])
L = list(np.insert(t,np.arange(2,len(t)+2,2),l))

input_layer = QgsProject.instance().mapLayersByName('Aspect')[0]
output_layer = '/tmp/reclassify_output.tif'
params = {
  'DATA_TYPE': 5,
  'INPUT_RASTER': input_layer,
  'NODATA_FOR_MISSING': False, 
  'NO_DATA': -9999,
  'OUTPUT': output_layer,
  'RANGE_BOUNDARIES': 2,
  'RASTER_BAND': 1,
  'TABLE': L # <---- I'd like to pass a list here!
}

alg_id = 'qgis:reclassifybytable'
processing.run(alg_id, params)

Which gives this traceback:

Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/usr/share/qgis/python/plugins/processing/tools/general.py", line 108, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
  File "/usr/share/qgis/python/plugins/processing/core/Processing.py", line 159, in runAlgorithm
    ret, results = execute(alg, parameters, context, feedback, catch_exceptions=False)
  File "/usr/share/qgis/python/plugins/processing/gui/AlgorithmExecutor.py", line 71, in execute
    results, ok = alg.run(parameters, context, feedback, {}, False)
_core.QgsProcessingException: Invalid output value:

I particularly do like the Invalid output value: with nothing after! And the OUTPUT value is actually perfectly valid.

It took me some time to actually figure out a way that works, but with which I'm not 100% satisfied; copy-pasting the value of the list L that is printed on the console when I simply barely hit "L" in the Python console to show the content of this variable and to naively put it where L is used in the params variable in my code:

>>> L
[0, 0, 1, 0, 90, 2, 90, 180, 3, 180, 270, 4, 270, 360, 1]

And this works.

So I thought that maybe, for some reason, the algorithm is waiting for a string at this position, so I also try to enter str(L) instead of simply L but then:

Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 24, in <module>
  File "/usr/share/qgis/python/plugins/processing/tools/general.py", line 108, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
  File "/usr/share/qgis/python/plugins/processing/core/Processing.py", line 159, in runAlgorithm
    ret, results = execute(alg, parameters, context, feedback, catch_exceptions=False)
  File "/usr/share/qgis/python/plugins/processing/gui/AlgorithmExecutor.py", line 71, in execute
    results, ok = alg.run(parameters, context, feedback, {}, False)
_core.QgsProcessingException: Invalid value for minimum: [0.0

Sounds very strange. It looks like it stops after reading the first value of the list.

Does anybody know what's going on here and how could I programmatically pass my list to the function params instead of entering it manually?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.