2

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?

1
3

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
)
1
  • As always brilliant! – Taras Mar 24 '20 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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