Tuesday 20 October 2015

Setting 'classificationMode = Continuous' to a raster layer using PyQGIS?


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()

Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...