I am trying to use PyQGIS to convert a Fill Symbol Layer into a geometry generator Symbol Layer in a rule-based renderer with 300 rules.
I am able to iterate over the rules and return each rule's symbol and then the respective (unique) Symbol Layer. I know there's a changeSymbolLayer()
What I am failing to do, is to create a QgsGeometryGeneratorSymbolLayer to overwrite the existing symbol layer. Until now, the code is something like this:
layer = iface.mapCanvas().currentLayer()
rules = layer.renderer().rootRule().children()
for rule in rules:
label = rule.label()
symbol_layer = rule.symbol().symbolLayer(0)
print(symbol_layer)
color = symbol_layer.color()
generator = QgsGeometryGeneratorSymbolLayer()
generator.setFillColor(color)
generator.setGeometryExpression(expression)
rule.symbol().changeSymbolLayer(0,generator)
In this case the QgsGeometryGeneratorSymbolLayer() fails by saying that I am not able to instantiate.
I am not understanding how to create the new symbol to apply on each rule.
.create()
or.clone()
instead?generator = QgsGeometryGeneratorSymbolLayer.create({})
seems to workcreate
properties is a bit lacking, but it seems you can create an empty one and just set everything later