4

I'm working on a Processing Tool in PyQGIS and am struggling with the problem described in the title. Layers are not visible in the "Layers Panel" or on the canvas but can be chosen and worked with in tools like Buffer or Dissolve.

I have 2 variations that produce the same result. One looks like this:

        poolLayer = processing.run('native:dissolve', {
            'INPUT': outputs['ReprojectPoolkarte']['OUTPUT'],
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }, context=context, feedback=feedback)['OUTPUT']
        QgsProject.instance().addMapLayer(poolLayer)

The other case looks like this:

        alg_params = {
            'FIELD_LENGTH': 10,
            'FIELD_NAME': 'Flaeche_Pool',
            'FIELD_PRECISION': 2,
            'FIELD_TYPE': 0,
            'FORMULA': 'Flaeche_A/poolFl',
            'INPUT': outputs['AttributeNachFeldwert']['OUTPUT'],
            'NEW_FIELD': True,
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['Flaeche_PoolFieldCalculator'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['ErgebnisNeanteileFinal'] = outputs['Flaeche_PoolFieldCalculator']['OUTPUT']
        
        layer = context.takeResultLayer(outputs['AttributeNachFeldwert']['OUTPUT'])     
        transform_context = QgsCoordinateTransformContext()
        save_options = QgsVectorFileWriter.SaveVectorOptions()
        save_options.driverName = "CSV"
        save_options.overrideGeometryType = QgsWkbTypes.Point
        save_options.fileEncoding = "System"
        save_options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile
        QgsVectorFileWriter.writeAsVectorFormatV2(layer, r'c:\temp\output.csv', transform_context, save_options)
        
        return results

This actually works fine until I add the part of the code for exporting the layer as a CSV-File as well. In both case the resulting layers are fine but I have to make them "visible" by manually running them through a buffer with 0m or something similar.

0

2 Answers 2

7

First, check if layer is an instance of QgsVectorLayer.

layer = ...
print(type(layer))

If so, then, you have to add the following method to your processing algorithm class to be able to add temporary layers to the "Layers Panel".

def flags(self):
    return super().flags() | QgsProcessingAlgorithm.FlagNoThreading

Then use this::

layer = ...
QgsProject.instance().addMapLayer(layer)
0
1

Try:

QgsProject.instance().addMapLayer(layer, False)
QgsProject.instance().layerTreeRoot()(0, layer)

The first command registers the layer. The second adds it to the layer panel. I am not entirely sure why this works, but I always do it..

7
  • now I get this Error: Traceback (most recent call last): File "<string>", line 63, in processAlgorithm TypeError: 'QgsLayerTree' object is not callable
    – wubbe
    May 3, 2021 at 13:22
  • Did you type LayerTree or LayerTreeRoot?
    – wingnut
    May 3, 2021 at 13:54
  • I copied your code exactly only changed the name of the layer.
    – wubbe
    May 3, 2021 at 14:05
  • QgsProject.instance().addMapLayer() returns None if it didn't work. Maybe test your original code for that.
    – wingnut
    May 3, 2021 at 14:17
  • thing is addMapLayer() must have worked, the layer is definitly there, I can Buffer it with 0m and it will show up perfectly finde. it just doesn't show in the Layer Panel before that.... very confusing to me.
    – wubbe
    May 3, 2021 at 14:20

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.