-1

I would like to symbolize raster layers using PyQGIS. If I understand correctly using a prepared style-file is no solution, since the maximum value of the raster changes depending on the inputs of it's calculation. So, if the symbology expects values from 0 to 100, all cells with values above that will disappear. If I configure the style file to go up to 1000 it will warp the colour ramp if the raster values are just up to 100. I would also like to change the labeling and remove the Band information of the raster representation in the legend in the process.

The following code works in doing the first but does not change the Legend Symbol.

sum_raster_in_map = QgsProject.instance().addMapLayer(sum_raster)

            ndvi_layer = sum_raster_in_map
            prov = ndvi_layer.dataProvider()
            stats = prov.bandStatistics(1, QgsRasterBandStats.All, ndvi_layer.extent(), 0)
            max_value = stats.maximumValue
            renderer = QgsSingleBandPseudoColorRenderer(prov, 1)
            style = QgsStyle.defaultStyle()
            color_ramp = style.colorRamp('Magma')
            renderer.setClassificationMin(1)
            renderer.setClassificationMax(max_value)
            renderer.createShader(color_ramp, clip=True)
            ndvi_layer.setRenderer(renderer)
            ndvi_layer.triggerRepaint()
6
  • Maybe have a look at: gis.stackexchange.com/questions/484128/…
    – Ben W
    Commented Nov 11 at 13:02
  • Thank you. The clip was missing, but I was able to fix that myself.
    – RavenS
    Commented Nov 11 at 13:57
  • I have added a second edit to the questions, since now I seemingly cannot edit the labeling of the raster color ramp to, say, 'high' to 'low'
    – RavenS
    Commented Nov 11 at 14:41
  • 1
    Instead of editing your question with a new problem you are facing, please ask a new question. This site has a One-Question-Per-Question and Best Answer policy. The questions are not ongoing projects that are solved incrementally.
    – Matt
    Commented Nov 12 at 9:10
  • 1
    Yes, indeed. Because the original issue is fixed with Ben's solution. You can of course link this question in the new one for context.
    – Matt
    Commented Nov 14 at 11:16

1 Answer 1

0

That's an easy fix. You just need to access the renderer color ramp shader's legend settings and set the minimum and maximum labels:

sum_raster_in_map = QgsProject.instance().addMapLayer(sum_raster)
prov = sum_raster_in_map.dataProvider()
stats = prov.bandStatistics(1, QgsRasterBandStats.All, sum_raster_in_map.extent(), 0)
max_value = stats.maximumValue
renderer = QgsSingleBandPseudoColorRenderer(prov, 1)
style = QgsStyle.defaultStyle()
color_ramp = style.colorRamp('Magma')
renderer.setClassificationMin(1)
renderer.setClassificationMax(max_value)
renderer.createShader(color_ramp, clip=True)
### Add these lines...
color_ramp_shader = renderer.shader().rasterShaderFunction()# QgsColorRampShader object
legend_settings = color_ramp_shader.legendSettings()
legend_settings.setMinimumLabel('Low')
legend_settings.setMaximumLabel('High')
#######################
sum_raster_in_map.setRenderer(renderer)
sum_raster_in_map.triggerRepaint()
2
  • I was toying with that but couldn't get it to work. Thank you most kindly, good sir! Is it also possible to remove the Band 1 text this way? Also, should I have been able to find that information somewhere? I searched for quite some time but could not turn up any solution
    – RavenS
    Commented Nov 12 at 7:40
  • @RavenS, the snippet in this GitHub Gist will show you how to remove the Band 1 legend node label.
    – Ben W
    Commented Nov 13 at 8:58

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