2

I have many raster layers in my QGIS project and they're all currently rendered as: Multiband color Red band: Band 1 Green band: Band 2 Blue band: Band 3

I'd like to know if there's any way I can change the rendering of all raster layers to: Red band: Band 3 Green band: Band 2 Blue band: Band 1 without having to do it by hand, layer by layer.

3

You can use pyqgis. This will change symbology of all raster layers in layer tree to red band=band 3, green=band 2, blue=band 1:

for lyr in QgsProject.instance().mapLayers().values():
    if isinstance(lyr, qgis.core.QgsRasterLayer): #Check to make sure it is a raster layer
        r = lyr.renderer().clone()
        r.setRedBand(3)
        r.setGreenBand(2)
        r.setBlueBand(1)
        lyr.setRenderer(r)
        lyr.triggerRepaint()

enter image description here

4
  • 1
    thank you so much! – will Aug 16 '20 at 20:43
  • I'm sorry to bother you, but I've ran into some trouble. The bands change accordingly, but the changes don't apply unless I go into each layer's symbology and hit the "Apply" button. I tried using triggerRepaint() and other similar methods like qgis.utils.iface.mapCanvas().refresh() but they don't seem to work. Any help? – will Aug 17 '20 at 2:22
  • Try now, I've changed the code! – BERA Aug 17 '20 at 6:19
  • The same thing still happens. I think it has something to do with applying the "stretch to min-max" contrast settings. I tried working around it with something like this: layer = iface.activeLayer() contrastFilter = QgsBrightnessContrastFilter() contrastFilter.setContrast(50) layer.pipe().set(contrastFilter) layer.triggerRepaint() but it basically does nothing – will Aug 17 '20 at 13:18
1

Set it up for one layer.

Right click that layer, Styles -> Copy Style.

Select all the other layers you want to apply the same style to, right-click, Styles -> Paste Style.

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.