3

I want to make a copy from an existing layer in the legend and render it with a rule-based renderer based on the original layer's graduated symbol renderer. Then I will change its rule expressions, clone all the rules (if that is even possible) and change again their expressions.

Right now I'm at step one: creating a rule-based renderer from an existing graduated symbol renderer. With iface.actionDuplicateLayer().trigger() I clone the original layer with its renderer. Then I use convertFromRenderer function to create a QgsRuleBasedRendererV2 from the new layer's graduated symbol renderer and I assign it to the layer. But there's something I'm doing wrong because QGIS crashes at this point and I can't find the reason.

# Duplicate original layer
for layer in iface.legendInterface().layers():
 if layer.name() == 'Lines':
 iface.setActiveLayer(layer)
 iface.actionDuplicateLayer().trigger()
# Make the new layer visible and change its name
for layer in iface.legendInterface().layers():
 if layer.name() == 'Lines copy':
 vl = layer
iface.legendInterface().setLayerVisible(vl, True)
vl.setLayerName('Lines - Atlas')
# Create rule-based renderer from its graduated symbol renderer
# and assign it to it
renderer = vl.rendererV2()
if renderer.type() == 'graduatedSymbol':
 rSimbol = renderer.sourceSymbol()
 ruleRend = QgsRuleBasedRendererV2(rSimbol).convertFromRenderer(renderer)
 vl.setRendererV2(ruleRend) #<------- QGIS crashes here
 iface.legendInterface().refreshLayerSymbology(vl)
#Refresh canvas
canvas = iface.mapCanvas()
canvas.refresh()
asked Oct 27, 2016 at 9:10
3
  • 1
    Does that work better? QgsRuleBasedRendererV2.convertFromRenderer(renderer) Commented Oct 27, 2016 at 9:50
  • Thank you so much @MatthiasKuhn. That has sense. There is no need to provide a default symbol to the renderer since it will take everything from the original renderer. I don't know why I discarded that possibility. Commented Oct 27, 2016 at 10:24
  • Great, turned it into an answer ;) Commented Oct 27, 2016 at 10:25

1 Answer 1

3

convertFromRenderer is a static function and therefore does not need an instance to work on.

You can simply use the following:

if renderer.type() == 'graduatedSymbol':
 ruleRend = QgsRuleBasedRendererV2.convertFromRenderer(renderer)
 ...
answered Oct 27, 2016 at 10:25
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.