TransWikia.com

QGIS processing algorithm - add saved geopackage layer to map

Geographic Information Systems Asked by SHeld on May 30, 2021

I am still new to Qgis and Python and most likely this is really simple but however I do not manage to do the following:

I am trying to write a python processing algorithm for the processing toolbox. It uses various native processing algorithms (like buffer) and these produce several output layers that I want to save to a single geopackage to make the results more organized. Because of this I can not use standard ProcessingParameterVectorDestination, because then you can only save one layer per gpkg.
The Layers are successfully saved to my geopackage as shown below, but I do not manage to display them in the map directly from my processing toolbox script.

Steps:

  • let my users choose a geopackage to save all the produced layers
       self.addParameter(
            QgsProcessingParameterFileDestination(
                self.OUTPUTFILE,
                self.tr('Output Geopackage'),
                'Geopackage (*.gpkg)'
            )
        )

and

destination = self.parameterAsFileOutput(parameters, self.OUTPUTFILE, context)
  • run several processing scripts and use processing.runAndLoadResults() to add the layers like in this Question
    Example:
        zaun = processing.runAndLoadResults('native:buffer',
            {
                'INPUT': parameters[self.INPUT],
                'DISTANCE': zaunabstand,
                'SEGMENTS': 5,
                'DISSOLVE': False,
                'END_CAP_STYLE': 1,
                'JOIN_STYLE': 1,
                'MITER_LIMIT': 2,
                'OUTPUT': 'ogr:dbname=''+ destination +'' table="zaun" (geom)' 
            },
            context=context,
            feedback=feedback)['OUTPUT']
  • When I use the python console to check if the layers are there, it seems like it worked but I cant see them on the map:
layers = QgsProject.instance().mapLayers()
print(layers)
{'OSM_Standard_efcb35f0_1b35_4f6c_ac58_14682871cc31': <QgsRasterLayer: 'OSM Standard' (wms)>, 'Umring_46c1f9a4_f251_44f1_a1d6_cad5aa3ad847': <QgsVectorLayer: 'Umring' (ogr)>, 'test_a7d3776d_c9f7_44ef_ae2b_26422643c173': <QgsVectorLayer: 'zaun' (ogr)>, 'test_e7c8812e_eb24_4497_a942_1cd185c82306': <QgsVectorLayer: 'tische' (ogr)>}

Most likely this is really trivial but how can I show the layers in the TOC and on the map programmatically from my toolbox script?

I also tried to add them with QgsProject.instance().addMapLayer() but this seems not to work out of a processing algorithm.

I wonder if it is possible to use QgsProcessingOutputVectorLayer to output the result of my buffer but I have not idea how.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP