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 ?
1 Answer
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
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: