1

I have a multiband (WV3) satellite image that I'm displaying in false color (R=NIR, G=Red, B=Green) on QGIS 2.14. The quality of the results I'm getting via simply changing the band rendering in Layer-> Properties -> Style -> Band rendering --> Render Type: Multiband color are substantially different, and lower than using the SCP-plugin:

Regular rendering via layer properties: enter image description here

Band rendering via SCP: enter image description here

I need to do this programmatically via PyQGIS, so I can't use the plugin. I have also tried to play around with raster enhancement buttons but no success. Does anyone know how to improve the quality of band rendering?

UPDATE As pointed out by @user30184, the issue was the fact the max values were not updated in the band rendering (pressing the 'Load' button in the band rendering window). The problem remains - how to update the rendered min/max values via PyQGIS? My current code:

lyr = iface.activeLayer() #An multiband (8 band) raster 

lyr.renderer().setRedBand(7)
lyr.renderer().setGreenBand(5)
lyr.renderer().setBlueBand(3)

if hasattr(lyr, "setCacheImage"):
    lyr.setCacheImage(None)
lyr.triggerRepaint()

The 'Load' action that needs to be completed in PyQGIS: enter image description here

  • What do you mean with no success with raster enhancement buttons? Spread out first the "Load min/max values" menu and populate the min/max values. – user30184 Mar 7 '18 at 17:36
  • That actually worked, thanks! I assumed it does this automatically from PyQGIS upon changing the band with lyr.renderer().setRedBand(7) , but it doesn't. Do you know what is the method I should call on the QgsMultiBandColorRenderer to do this in Python (equivalent to the 'Load' button in the interface)? – 15Step Mar 7 '18 at 17:45
2

Here's a solution to updating the min/max values after changing band order using PyQGIS:

#Change Bands rendering
#5-3-2 = true color /// 7-5-3 = false color vegetation
lyr = iface.activeLayer()

bands=[7,5,3]
lyr.renderer().setRedBand(bands[0])
lyr.renderer().setGreenBand(bands[1])
lyr.renderer().setBlueBand(bands[2])

if hasattr(lyr, "setCacheImage"):
    lyr.setCacheImage(None)
lyr.triggerRepaint()

contrast = QgsRaster.ContrastEnhancementStdDev #OR: QgsRaster.ContrastEnhancementCumulativeCut
lyr.setContrastEnhancement(QgsContrastEnhancement.StretchToMinimumMaximum, contrast, iface.mapCanvas().extent())

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.