I am currently working on a model allowing me to extract and save the entities extracted from different sources. So far, with the graphical modeler, i managed to perform the extraction and the save into the correct folder. The last step would be to change the data source location of the existing layers with the layers generated by the extraction.

I prefer to keep existing layers to maintain map themes.

Is there a way to change the data location of existing layers with the path name of the generated layers ?

EDIT : I found a way to do it

Script below :

# Modifier la source de données des couches "X" et "Y"
X_layer = QgsProject.instance().mapLayersByName('X')[0]
X_layer.setDataSource(expression_save, 'X', 'ogr')

Y_layer = QgsProject.instance().mapLayersByName('Y')[0]
Y_layer.setDataSource(expression_group, 'Y', 'ogr')

1 Answer 1


You need to dig into the API. I think this might help you: qgis.core.QgsMapLayer.setDataSource

  • I tried adding these lignes but it did not work def setDataSource(QgsExpression("@project_folder||'/Couches/'|| @n_projet ||'_Cadastre_Site.shp'").evaluate(),"Cadastre_Site_71b2a1a7_6a27_4426_8dc5_ec2d40fe0ba1", "ogr") def setDataSource(QgsExpression("@project_folder||'/Couches/'|| @n_projet ||'_Site.shp'").evaluate(),"Site_bc4ccbb9_2f91_49b5_83dd_5a0f4985496b", "ogr") What am i missing ? Commented Mar 5, 2023 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.