2

I'm trying to control the number of decimal places represented for a layer in my project. I can do this in the symbology tab like this: enter image description here

I want to do this in PyQGIS. Here is an example of how I would symbolize my layer:

uri = '/Users/ep9k/Desktop/SandraMonson/TestZips.shp'
join_layer = iface.addVectorLayer(uri, 'Patients by Zip Code', 'ogr')
target_field = 'Patient_Da'

def apply_graduated_symbology():    

    myRangeList = []

    symbol = QgsSymbol.defaultSymbol(join_layer.geometryType())     
    symbol.setColor(QColor("#f5c9c9"))                              
    myRange = QgsRendererRange(0, 3, symbol, '0-3')                   
    myRangeList.append(myRange)                                     

    myRenderer = QgsGraduatedSymbolRenderer(target_field, myRangeList)  
    myRenderer.setMode(QgsGraduatedSymbolRenderer.Custom)               

    join_layer.setRenderer(myRenderer)                                  

    print(f"Graduated color scheme applied")

apply_graduated_symbology()

The class QgsRendererRangeLabelFormat seems to be what I need to control the precision of these labels, as it has a method setPrecision.

What I do not understand is, it seems QgsRendererRange and QgsRendererRangeLabelFormat have no relationship to each other according to the documentation, the PyQGIS console, etc. In fact, QgsRenderRangeLabelFormat seems to have no parent or child classes and I can't figure out how this class relates to anything else.

It seems like I should be able to do something like this:

label = QgsRendererRangeLabelFormat('0-3', 2)   #2nd argument sets the precision 
myRange = QgsRendererRange(0, 3, symbol, label) 

But this really doesn't make sense because I create a string ('0-3') and then setPrecision, which should affect the precision of a float.

Here is the help from the QGIS console regarding arguments. Why is the first argument a Qstring?: enter image description here

How do I use the class QgsRendererRangeLabelFormat?

1

When you set up each custom range, instantiating the QgsRendererRange class, you can simply pass your legend label as the 4th argument in whatever form you like.

For example:

myRange1 = QgsRendererRange(0.00, 3.00, mySymbol1, '0.00-3.00')
...
myRange2 = QgsRendererRange(3.10, 6.00, mySymbol2, '3.10-6.00')

etc.

In practice the appropriate precision, both for setting and interpreting each range, would probably depend on factors like the actual precision of the numeric attribute field you are using, the size of each value range etc.

Since that argument is just a string which will be used to label each range in the table of contents panel/ legend you can also include units to provide context e.g. '0-3 patients' or use very simple labels to broadly interpret each value range such as 'Low', 'Medium', 'High' etc.

By the way, you may find you also need to call: join_layer.triggerRepaint() after setting the renderer to apply the new layer symbology in the map canvas.

Edit: As for using QgsRendererRangeLabelFormat, I may be missing something but as far as I can tell, it just has an effect on the legend format settings in the layer symbology dialog.

Make a vector layer active and run this code in the Python Console:

myLayer = iface.activeLayer()
myFormat = QgsRendererRangeLabelFormat()
myFormat.setFormat("%1 - %2")
myFormat.setPrecision(2)
myFormat.setTrimTrailingZeroes(True)
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setLabelFormat(myFormat)
myLayer.setRenderer(myRenderer)

The graduated renderer has been applied to the layer and the label format settings have been applied:

enter image description here

  • thanks Ben. I arrived at the same conclusion you did, where the 4th argument in QgsRendererRange is the label which I can customize how I like. And this solution works well enough but it is not quite what I'm looking for. My question is really a meta-question of "how do I use this weird class that has no documentation and seemingly no relationship to other classes, but supposedly does what I want" – Erich Purpur May 20 at 1:53
  • 1
    Hi Erich, I have edited my answer- as far as I can tell, the only effect it has is on the label format settings in the graduated symbology dialog. – Ben W May 20 at 9:45
  • 1
    Thanks again Ben. After much effort I also could not get it to do anything except affect the graduated symbology dialog box. I wonder, what is the purpose of this class? Maybe it exists to support the dialog box and isn't really meant to be used programmatically? – Erich Purpur May 20 at 14:11

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.