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 Jan 20 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 Jan 21 at 15:29
  • 1
    Try to force with QgsProcessingUtils.generateTempFilename('example.gpkg') instead of QgsProcessing.TEMPORARY_OUTPUT – lejedi76 Jan 21 at 15:56
  • It works. At least for now :) Thank you. If you want answer my question and I will vote. – Bárbara Duarte Jan 22 at 17:33
0

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

QgsProcessingUtils.generateTempFilename('example.gpkg')

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.