1

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)

1 Answer 1

4

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
1
  • 1
    This worked! Thanks again. For anyone interested, I added ramplst[i].label = str(round(val)) to the for loop to round the numbers to zero decimal places.
    – ia200
    Feb 17, 2022 at 22:23

Your Answer

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

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