1

I'm writing a script to load and manipulate some shapefiles. Unlike my previous question I now need to display labels only on certain zoom levels. My usual result is that the previous formatting gets over written after a rule has been set.

Outside of the rule set up I can display a point (without a marker) like this:

rendererP = layer.rendererV2()
symbolP = QgsFillSymbolV2.createSimple({'size':'0'})
rendererP.setSymbol(symbolP)
label = QgsPalLayerSettings()
label.readFromLayer(layer)
label.enabled = True
label.fieldName = 'Variable'
label.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'6','') label.placement= QgsPalLayerSettings.OverPoint
label.writeToLayer(layer)
QgsMapLayerRegistry.instance().addMapLayers([layer])

I'm using the following to set up the display on certain zoom levels:

symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
renderer = QgsRuleBasedRendererV2(symbol)
root_rule = renderer.rootRule()
## copy the rule to edit
rule = root_rule.children()[0].clone()
rule.setScaleMinDenom(1000000)
rule.setScaleMaxDenom(4000000) 
root_rule.appendChild(rule)
## delete the default rule
root_rule.removeChildAt(0)
## apply the renderer to the layer
layer.setRendererV2(renderer)

But the moment I attempt to apply the zoom level rules to this layer the markers returns. Can anyone give me some guidance?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.