QGIS – Fix Custom Process Script Crash When Output Set to Temporary File

pyqgisqgisqgis-processing

I'm trying to convert a pretty simple pyqgis script to a proccessing script for use in a model I'm creating. The script works as intended when setting the output in the process UI to a file on my computer. But when selecting the temporary file option (leaving the field blank), QGIS crashes when trying to create the output. This is creating a problem since the model is made more complicated if I have to save temporary files manually in each step.

The following is the code most likely causing the crash:

    def initAlgorithm(self, config=None):
        
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.DIFFERENCE,
                self.tr('Difference'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )
        self.addParameter(
            QgsProcessingParameterVectorDestination(
                self.OUTPUT,
                self.tr('Output layer')
            )
        )
    
    def processAlgorithm(self, parameters, context, feedback):
        
        distance = QgsDistanceArea()
        crs = QgsCoordinateReferenceSystem()
        crs.createFromSrsId(3006)
        distance.setSourceCrs(crs, QgsCoordinateTransformContext())
        distance.setEllipsoid('SWEREF99 TM')

        inputLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
        diffLayer = self.parameterAsVectorLayer(parameters, self.DIFFERENCE, context)
        
        layerPoly = QgsVectorLayer('Polygon?crs=epsg:3006', 'polygonsFiltered', 'memory')
        prov = layerPoly.dataProvider()
        prov.addAttributes([QgsField('objekt_id', QVariant.String)])
        layerPoly.startEditing()
        for poly in inputLayer.getFeatures():
            if distance.measureArea(poly.geometry()) < 100000:
                feat = QgsFeature()
                feat.setGeometry(poly.geometry())
                feat.setAttributes([poly['objekt_id']])
                prov.addFeature(feat)
        layerPoly.updateExtents()
        layerPoly.commitChanges()
    
        output_layer = processing.run("native:difference",
            {
                'INPUT': layerPoly,
                'OVERLAY': diffLayer,
                'OUTPUT': parameters[self.OUTPUT]
            },
            context=context,
            feedback=feedback
        )["OUTPUT"]
        
        return {self.OUTPUT: output_layer}

I tried to include only relevant parts of the code. I've messed around a lot with the parameters trying to get this to work but it either crashes or outputs nothing when trying to output to a temporary file.

I have tried changing the temporary file directory in QGIS processing settings to one with a very simple filepath (E:\GeoDATA\OUTPUT), which didn't work.
I have tried setting 'OUTPUT' to both 'memory' and 'TEMPORARY_OUTPUT'.

Only thing I haven't tried is reinstalling QGIS.

Best Answer

Try adding is_child_algorithm=True as the last argument to your processing.run() call (after feedback=feedback). E.g. processing.run(... context=context, feedback=feedback, is_child_algorithm=True)

– Ben W

Solved my issue. It now works

Related Question