[QGIS-Developer] How to use addLayerToLoadOnCompletion() ?

Bourdon, Jean-François (DIF) Jean-Francois.Bourdon at mffp.gouv.qc.ca
Wed Jun 16 08:31:26 PDT 2021


[Intranet logo]
Thanks. The solution came from Nyall, I only needed to add   context.temporaryLayerStore().addMapLayer(rlayer)   in order for the layer to not diseapear at the end of processing script.

However, how can I chose de name displayed for the layer? I tought that the name defined at    rlayer = QgsRasterLayer("F:/image.tif", "MyImage")   would be taken but it's not the case, it's rather the filename (without extension) that is displayed.

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>

De : Luigi Pirelli <luipir at gmail.com>
Envoyé : 16 juin 2021 04:40
À : Bourdon, Jean-François (DIF) <Jean-Francois.Bourdon at mffp.gouv.qc.ca>
Cc : qgis-developer <qgis-developer at lists.osgeo.org>
Objet : Re: [QGIS-Developer] How to use addLayerToLoadOnCompletion() ?

I didn't used too much this feature, I usually delegate to the results of the algorithm if to load or not.
BTW in my snippets (all commented so not used in my scripts) I set last LayerDetails parameter as the key in result dictionary =>

in your case should be:
context.addLayerToLoadOnCompletion(rlayer.id<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>(), QgsProcessingContext.LayerDetails('image_layer', QgsProject.instance(), self.OUTPUT_raster))

regards

Luigi Pirelli

**************************************************************************************************
* LinkedIn: https://www.linkedin.com/in/luigipirelli<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>
* Stackexchange: http://gis.stackexchange.com/users/19667/luigi-pirelli<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>
* GitHub: https://github.com/luipir<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>
* Book: Mastering QGIS3 - 3rd Edition<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>
* Hire a team: http://www.qcooperative.net<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>
**************************************************************************************************


On Wed, 16 Jun 2021 at 04:05, Bourdon, Jean-François (DIF) <Jean-Francois.Bourdon at mffp.gouv.qc.ca<mailto:Jean-Francois.Bourdon at mffp.gouv.qc.ca>> wrote:
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://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> 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<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>('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<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>(), QgsProcessingContext.LayerDetails('image_layer', QgsProject.instance(), ''))

        return {self.OUTPUT_raster: rlayer.id<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>()}

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<mailto:jean-francois.bourdon at mffp.gouv.qc.ca>
mffp.gouv.qc.ca<http://www.mffp.gouv.qc.ca/accueil.jsp>

_______________________________________________
QGIS-Developer mailing list
QGIS-Developer at lists.osgeo.org<mailto:QGIS-Developer at lists.osgeo.org>
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer<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>
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer<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>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210616/1dcaad51/attachment-0001.html>


More information about the QGIS-Developer mailing list