Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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():
        range.setLowerValue(rangePoint)
        rangePoint += rangeIncrement
        range.setUpperValue(rangePoint)

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:

range.setLowerValue(float(rangePoint))

with no luck.

What am I doing wrong?

share|improve this question
    
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
add comment

1 Answer

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
        sym.setColor(QColor.fromHsv(hue,i,j))
    canvas = qgis.utils.iface.mapCanvas()
    canvas.refresh()

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

# 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:
            break
        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)
        rangeList.append(newRange)
        rangePoint += rangeIncrement

    newRenderer = QgsGraduatedSymbolRendererV2('',rangeList)
    newRenderer.setClassAttribute(layer.rendererV2().classAttribute())
    newRenderer.setMode(QgsGraduatedSymbolRendererV2.EqualInterval)
    layer.setRendererV2(newRenderer)

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

    qgis.utils.iface.legendInterface().refreshLayerSymbology(layer)
    canvas = qgis.utils.iface.mapCanvas()
    canvas.refresh()
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.