TransWikia.com

Configure symbol levels using PyQGIS

Geographic Information Systems Asked on August 2, 2021

I have a QgsCategorizedSymbolRenderer and I’m trying to configure it so that symbol_a:

  1. appears at the top of the layers window legend, but also
  2. appears on top of all other QgsRendererCategory symbols (i.e. it should be rendered last).

In QGIS, I can do this by going to Symbology > Advanced > Symbol Levels, checking Enable symbol levels and configuring the Layer 0 for Label A to 1, leaving the others at 0.

Using PyQGIS, I can enable symbol levels, but can’t find a way to change the specific symbol levels (i.e. the order they are rendered). Re-ordering the categories achieves this, but I’d prefer Label A to appear first in the layers window. Is this possible?

My existing code looks something like this:

categories = [
    QgsRendererCategory('value_a', symbol_a, 'Label A'),
    QgsRendererCategory('value_b', symbol_b, 'Label B'),
    QgsRendererCategory('value_c', symbol_c, 'Label C'),
]

renderer = QgsCategorizedSymbolRenderer('field_name', categories)
renderer.setUsingSymbolLevels(True)

One Answer

This post give a good answer with a clause applied on the renderer :

myRenderer.setOrderBy(QgsFeatureRequest.OrderBy([QgsFeatureRequest.OrderByClause('Value', False)]))
myRenderer.setOrderByEnabled(True)

Answered by GeoGyro on August 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP