TransWikia.com

PyQGIS categorized symbol renderer sort by label

Geographic Information Systems Asked by GeoGyro on December 2, 2020

I wan’t to sort a QGIS categorized symbol renderer by label name with PyQGIS. First i create categories and after i apply them to my layer but how to make an ascending alphabetic order on categorie’s name ?

Documentation explain we can sort categories by their label with .sortByLabel().

I’m using QGIS3.10.2.

# Categories
categories = []
for myAttrName, (color, label, contour) in myStyleDict.items():
    symbol = QgsFillSymbol.defaultSymbol(myLayer.geometryType())
    symbol.setColor(QColor(color))
    symbol.symbolLayer(0).setStrokeWidth(0.26)
    symbol.symbolLayer(0).setStrokeColor(QColor(contour))
    category = QgsRendererCategory(myAttrName, symbol, label)
    categories.append(category)

# Renderer
expression = 'myAttrField'
renderer = QgsCategorizedSymbolRenderer(expression, categories) 
#renderer = QgsCategorizedSymbolRenderer(expression, categories).sortByLabel() ?  
myLayer.setRenderer(renderer)

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