2

For a script, I want to update the exsiting min and max of a colour ramp with a new one. In the image you can see what I want to update. enter image description here

To do so I tried to follow this thread. I tested the following code to update the raster:

min = 1
max = 5

l.renderer().setClassificationMin(min)
l.renderer().setClassificationMax(max)

I get the following error:

AttributeError: 'QgsSingleBandGrayRenderer' object has no attribute 'setClassificationMin'

I can't find any information about this code on the internet. The answer of the thread gives a solution but you HAVE to update the colour as well.

I'm wondering if somebody know how to update the min and max without updating the colour.

4
  • Are you wanting to update Singleband gray or Singleband pseudocolor? – Joseph Jun 16 '16 at 9:55
  • @Joseph pseudocolor – B.Termeer Jun 16 '16 at 12:44
  • Make sure you set the render type to pseudocolor before running the code. – Joseph Jun 16 '16 at 12:51
  • can you explain me how i can do this? – B.Termeer Jun 16 '16 at 12:54
1
  1. Double-click your raster to access its properties then go to the Style tab as you have shown in your question. Make sure the render type is set to Singleband pseudocolor:

    Raster render type


  1. Click Apply and OK.

  1. Make sure the raster layer is still selected and run the following code to change the min and max values:

    rLayer = iface.activeLayer()
    provider = rLayer.dataProvider()
    stats = provider.bandStatistics(1, QgsRasterBandStats.All, rLayer.extent())
    
    min = 1
    max = 5
    
    rLayer.renderer().setClassificationMin(min)
    rLayer.renderer().setClassificationMax(max)
    

    The min and max values should be the only settings which are updated.

    New min and max values

8
  • 1
    Thank you very much! can you briefly explain how you also update the layer? because the min and max is filled but it isn;t classified yet. I m sorry that i ased so much – B.Termeer Jun 16 '16 at 13:14
  • @B.Termeer - Most welcome! No need to apologise but not sure I can help you much there. In your question, you included a link to a thread. The answer in that thread allows you to apply classifications. – Joseph Jun 16 '16 at 13:31
  • i was aleready looking in that thread. I tried it with : rLayer.triggerRepaint() but that was not the trick – B.Termeer Jun 16 '16 at 13:34
  • @B.Termeer - triggerRepaint() only updates the appearence of the layer shown on the map, it doesn't apply to its settings. – Joseph Jun 16 '16 at 13:35
  • hmm, you can use QgsSingleBandPseudoColorRenderer but i have no shader argument, right? – B.Termeer Jun 16 '16 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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