Sunday, 12 April 2015

qgis 3 - Adding new rule by using QgsRuleBasedRenderer to existing one in PyQGIS?


I want to add new rules for a layer. However when I add them previous one(s) are deleted. Here is my code:


layer = iface.activeLayer()
symbol = QgsSymbol.defaultSymbol(layer.geometryType())

renderer = QgsRuleBasedRenderer(symbol)

# get the root rule
root_rule = renderer.rootRule()

# create clone of the default rule
rule = root_rule.children()[0].clone()

# set the label, expression and color
rule.setLabel("Akarsu Kutlesi")

rule.setFilterExpression(""""Subbasin" IN (2,4,6)""")
rule.symbol().setColor(QColor("orange"))

# append the rule to the list of rules
root_rule.appendtChild(rule)
# apply the renderer
layer.setRenderer(renderer)

Here is existing rules of layer:


enter image description here



enter image description here




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...