Background: I'm currently trying to dynamically change the ranges for a layer with new style graduated symbology.

Problem: I thought it may be as simple as updating the ranges for the existing symbology using the setLowerValue() and setUpperValue() methods on the QgsRendererRangeV2 class as below:

layer = qgis.util.iface.activeLayer()
rangePoint = 0
rangeIncrement = 100
for range in layer.rendererV2().ranges():
        rangePoint += rangeIncrement

By my reconning the API documentation suggests that this should be possible.

However, I am getting the error:

AttributeError: 'QgsRendererRangeV2' object has no attribute 'setLowerValue'

I've tried also using:


with no luck.

What am I doing wrong?

  • Are you using 1.8? – Nathan W Oct 17 '12 at 5:24
  • Sorry, yes, 1.8. – Jayden Oct 17 '12 at 6:22
  • 2
    I suspect there might be missing Python binding for that class. – Nathan W Oct 17 '12 at 7:27
  • I'm able to call the lowerValue() method and it returns a value, I'm guessing this would suggest that the binding is there and exists? – Jayden Oct 17 '12 at 8:49
  • no because setLowerValue() is a different method and could be missing the binding. – Nathan W Oct 17 '12 at 8:51

I haven't been able to figure out how to fix the bindings if this was indeed the cause, I've used a work around, which was to follow the approach presented by pyqgis-cookbook, and to remove the existing ranges / symbology and re-create new ones (based on the existing ones). This approach now works for me. I'm not sure how efficient it is, but with the data set I'm currently using, it's quick enough.

from PyQt4.QtGui import QColor              # import this namespace as I want to refer to QColor later
from PyQt4.QtCore import QVariant

def satramp(hue):
    layer = qgis.utils.iface.activeLayer()
    grads = len(layer.rendererV2().symbols())
    vinc = 0
    val = 180
    i = 0 - (255 / grads)                                       # used to increment the saturation value
    j = val - (grads * vinc)
    for sym in layer.rendererV2().symbols():        # loop through each symbol in the layer using 'new symbology'
        i += (255 / grads)
        j += vinc
    canvas = qgis.utils.iface.mapCanvas()

def rap():
    canvas = qgis.utils.iface.mapCanvas()
    layer = qgis.utils.iface.activeLayer()

# gets the layer for the passed index value based on the legend index
def getLayerByIndex(layerIndex):
    idx = 0
    for layer in qgis.utils.iface.legendInterface().layers():
        if idx == layerIndex:
        idx += 1
    return layer

# gets the field index for the given field name
def getFieldIndex(fieldName,layer):
    mFieldMap = {}
    fields = layer.pendingFields()
    for ( key, field ) in fields.iteritems():
        if field.type() == QVariant.Int or field.type() == QVariant.Double:
            if field.name() == fieldName:
                return key

# sets a saturation ramp for a graduated layer only, based on the passed hue, value and index of the layer from the legend
def satramp(layerindex, hue, value):
    layer = getLayerByIndex(layerindex)
    grads = len(layer.rendererV2().symbols())                   # determine the number of graduations to use based on 
    vinc = 0                                                    # value incrementer, currently not used, but can be adjusted to increment the value also
    val = value                                                 
    i = 0 - (255 / grads)                                       # used to increment the saturation value
    j = val - (grads * vinc)
    layer.reload()                                              # refresh the data source

    # determine range increments
    provider = layer.dataProvider()
    fieldName = layer.rendererV2().classAttribute()
    fieldIndex = getFieldIndex(fieldName,layer)
    min = provider.minimumValue(fieldIndex).toDouble()[ 0 ]     # get the min and max values to calculate the new gradations
    max = provider.maximumValue(fieldIndex).toDouble()[ 0 ]
    rangeCount = len(layer.rendererV2().ranges())
    rangeIncrement = (max - min) / rangeCount
    rangePoint = min

    # construt a new renderer using ranges based on the existing renderer
    rangeList = []  
    for range in layer.rendererV2().ranges():
        newSymbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
        label = str(rangePoint) + " - " + str(rangePoint + rangeIncrement)
        newRange = QgsRendererRangeV2(rangePoint,rangePoint + rangeIncrement, newSymbol, label)
        rangePoint += rangeIncrement

    newRenderer = QgsGraduatedSymbolRendererV2('',rangeList)

    for sym in layer.rendererV2().symbols():        # loop through each symbol in the layer using 'new symbology'
        i += (255 / grads)
        j += vinc

    canvas = qgis.utils.iface.mapCanvas()
  • PS: I know my code isn't very robust and doesn't check appropriate pre-conditions, like ensuring the layer is actually a vector layer with a new graduated symbology - but for now, this meets my need without this additional consideration. – Jayden Oct 17 '12 at 20:40

First of all, checking the QGIS API doc, I can see that the ranges method from the QgsGraduatedSymbolRendererV2 class returns a

const QgsRangeList &

probably meaning that you have little chance to modify its return value.

Additionally, when looping the way you do in Python, you cannot modify a list's values as explained here.

=> twice impossible :)

Fortunately, the solution directly comes from the QgsGraduatedSymbolRendererV2 class thanks to :


The binding works correctly on my version (QGIS 2.8.2). The final code would look like this:

limit = 0
step = 100
for i, range in enumerate(renderer.ranges()):

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.