[QGIS-Developer] How to use addLayerToLoadOnCompletion() ?
Luigi Pirelli
luipir at gmail.com
Wed Jun 16 01:40:26 PDT 2021
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(),
QgsProcessingContext.LayerDetails('image_layer',
QgsProject.instance(), self.OUTPUT_raster))
regards
Luigi Pirelli
**************************************************************************************************
* LinkedIn: https://www.linkedin.com/in/luigipirelli
* Stackexchange: http://gis.stackexchange.com/users/19667/luigi-pirelli
* GitHub: https://github.com/luipir
* Book: Mastering QGIS3 - 3rd Edition
<https://www.packtpub.com/eu/application-development/mastering-geospatial-development-qgis-3x-third-edition>
* Hire a team: http://www.qcooperative.net
**************************************************************************************************
On Wed, 16 Jun 2021 at 04:05, Bourdon, Jean-François (DIF) <
Jean-Francois.Bourdon at mffp.gouv.qc.ca> wrote:
> [image: 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
> <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
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210616/77cf8ece/attachment-0001.html>
More information about the QGIS-Developer
mailing list