3

I have a list ("datList") with names of raster layers which I want to add. When I add raster layers in the python console everything works fine:

for da in datListe:
   layerDir = "C:/[path]/" + da
   iface.addRasterLayer(layerDir,da)

When I do same in a processing script with...

for da in datListe:
   layerDir = "C:/[path]/" + da
   rlayer = QgsRasterLayer(layerDir,da)
   QgsProject.instance().addMapLayer(rlayer) 

...the layers are not added to my map. However they seem to be in the project as...

QgsProject.instance().mapLayers()

...(typed in the python console) prints a list with the desired layers

  • Try a refresh: iface.mapCanvas().refresh() – Andreas Müller Jul 12 at 12:20
  • Sorry, didn´t work. – jannik schilling Jul 12 at 12:53
  • Ok, processing scripts use a different approach, i found this question: gis.stackexchange.com/q/280503/46073 - looks strange to me, but may be a processing script is not an adequate method? – Andreas Müller Jul 12 at 13:21
  • Unfortunately I need to use a processing script: I´m generating the list "datListe" by extracting cooardinates of another point layer. Then these coordinates (concatenated as strings in a certain way) are the names of the raster layers I want to load. – jannik schilling Jul 12 at 15:11
  • In processing scripts you can't add layers to qgis while the the processing script is running. But you can specify an output parameter with a type of QgsProcessingOutputDefinition. If this is a layer, qgis will load it It's described in the docs. I looked into the qgis python api and found this class: QgsProcessingOutputMultipleLayers. It seems that this one may fit your needs. Unfortunately I didn't find an example describing how to use it. – Andreas Müller Jul 14 at 12:02

Your Answer

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

Browse other questions tagged or ask your own question.