<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;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        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="blue" vlink="purple">
<img alt="Intranet logo" src="https://mffp.gouv.qc.ca/documents/ministere/piv/mffp.png">
<div>
<div class="WordSection1">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Thanks. The solution came from Nyall, I only needed to add   </span><span style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:EN-US">context.temporaryLayerStore().addMapLayer(rlayer)</span><span style="font-size:10.0pt;mso-fareast-language:EN-US">
   </span><span style="mso-fareast-language:EN-US">in order for the layer to not diseapear at the end of processing script.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">However, how can I chose de name displayed for the layer? I tought that the name defined at    </span><span style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:EN-US">rlayer =
 QgsRasterLayer("F:/image.tif", "MyImage")</span><span style="mso-fareast-language:EN-US">   would be taken but it's not the case, it's rather the filename (without extension) that is displayed.<o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto"><span style="color:black"><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">Jean-François Bourdon, ing.f.</span></b><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black"><o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#2E74B5">Analyste en télédétection</span></b><span style="font-size:10.0pt;font-family:"Arial",sans-serif"><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><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><o:p></o:p></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="FR">De :</span></b><span lang="FR"> Luigi Pirelli <luipir@gmail.com>
<br>
<b>Envoyé :</b> 16 juin 2021 04:40<br>
<b>À :</b> Bourdon, Jean-François (DIF) <Jean-Francois.Bourdon@mffp.gouv.qc.ca><br>
<b>Cc :</b> qgis-developer <qgis-developer@lists.osgeo.org><br>
<b>Objet :</b> Re: [QGIS-Developer] How to use addLayerToLoadOnCompletion() ?<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">I didn't used too much this feature, I usually delegate to the results of the algorithm if to load or not.<o:p></o:p></p>
<div>
<p class="MsoNormal">BTW in my snippets (all commented so not used in my scripts) I set last LayerDetails parameter as the key in result dictionary =><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">in your case should be:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">context.addLayerToLoadOnCompletion(</span><a href="https://can01.safelinks.protection.outlook.com/?url=http%3A%2F%2Frlayer.id%2F&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415294608%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=C31QUv5m8jgdtrXRs8N7%2FG8pk%2BGH3gY5t6OEar%2FEISQ%3D&reserved=0" target="_blank"><span style="font-family:"Courier New"">rlayer.id</span></a><span style="font-family:"Courier New"">(),
 QgsProcessingContext.LayerDetails('image_layer', QgsProject.instance(), </span>self.OUTPUT_raster<span style="font-family:"Courier New"">))</span><o:p></o:p></p>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">regards<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Luigi Pirelli<br>
<br>
**************************************************************************************************<br>
* LinkedIn: <a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.linkedin.com%2Fin%2Fluigipirelli&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415304608%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=%2BrIMAvpPIcfPpSu9yaZ7W2fnp377O4tQ06Z%2Fe10vjfY%3D&reserved=0" target="_blank">
https://www.linkedin.com/in/luigipirelli</a><br>
* Stackexchange: <a href="https://can01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fgis.stackexchange.com%2Fusers%2F19667%2Fluigi-pirelli&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415304608%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=0yzkBn9YuoNx5WZm1j2YbB%2BamU%2FsC8RNFP0Q67iSBWg%3D&reserved=0" target="_blank">
http://gis.stackexchange.com/users/19667/luigi-pirelli</a><br>
* GitHub: <a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fluipir&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415314595%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=ziRXcudSAXlGux7tW9861S6sKYq15f05CnThn8%2BPhXE%3D&reserved=0" target="_blank">
https://github.com/luipir</a><br>
* Book: <a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.packtpub.com%2Feu%2Fapplication-development%2Fmastering-geospatial-development-qgis-3x-third-edition&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415314595%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=ysC6h9l6QSoOjK%2FteKI5B%2FO4MZ7spdpvbHI62WFTv7A%3D&reserved=0" target="_blank">Mastering
 QGIS3 - 3rd Edition</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">* Hire a team: <a href="https://can01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.qcooperative.net%2F&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415324588%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=GeOAEgasjj6rII5OV6ubTF90k6hOkJ6wLcOiYZ3CwVA%3D&reserved=0" target="_blank">
http://www.qcooperative.net</a><br>
**************************************************************************************************<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">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:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<p class="MsoNormal"><o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Hi Devs,<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">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" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">def flags(self):</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">    return super().flags() | QgsProcessingAlgorithm.FlagNoThreading</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">I saw this answer from Nyall on
<a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgis.stackexchange.com%2Fquestions%2F280520%2Flet-qgis-3-0-processing-algorithm-output-a-vectorlayer-loaded-via-the-delimited&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415324588%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=MkXiGTIDUro%2BK%2FEqbwp3qTOtaEQYa2KVTokrbaHwxOc%3D&reserved=0" 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>
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">class addLayerToCanvas(QgsProcessingAlgorithm):</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">    OUTPUT_raster = 'OUTPUT_raster'</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">   
</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">    def initAlgorithm(self, config):</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">        self.addOutput(QgsProcessingOutputMultipleLayers(self.OUTPUT_raster,</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">                                                        
<a href="https://can01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fself.tr%2F&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415334586%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=fTsev%2BfCeZBkYCHbCl8LFIS76opFy2nA7fbaM7%2FuGjU%3D&reserved=0" target="_blank">
self.tr</a>('Output raster')))</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">   
</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">    def processAlgorithm(self, parameters, context, feedback):</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">       
</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">        rlayer = QgsRasterLayer("F:/image.tif", "image") # The layer I want to add to the current project/canvas</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">        context.addLayerToLoadOnCompletion(<a href="https://can01.safelinks.protection.outlook.com/?url=http%3A%2F%2Frlayer.id%2F&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415334586%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=5xLEq4spSdCgtE1pBJdO1tYyC%2Bayb%2FpRGVndzpwAZFI%3D&reserved=0" target="_blank">rlayer.id</a>(),
 QgsProcessingContext.LayerDetails('image_layer', QgsProject.instance(), ''))</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">       
</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New"">        return {self.OUTPUT_raster:
<a href="https://can01.safelinks.protection.outlook.com/?url=http%3A%2F%2Frlayer.id%2F&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415344582%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=uT2tyLXn8rkNU4wojLGpzfOoKsDpaCFT5DISq1nrM1Q%3D&reserved=0" target="_blank">
rlayer.id</a>()}</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Thanks for any help<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="color:black"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black">Jean-François Bourdon, ing.f.</span></b><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#2E74B5">Analyste en télédétection</span></b><span style="font-size:10.0pt;font-family:"Arial",sans-serif"><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"><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><a href="http://www.mffp.gouv.qc.ca/accueil.jsp" target="_blank"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#2E74B5">mffp.gouv.qc.ca</span></b></a><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
</div>
</div>
<p class="MsoNormal">_______________________________________________<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://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415344582%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=sQYYs6MjBje6ek%2BLwMnNwM6PSM6sE2UM%2FnFa1v2mQsM%3D&reserved=0" target="_blank">
https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=04%7C01%7C%7Cba60a5a8d45f43a8ba7608d930a26b0e%7C8705e97737814f4790e1c84c8b884da1%7C0%7C1%7C637594296415354575%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=LWX6gbkLJ7bzo25AoGhJT2HLSn%2B1%2BxuzJf5Q0VklM8k%3D&reserved=0" target="_blank">
https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><o:p></o:p></p>
</blockquote>
</div>
</div>
</div>
</body>
</html>