[QGIS-Developer] QgsProcessingAlgorithm / add Layer to project

Valérian Lebert vlebert at tactis.fr
Wed Mar 27 02:32:35 PDT 2019


Hello,

I am trying to add a layer to project in a QgsProcessingAlgorithm.

I run the folowing test code, just to find a way to add a layer to the
project. I don't want to use featureSink because at the end the layer I
want to add is a raster layer :

def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """

        vlayer1 =
QgsVectorLayer('Point?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes',
'vlayer1', "memory")
        vlayer2 =
QgsVectorLayer('Point?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes',
'vlayer2', "memory")
        vlayer3 =
QgsVectorLayer('Point?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes',
'vlayer3', "memory")
        vlayer4 =
QgsVectorLayer('Point?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes',
'vlayer4', "memory")

        context.addLayerToLoadOnCompletion(vlayer1.id(),context.LayerDetails('toto',
QgsProject.instance(), 'test1'))
        context.addLayerToLoadOnCompletion(vlayer2.id(),context.LayerDetails('toto',
context.project(), 'test2'))
        QgsProject.instance().addMapLayer(vlayer3, True)
        context.project().addMapLayer(vlayer4, True)


        return {self.OUTPUT: 'toto'}

I read that adding a layer directly from instance() should not be used in a
processing algorithm.
However, none of the created vectorLayer is added to the legend.

I get a warning on processing feedback with no details in message log :
Chargement des couches de résultat
Les couches suivantes n'ont pas été générées
correctement.<ul><li>vlayer1_98306459_f1bc_4285_b8dc_660502e5cb47</li><li>vlayer2_040a7ea0_7a5f_4e02_bfd1_4ce311c60219</li></ul>Vous
pouvez vérifier le Panel de messages du journal dans la fenêtre principale
de QGIS pour trouver plus d'informations à propos de l'exécution de
l'algorithme.

Am i doing wrong or is there something broken in procesing API?

Thanks

Valérian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190327/11c4b725/attachment.html>


More information about the QGIS-Developer mailing list