How to get raster rendering labels in legend from Python console in QGIS

pyqgispyqgis-3pythonqgis

I have some code that searches a set of .tif files to find the overall min and max values so I can apply a consistent color ramp to all the files. This code works well, except that no labels show for each color in the legend. There are also no labels showing within the QGIS layers window.

enter image description here

Here is my code through where I set the renderer for the raster:

# Loop through files in folder to get overall min and max values for color ramp
ovrmin = 1000
ovrmax = -1000
for i in range(0,len(targ)):
    filepath = "/".join([fldr, targ[i]])
    rasti = QgsRasterLayer(filepath, "") # Get layer without loading into QGIS consol
    stats = rasti.dataProvider().bandStatistics(1, QgsRasterBandStats.All)
    filemin = stats.minimumValue
    filemax = stats.maximumValue
    if filemin < ovrmin:
        ovrmin = filemin
    if filemax > ovrmax:
        ovrmax = filemax



# Create color ramp
mid = ((ovrmax-ovrmin)/2)+ovrmin
rampvals = [ovrmin, ((mid-ovrmin)/2)+ovrmin, mid, ((ovrmax-mid)/2)+mid, ovrmax]
rampcols = [QColor(43, 131, 186),QColor(171, 221, 164),QColor(255, 255, 191),QColor(253, 174, 97),QColor(215, 25, 28)] # Spectral colors

fnc = QgsColorRampShader()
fnc.setColorRampType(QgsColorRampShader.Interpolated)

ramplst = [QgsColorRampShader.ColorRampItem(rampvals[0], rampcols[0]),
QgsColorRampShader.ColorRampItem(rampvals[1], rampcols[1]),
QgsColorRampShader.ColorRampItem(rampvals[2], rampcols[2]),
QgsColorRampShader.ColorRampItem(rampvals[3], rampcols[3]),
QgsColorRampShader.ColorRampItem(rampvals[4], rampcols[4]),]

fnc.setColorRampItemList(ramplst)

shader = QgsRasterShader()
shader.setRasterShaderFunction(fnc)



# Use overall min max values to symoblize and export all rasters

#for i in range(0,len(targ)):
for i in (1,2):# Testing
    
    # Load Raster File
    fn = "/".join([fldr, targ[i]])
    fi = QFileInfo(fn)
    fname = fi.baseName()
    rlayer = iface.addRasterLayer(fn, fname)
    
    renderer = QgsSingleBandPseudoColorRenderer(rlayer.dataProvider(), 1, shader)
    rlayer.setRenderer(renderer)

Best Answer

I can't test properly right now, but I believe you set the legend labels like so:

mid = ((ovrmax-ovrmin)/2)+ovrmin
rampvals = [ovrmin, ((mid-ovrmin)/2)+ovrmin, mid, ((ovrmax-mid)/2)+mid, ovrmax]
rampcols = [QColor(43, 131, 186),QColor(171, 221, 164),QColor(255, 255, 191),QColor(253, 174, 97),QColor(215, 25, 28)] # Spectral colors

fnc = QgsColorRampShader()
fnc.setColorRampType(QgsColorRampShader.Interpolated)

ramplst = [QgsColorRampShader.ColorRampItem(rampvals[0], rampcols[0]),
QgsColorRampShader.ColorRampItem(rampvals[1], rampcols[1]),
QgsColorRampShader.ColorRampItem(rampvals[2], rampcols[2]),
QgsColorRampShader.ColorRampItem(rampvals[3], rampcols[3]),
QgsColorRampShader.ColorRampItem(rampvals[4], rampcols[4]),]

# set the legend item labels
for i,val in enumerate(rampvals):    # for each of values in rampvals
    ramplst[i].label = str(val)      # set the string of the value to the `label` attribute of the corresponding QgsColorRampShader object in ramplst

fnc.setColorRampItemList(ramplst)

# etc