QGIS – Updating Symbol Size Units from mm to Map Units in Rule-Based Symbology

map-unitqgisrule-basedstylesymbology

I have a quite complex rule-based symbology that is currently using millimetres as size units.

rule-based symbology

All third levels look the same, they just have different parents.

Is there a way to change symbol size units from millimetres to map units for every rule entry so I don't have to click each entry and switch (which would be a pain in the * for a few dozen entries)?

When I have a categorized approach, I have the general symbology, that I can easily change and then applies to all categories, but how do I achieve that with a rule-based symbology?

The same goes for other aspects, like symbol size. Can I change that size for all symbols at once? e. g. from 15 mu to 18 mu.

Best Answer

Use the following script in QGIS Code Editor. It also works on nested rules. Select the layer before running the script.

layer = iface.activeLayer()
renderer = layer.renderer().clone()

def set_output_unit(rules):
    for rule in rules.children():
        rule.symbol().setOutputUnit(1) # 1: MapUnits    
        if rule.children():
            set_output_unit(rule)

rules = renderer.rootRule()
set_output_unit(rules)

layer.setRenderer(renderer)
layer.triggerRepaint()

enter image description here

Related Question