4

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.

4
  • It's very strange. Did you have the same thing with processing.run instead of Processing.runAlgorithm ?
    – lejedi76
    Commented 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. Commented Jan 21, 2021 at 15:29
  • 1
    Try to force with QgsProcessingUtils.generateTempFilename('example.gpkg') instead of QgsProcessing.TEMPORARY_OUTPUT
    – lejedi76
    Commented Jan 21, 2021 at 15:56
  • It works. At least for now :) Thank you. If you want answer my question and I will vote. Commented Jan 22, 2021 at 17:33

2 Answers 2

1

Strange issue. I don't understand the why or how but consider to replace QgsProcessing.TEMPORARY_OUTPUT by :

QgsProcessingUtils.generateTempFilename('example.gpkg')
1

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.

Your Answer

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

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