6

I'm trying to update a single band pseudo color renderer with new min/max values from the python console. All the code samples i have been able to find deal with constructing a renderer from scratch, but in this case I have a color ramp setup for an existing layer and I want to be able to stretch to color ramp with new min/max values.

I can set the new min/max values for the renderer, I just haven't figured out how to reclassify the color ramp (I'm looking for the equivalent of hitting the "classify" button in the style properties dialog).

This is what I have so far:

l = iface.activeLayer()

provider = l.dataProvider()
extent = canvas.extent()
stats = provider.bandStatistics(1, QgsRasterBandStats.All, extent)

min = stats.minimumValue
max = stats.maximumValue

l.renderer().setClassificationMin(min)
l.renderer().setClassificationMax(max)
Jochen Schwarze
  • 14,605
  • 7
  • 49
  • 117
amcaninch
  • 248
  • 2
  • 6

1 Answers1

4

With the next code you can get a ramp color red-yellow-blue . The min/max values for the renderer are got from raster stats.

from PyQt4.QtCore import *
from PyQt4.QtGui import *

layer = iface.activeLayer()

renderer = layer.renderer()
provider = layer.dataProvider()
extent = layer.extent()

ver = provider.hasStatistics(1, QgsRasterBandStats.All)

stats = provider.bandStatistics(1, QgsRasterBandStats.All,extent, 0)

if ver is not False:
    print "minimumValue = ", stats.minimumValue

    print "maximumValue = ", stats.maximumValue

if (stats.minimumValue < 0):
    min = 0  

else: 
    min= stats.minimumValue

max = stats.maximumValue
range = max - min
add = range//2
interval = min + add

colDic = {'red':'#ff0000', 'yellow':'#ffff00','blue':'#0000ff'}

valueList =[min, interval, max]

lst = [ QgsColorRampShader.ColorRampItem(valueList[0], QColor(colDic['red'])), 
        QgsColorRampShader.ColorRampItem(valueList[1], QColor(colDic['yellow'])), 
        QgsColorRampShader.ColorRampItem(valueList[2], QColor(colDic['blue']))]

myRasterShader = QgsRasterShader()
myColorRamp = QgsColorRampShader()

myColorRamp.setColorRampItemList(lst)
myColorRamp.setColorRampType(QgsColorRampShader.INTERPOLATED)
myRasterShader.setRasterShaderFunction(myColorRamp)

myPseudoRenderer = QgsSingleBandPseudoColorRenderer(layer.dataProvider(), 
                                                    layer.type(),  
                                                    myRasterShader)

layer.setRenderer(myPseudoRenderer)

layer.triggerRepaint()

Before running the code (raster in Map Canvas must be the active layer):

enter image description here

After running the code at the Python Console of QGIS:

enter image description here

xunilk
  • 29,891
  • 4
  • 41
  • 80
  • 2
    Thanks for the reply but this doesn't help me. As I tried to explain in the question I already have a raster layer loaded and symbolized. I don't want to define the color ramp because I want to be able to use this to stretch any raster layer I have loaded with whatever color ramp I already have set. I just want to reclassify the existing color ramp to the new min/max values. – amcaninch May 28 '15 at 22:36