2

I would like to produce a new temporary raster after a calculation with the SAGA raster calculator using a custom python algorithm script. This is my code so far:

##MaechtigkeitunterhalbeinerHoehe=name
##Input_Raster=raster
##Hoehe=string 28.65
##Rasterout=output raster
##KoordS=crs

outputs_SAGARASTERCALCULATOR_1=processing.runalg('saga:rastercalculator', Input_Raster,[],'a - Hoehe',0,False,7,Rasterout)

It produces the error in the title. The log shows this:

Uncaught error while executing algorithm Traceback (most recent call last): File "C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python/plugins\processing\core\GeoAlgorithm.py", line 205, in execute self.convertUnsupportedFormats(progress) File "C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python/plugins\processing\core\GeoAlgorithm.py", line 296, in convertUnsupportedFormats crsid = layer.crs().authid() AttributeError: 'NoneType' object has no attribute 'crs'

2019-02-08T14:12:03 1 There were errors executing the algorithm. 2019-02-08T14:12:39 2 Error loading result layer: Traceback (most recent call last): File "C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python/plugins\processing\gui\Postprocessing.py", line 77, in handleAlgorithmResults isRaster) File "C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python/plugins\processing\tools\dataobjects.py", line 186, in load + '\nCheck the processing framework log to look for errors') RuntimeError: Could not load layer: C:\Users\saku\AppData\Local\Temp\processing2bd0c9ce76084b92b2e46ef30e6e930d\e5a1e836014f4fafb330f42ca6e8da80\Rasterout.tif Check the processing framework log to look for errors

I have checked other threads kind of regarding this topic and found out that this error usually means that SAGA produces an emtpy file, in part because of spaces in the file name or path. I checked my error log and noch such things occured. An interesting fact is when using the SAGA Raster Calculator from the Toolbox and using the same parameters (except Hoehe which would simply be a number) and it create a temporary tif/raster layer no problem.

How can I solve this error? Why is it working when solely using the tool from the toolbox, but not when implemented in a script?

Even when trying to save the Rasterout.tif to a different directory I get this error. This is also not what I want by the way. I need to use the temporary raster as an input for another calculation and only want to output/write and load the finished final raster layer.

I hope I described my problem clear enough.

1

The problem is the formula or let's say how you build it. try to build it like this:

formula = 'a-{}'.format(Hoehe)
outputs_SAGARASTERCALCULATOR_1=processing.runalg('saga:rastercalculator', Input_Raster,[],formula,0,False,7,Rasterout)
  • Thank you so much for your reply and the formula edit. It worked perfectly. Could you please clarify on why it worked? what does 'a-{}'.format(Hoehe) do exactly? Because I will need other formulas later do use on the Rasterout (e.g. Rasterout*(Rasterout>0)) – S. Kuske Feb 11 at 9:37
  • Ok, I will try to explain it: Hoehe is one of your input parameters and it is stored in the variable Hoehe. Your formula was: 'a - Hoehe' in this case it will be a string because hoehe is inside the string and regarded as the word 'hoehe and not as the variable. with the python construct: 'a-{}'.format(Hoehe) the value from the variable will be pasted into the string, something like: 'a-28.65', see pyformat.info for more infos on that. Please don't forget to accept my answer if it was the solution. Viel Glück und viele Grüße, Stefan – eurojam Feb 11 at 9:48
  • Thanks for the explanation. I had found out what it does in the meanwhile by googling ;P, but I think for anyone reading this it might be clearer now. Alles Gute, SK – S. Kuske Feb 11 at 10:57
  • bitte noch die Lösung akzeptieren, danke – eurojam Feb 11 at 12:47
  • Habe ich nachgeholt. Sorry. Könntest du vllt bei meinem neusten Problem auch mal drüber schauen, oder darf ich das nicht fragen? :) gis.stackexchange.com/questions/311738/… – S. Kuske Feb 12 at 13:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.