<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
.MsoChpDefault
        {mso-style-type:export-only;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="FR-CA" link="#0563C1" vlink="#954F72">
<img alt="Intranet logo" src="https://mffp.gouv.qc.ca/documents/ministere/piv/mffp.png">
<div>
<div class="WordSection1">
<p class="MsoNormal">Hi Devs,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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 <span style="font-family:"Courier New"">
context.project().addMapLayer(rlayer, True)</span> 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<o:p></o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">def flags(self):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    return super().flags() | QgsProcessingAlgorithm.FlagNoThreading<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I saw this answer from Nyall on <a href="https://gis.stackexchange.com/questions/280520/let-qgis-3-0-processing-algorithm-output-a-vectorlayer-loaded-via-the-delimited">
SO</a> 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:
<i>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.</i>
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">class addLayerToCanvas(QgsProcessingAlgorithm):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    OUTPUT_raster = 'OUTPUT_raster'<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    def initAlgorithm(self, config):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        self.addOutput(QgsProcessingOutputMultipleLayers(self.OUTPUT_raster,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">                                                         self.tr('Output raster')))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    def processAlgorithm(self, parameters, context, feedback):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        rlayer = QgsRasterLayer("F:/image.tif", "image") # The layer I want to add to the current project/canvas<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        context.addLayerToLoadOnCompletion(rlayer.id(), QgsProcessingContext.LayerDetails('image_layer', QgsProject.instance(), ''))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        return {self.OUTPUT_raster: rlayer.id()}<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks for any help<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto"><span style="color:black;mso-fareast-language:FR-CA"><o:p> </o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto"><b><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black;mso-fareast-language:FR-CA">Jean-François Bourdon, ing.f.</span></b><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black;mso-fareast-language:FR-CA"><o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#2E74B5;mso-fareast-language:FR-CA">Analyste en télédétection</span></b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;mso-fareast-language:FR-CA"><br>
<b><span style="color:#2E74B5">Direction des inventaires forestiers</span></b><br>
<b><span style="color:#2E74B5">Ministère des Forêts, de la Faune et des Parcs</span></b><br>
<span style="color:#2E74B5">5700, 4<sup>e</sup> Avenue Ouest, local A-108</span><br>
<span style="color:#2E74B5">Québec (Québec) G1H 6R1</span><br>
<span style="color:#2E74B5">Téléphone : <s>418 627-8669, poste 704304</s></span><br>
<b><u><span style="color:#2E74B5">jean-francois.bourdon@mffp.gouv.qc.ca</span></u></b>
<br>
</span><span style="mso-fareast-language:FR-CA"><a href="http://www.mffp.gouv.qc.ca/accueil.jsp"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#2E74B5">mffp.gouv.qc.ca</span></b></a></span><span style="mso-fareast-language:FR-CA"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body>
</html>