[QGIS-Developer] How to use addLayerToLoadOnCompletion() ?
Bourdon, Jean-François (DIF)
Jean-Francois.Bourdon at mffp.gouv.qc.ca
Tue Jun 15 19:04:23 PDT 2021
[Intranet logo]
Hi Devs,
I'm trying to load a raster/vector layer to the current project at the end of the run of a processing algorithm script. I know about QgsProcessingParameterFeatureSink() and I use it when creating a new layer during processing, but how to add an existing layer that will be shown on the map? I tried using context.project().addMapLayer(rlayer, True) but it obviously doesn't work because the process is running in a separate thread. Speaking of thread, I would prefer avoiding running the script in the main thread with
def flags(self):
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
I saw this answer from Nyall on SO<https://gis.stackexchange.com/questions/280520/let-qgis-3-0-processing-algorithm-output-a-vectorlayer-loaded-via-the-delimited> for something similar but I haven't been able to emulate. Below is a short example of what I last tried and which gives me the following error: The following layers were not correctly generated. * image_73e75830_4808_4993_abc7_24a5ee61217e You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.
class addLayerToCanvas(QgsProcessingAlgorithm):
OUTPUT_raster = 'OUTPUT_raster'
def initAlgorithm(self, config):
self.addOutput(QgsProcessingOutputMultipleLayers(self.OUTPUT_raster,
self.tr('Output raster')))
def processAlgorithm(self, parameters, context, feedback):
rlayer = QgsRasterLayer("F:/image.tif", "image") # The layer I want to add to the current project/canvas
context.addLayerToLoadOnCompletion(rlayer.id(), QgsProcessingContext.LayerDetails('image_layer', QgsProject.instance(), ''))
return {self.OUTPUT_raster: rlayer.id()}
Thanks for any help
Jean-François Bourdon, ing.f.
Analyste en télédétection
Direction des inventaires forestiers
Ministère des Forêts, de la Faune et des Parcs
5700, 4e Avenue Ouest, local A-108
Québec (Québec) G1H 6R1
Téléphone : 418 627-8669, poste 704304
jean-francois.bourdon at mffp.gouv.qc.ca
mffp.gouv.qc.ca<http://www.mffp.gouv.qc.ca/accueil.jsp>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210616/4d8f05fb/attachment.html>
More information about the QGIS-Developer
mailing list