8

In QGIS 3.4 raster values were in label form rounded to 2 decimal places regardless of exact values of the given raster. Is there a possibility to reduce labelling of raster values in QGIS 3.8 to 2 decimal places regardless of exact values as in QGIS 3.4 ?

labelling possibilities in QGIS 3.4

labelling possibilities in QGIS 3.8

1 Answer 1

5

Not sure if there is a simple way of doing this through the interface but we could accomplish this with a bit of Python. The code below stores the value and colour of the raster's symbology properties and rounds the value to 2 decimal places (you can change the value if you want) which it stores as the new label. Then it reapplies the updated properties


  1. So first select your layer from the Layers Panel to set it as the active layer:

    Raster with long labels


  1. Then enter the following code in the Python Console (press Ctrl + Alt + P to bring it up):

    rlayer = iface.activeLayer()
    fcn = QgsColorRampShader()
    fcn.setColorRampType(QgsColorRampShader.Interpolated)
    fcn.setClassificationMode(QgsColorRampShader.Continuous)
    lst = []
    for x, y in rlayer.renderer().legendSymbologyItems():
        value = float(x)
        label = str("{0:.2f}".format(float(value)))
        lst.append(QgsColorRampShader.ColorRampItem(value, QColor(y.red(), y.green(), y.blue()), label))
    
    fcn.setColorRampItemList(lst)
    shader = QgsRasterShader()
    shader.setRasterShaderFunction(fcn)
    renderer = QgsSingleBandPseudoColorRenderer(rlayer.dataProvider(), 1, shader)
    rlayer.setRenderer(renderer)
    rlayer.triggerRepaint()
    

    Result:

    Raster with shortened labels

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.