[QGIS-Developer] QgsProcessingAlgorithm / add Layer to project

Valérian Lebert vlebert at tactis.fr
Wed Mar 27 07:27:45 PDT 2019


Thanks Denis for your answer.

It helps but I still have some issues with rasterLayers :

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

c_insee = '57533'
n_couche = 'test_cadastre'
EPSG_code = '2154'
urlWithParams = "url=http://inspire.cadastre.gouv.fr/scpc/
"+c_insee+".wms?contextualWMSLegend=0&crs=EPSG:"+EPSG_code+"&dpiMode=7&featureCount=10&format=image/png&layers=AMORCES_CAD&layers=LIEUDIT&layers=CP.CadastralParcel&layers=SUBFISCAL&layers=CLOTURE&layers=DETAIL_TOPO&layers=HYDRO&layers=VOIE_COMMUNICATION&layers=BU.Building&layers=BORNE_REPERE&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&maxHeight=1024&maxWidth=1280"

rlayer = QgsRasterLayer(urlWithParams, 'Cadastre_'+n_couche+'_'+c_insee,
'wms')

context.temporaryLayerStore().addMapLayer(vlayer2)
context.temporaryLayerStore().addMapLayer(rlayer)

context.addLayerToLoadOnCompletion(vlayer2.id(),context.LayerDetails('test2',
context.project(), 'test2'))
context.addLayerToLoadOnCompletion(rlayer.id(),context.LayerDetails('cadastre',
context.project(), 'cadastre'))
------

vlayer2 is properly added to the project but i still get an error for rlayer


Chargement des couches de résultat

Les couches suivantes n'ont pas été générées
correctement.<ul><li>Cadastre_test_cadastre_57533_249deb86_fd00_4df3_af9a_8d084388ca91</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.

Any idea?

Le mer. 27 mars 2019 à 14:40, Denis Rouzaud <denis.rouzaud at gmail.com> a
écrit :

> Hi Valérian,
>
> Not 100% sure, but I think you miss
> context.temporaryLayerStore().addMapLayer(vlayer1) and up to vlayer4
> before actually adding them to the context.
>
> Let me know if it works,
>
> Denis
>
> Le mer. 27 mars 2019 à 04:32, Valérian Lebert <vlebert at tactis.fr> a
> écrit :
>
>> 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
>> _______________________________________________
>> 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
>
>
>
> --
> Denis Rouzaud
> denis at opengis.ch  <denis at opengis.ch>
> +41 76 370 21 22 <+41763702122>
> [image: OPENGIS.ch Logo] <https://www.opengis.ch>
>


-- 
Valérian Lebert

Directeur de projet
M : +33.7.75.10.77.51

P :  +33.1.49.57.05.05
E :  vlebert at tactis.fr
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190327/03ba1e2b/attachment-0001.html>


More information about the QGIS-Developer mailing list