I have a QGIS Project with many raster layers. I need to change/repair all paths since I moved the original data to Another disk.
layer = iface.activeLayer()
old_path = r'D:/Somefolder_QGIS_200317'
new_path = r'C:\GIS\Newfolder'
layer.setDataSource(layer.source().replace(old_path, new_path))
But im getting error:
TypeError: QgsRasterLayer.setDataSource(): not enough arguments
What am I missing?
-
2Look at : qgis.org/pyqgis/master/core/…J. Monticolo– J. Monticolo2020年03月24日 08:28:07 +00:00Commented Mar 24, 2020 at 8:28
1 Answer 1
If you look at the PyQGIS documentation, you'll see that the function has not enough arguments
, it miss the baseName
, provider
and data provider options
. The code below works for me. I just take all the same arguments, except the source :
vl = iface.activeLayer()
new_data_source = "/home/me/Desktop/QGIS-Training-Data-2.0/exercise_data/raster/3320C_2010_314_RGB_LATLNG.tif"
base_name = vl.name()
provider = vl.providerType()
options = vl.dataProvider().ProviderOptions()
vl.setDataSource(
new_data_source,
base_name,
provider,
options
)