Sunday 24 March 2019

qgis 2 - How to update the Layer Tree View after changing the color ramp in PyQGIS?


After changing a raster style/color ramp (wich is working properly to define the class colors and values) using:


layer = processing.getObjectFromName('NDVIrec')
provider = layer.dataProvider()
myColorRamp = QgsColorRampShader()
myColorRamp.setColorRampType(QgsColorRampShader.INTERPOLATED)


CumulativeLower = 0.02
CumulativeUpper = 0.98
min, max = layer.dataProvider().cumulativeCut(1, CumulativeLower,
CumulativeUpper)
range = max - min

add = range / 11
int2 = min + add
int3 = min + 2 * add
...

int10 = min + 9 * add

colDic = {'1': '#a50026', ..., '11': '#006837'}

valueList = [min, int2, ... int10, max]

lst = [QgsColorRampShader.ColorRampItem(valueList[0], QColor(colDic['1'])),
QgsColorRampShader.ColorRampItem(valueList[1], QColor(colDic['2'])),
...
QgsColorRampShader.ColorRampItem(valueList[10], QColor(colDic['11']))]


myColorRamp.setColorRampItemList(lst)
myRasterShader = QgsRasterShader()
myRasterShader.setRasterShaderFunction(myColorRamp)
myPseudoRenderer = QgsSingleBandPseudoColorRenderer(provider,
1,
myRasterShader)

layer.setRenderer(myPseudoRenderer)
layer.triggerRepaint()

iface.layerTreeView().refreshLayerSymbology(layer.id())

I get the expected result in the map canvas, but the Layer Tree View does not update properly, as seen here (the class values wont show up):



The only way i found for showing the values is to open the layer properties editor and hit the "apply" or "ok" button, wich is not sufficient for my application.


Am i doing something wrong with the code for updating the Layer Tree View (bellow)?


iface.layerTreeView().refreshLayerSymbology(layer.id())

Answer



You need a new list, another dictionary or put them (labels) directly in your QgsColorRampShader list (lst) as:


lst = [QgsColorRampShader.ColorRampItem(valueList[0], QColor(colDic['1']), 'class 1'),

QgsColorRampShader.ColorRampItem(valueList[1], QColor(colDic['2']), 'class 2'),
...
QgsColorRampShader.ColorRampItem(valueList[10], QColor(colDic['11']), 'class 11']

I tried out an equivalent code in QGIS 3, completely compatible with QGIS 2 because there are only a few differences as in following code lines:


layer = processing.getObjectFromName('NDVIrec')
.
.
.
myColorRamp.setColorRampType(QgsColorRampShader.INTERPOLATED)

.
.
.

My complete code is:


layer=iface.activeLayer()

colDic={'green light':'#1dd758', 'green':'green','light brown':'#d77c1c', 'brown':'brown', 'black':'black'}

valueList =[1, 2, 3, 4, 5]


lst = [ QgsColorRampShader.ColorRampItem(valueList[0], QColor(colDic['green light']), 'class 1'),
QgsColorRampShader.ColorRampItem(valueList[1], QColor(colDic['green']), 'class 2'),
QgsColorRampShader.ColorRampItem(valueList[2], QColor(colDic['light brown']), 'class 3'),
QgsColorRampShader.ColorRampItem(valueList[3], QColor(colDic['brown']), 'class 4'),
QgsColorRampShader.ColorRampItem(valueList[4], QColor(colDic['black']), 'class 5')]

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

After running above code at Python Console of QGIS I got updated LayerTree View with respective labels; as it can be observed at following image with a little random raster:


enter image description here


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...