<div dir="ltr">I didn't used too much this feature, I usually delegate to the results of the algorithm if to load or not.<div>BTW in my snippets (all commented so not used in my scripts) I set last LayerDetails parameter as the key in result dictionary =></div><div><br></div><div>in your case should be:</div><div><span style="font-family:"Courier New"">context.</span><span style="font-family:"Courier New"">addLayerToLoadOnCompletion(</span><a href="http://rlayer.id/" target="_blank" style="font-family:"Courier New"">rlayer.id</a><span style="font-family:"Courier New"">(), QgsProcessingContext.</span><span style="font-family:"Courier New"">LayerDetails('image_layer', QgsProject.instance(), </span>self.OUTPUT_raster<span style="font-family:"Courier New"">))</span></div><div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><br></div><div>regards</div><div><br></div><div>Luigi Pirelli<br><br>**************************************************************************************************<br>* LinkedIn: <a href="https://www.linkedin.com/in/luigipirelli" target="_blank">https://www.linkedin.com/in/luigipirelli</a><br>* Stackexchange: <a href="http://gis.stackexchange.com/users/19667/luigi-pirelli" target="_blank">http://gis.stackexchange.com/users/19667/luigi-pirelli</a><br>* GitHub: <a href="https://github.com/luipir" target="_blank">https://github.com/luipir</a><br>* Book: <a href="https://www.packtpub.com/eu/application-development/mastering-geospatial-development-qgis-3x-third-edition" target="_blank">Mastering QGIS3 - 3rd Edition</a></div><div>* Hire a team: <a href="http://www.qcooperative.net" target="_blank">http://www.qcooperative.net</a><br>**************************************************************************************************</div></div></div></div></div></div></div></div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 16 Jun 2021 at 04:05, Bourdon, Jean-François (DIF) <<a href="mailto:Jean-Francois.Bourdon@mffp.gouv.qc.ca">Jean-Francois.Bourdon@mffp.gouv.qc.ca</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="FR-CA">
<img alt="Intranet logo">
<div>
<div class="gmail-m_-1257981077243391665WordSection1">
<p class="MsoNormal">Hi Devs,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></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<u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">def flags(self):<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    return super().flags() | QgsProcessingAlgorithm.FlagNoThreading<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></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" target="_blank">
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>
<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">class addLayerToCanvas(QgsProcessingAlgorithm):<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    OUTPUT_raster = 'OUTPUT_raster'<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    def initAlgorithm(self, config):<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        self.addOutput(QgsProcessingOutputMultipleLayers(self.OUTPUT_raster,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">                                                         <a href="http://self.tr" target="_blank">self.tr</a>('Output raster')))<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">    def processAlgorithm(self, parameters, context, feedback):<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        <u></u><u></u></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<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        context.addLayerToLoadOnCompletion(<a href="http://rlayer.id" target="_blank">rlayer.id</a>(), QgsProcessingContext.LayerDetails('image_layer', QgsProject.instance(), ''))<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        return {self.OUTPUT_raster: <a href="http://rlayer.id" target="_blank">rlayer.id</a>()}<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks for any help<u></u><u></u></p>
<p class="MsoNormal"><span style="color:black"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:12pt;font-family:Arial,sans-serif;color:black">Jean-François Bourdon, ing.f.</span></b><span style="font-size:9pt;font-family:Arial,sans-serif;color:black"><u></u><u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:Arial,sans-serif;color:rgb(46,116,181)">Analyste en télédétection</span></b><span style="font-size:10pt;font-family:Arial,sans-serif"><br>
<b><span style="color:rgb(46,116,181)">Direction des inventaires forestiers</span></b><br>
<b><span style="color:rgb(46,116,181)">Ministère des Forêts, de la Faune et des Parcs</span></b><br>
<span style="color:rgb(46,116,181)">5700, 4<sup>e</sup> Avenue Ouest, local A-108</span><br>
<span style="color:rgb(46,116,181)">Québec (Québec) G1H 6R1</span><br>
<span style="color:rgb(46,116,181)">Téléphone : <s>418 627-8669, poste 704304</s></span><br>
<b><u><span style="color:rgb(46,116,181)"><a href="mailto:jean-francois.bourdon@mffp.gouv.qc.ca" target="_blank">jean-francois.bourdon@mffp.gouv.qc.ca</a></span></u></b>
<br>
</span><span><a href="http://www.mffp.gouv.qc.ca/accueil.jsp" target="_blank"><b><span style="font-size:10pt;font-family:Arial,sans-serif;color:rgb(46,116,181)">mffp.gouv.qc.ca</span></b></a></span><span><u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>

_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>