4

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?

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Mar 24, 2020 at 6:47
1

1 Answer 1

6

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
)
answered Mar 24, 2020 at 8:42
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.