3

I need to add to this script:

How to update raster color ramp with new min/max values using PyQGIS?

this function: QgsColorRampShader for setting 'classificationMode = Continuous', similarly as setColorRampType(QgsColorRampShader.INTERPOLATED), but I tried it and it doesn't work.

    def setStyle(self, layer):
    provider = layer.dataProvider()
    extent = layer.extent()

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

    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)
    # TODO: add classificationMode=Continuos
    myRasterShader.setRasterShaderFunction(myColorRamp)

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

    layer.setRenderer(myPseudoRenderer)

    layer.triggerRepaint()

1 Answer 1

1

Unfortunately, it seems that the last QgsColorRampShader() class has not a method that allows setting a classification mode.

Instead, looking at the same API for the upcoming QGIS 3.0, there is the setClassificationMode() module that should do what you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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