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?

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.

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.