I am developing a plugin in QGIS using Python language. This plugin uses algorithms from Processing Toolbox and I save some results in the temporary folder assigning QgsProcessing.TEMPORARY_OUTPUT
in the output field of each algorithm. However, my plugin is just working well when I return the temporary folder directory using QMessageBox
. If I do not use this, my plugin does not work, so nothing is saved in the temporary folder. Why this happens?
Example of code working:
clip_ndvi = Processing.runAlgorithm("saga:cliprasterwithpolygon", {
'INPUT': str(self.dlg.lineEdit.text()),
'POLYGONS': vl,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT})
out_ndvi = clip_ndvi['OUTPUT']
QMessageBox.about(self.dlg,'teste', str(out_ndvi))
Example of code not working:
clip_ndvi = Processing.runAlgorithm("saga:cliprasterwithpolygon", {
'INPUT': str(self.dlg.lineEdit.text()),
'POLYGONS': vl,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT})
out_ndvi = clip_ndvi['OUTPUT']
The difference is in the QMessageBox
.
-
It's very strange. Did you have the same thing with processing.run instead of Processing.runAlgorithm ?lejedi76– lejedi762021年01月20日 13:17:37 +00:00Commented Jan 20, 2021 at 13:17
-
The processing.run is only used in the Python console in QGIS and that is ok. But if I need to use in a Python script I need to use Processing.runAlgorithm.... I think that the problem is in the output be saved in temporary folder. Because sometimes it does not create the output raster... But I do not understand why. And also if there is another way to save in temporary folder.Bárbara Duarte– Bárbara Duarte2021年01月21日 15:29:24 +00:00Commented Jan 21, 2021 at 15:29
-
1Try to force with QgsProcessingUtils.generateTempFilename('example.gpkg') instead of QgsProcessing.TEMPORARY_OUTPUTlejedi76– lejedi762021年01月21日 15:56:33 +00:00Commented Jan 21, 2021 at 15:56
-
It works. At least for now :) Thank you. If you want answer my question and I will vote.Bárbara Duarte– Bárbara Duarte2021年01月22日 17:33:47 +00:00Commented Jan 22, 2021 at 17:33
2 Answers 2
Strange issue. I don't understand the why or how but consider to replace QgsProcessing.TEMPORARY_OUTPUT
by :
QgsProcessingUtils.generateTempFilename('example.gpkg')
If you run the saga:cliprasterwithpolygon algorithm in the QGIS GUI, and try to set a temporary output, the processing tool will fail.
This is because both GRASS and SAGA processing tools don't seem to work with temporary outputs (despite their GUI's suggesting that they will accept a temporary output).
Needless to say, once you set the output in both your code or in the GUI, the algorithm will work.