7

I am writing a python script to label features (lines). However I can't figure out:

  1. how to get the script to turn on the buffer

  2. how to get the script to apply the changes. When I currently run it, it changes everything in the properties (except the buffer) but I still have to hit ok before QGIS applies it to the map.

.

layer = iface.activeLayer()
layer.setCustomProperty("labeling/fieldName", "Length" )
layer.setCustomProperty("labeling/placement", QgsPalLayerSettings.Horizontal)
layer.setCustomProperty("labeling/predefinedPointPosition", QgsPalLayerSettings.BottomRight)
layer.setCustomProperty("labeling/fontSize","10" )
layer.setCustomProperty("labeling/buffer",QgsPalLayerSettings.BufferDraw)
layer.setCustomProperty("labeling/enabled","true" )
layer.triggerRepaint()

I have had a look at one similar question but I cannot seem to figure it out: How to label line features using python code?

Once I get this to work I want to transform it into a batch-script by adding some lines like:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
asked Jan 6, 2017 at 10:46
1
  • 1
    I only knew I could upvote but didn't see the little "accept" button. Thanks! Commented Aug 28, 2017 at 7:57

1 Answer 1

4

To turn on the buffer, you can use:

layer.setCustomProperty("labeling/bufferDraw", True)

You can also set various properties as described in the QgsPalLayerSettings class such as the size:

layer.setCustomProperty("labeling/bufferSize", "5")

So if you would like to apply this to all your loaded layers, you could use something like:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
 layer.setCustomProperty("labeling/fieldName", "Length" )
 layer.setCustomProperty("labeling/placement", QgsPalLayerSettings.Horizontal)
 layer.setCustomProperty("labeling/predefinedPointPosition", QgsPalLayerSettings.BottomRight)
 layer.setCustomProperty("labeling/fontSize","10" )
 layer.setCustomProperty("labeling/bufferDraw", True)
 layer.setCustomProperty("labeling/enabled","true" )
 layer.triggerRepaint()
answered Jan 6, 2017 at 11:15

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.