2

I would like to load all raster layers with type .xyz which are in a folder into my QGIS Project using PyQGIS and wrote the following function

 for lyr in glob.glob(os.path.join(inputDir, "* .xyz")):
 rasterlyr = QgsRasterLayer(lyr , "")
 QgsProject.instance().addMapLayers(rasterlyr) 

I'm getting the following error message:

QgsProject.addMapLayers(): argument 1 has unexpected type 'QgsRasterLayer'

But according to the Dokumentation "Cheat Sheet" this should actually work.

Does anyone see the mistake?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 23, 2019 at 21:41

1 Answer 1

2

You used QgsProject.instance().addMapLayers(rasterlyr) with a s while you are giving a single raster layer. You need to use the singular version or to give a list of layers (even with a single layer in the list).

Either do

QgsProject.instance().addMapLayer(rasterlyr) 

or

QgsProject.instance().addMapLayers([rasterlyr]) 

My preference is the first one.

answered Nov 23, 2019 at 21:43

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.