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():
  • 1
    I only knew I could upvote but didn't see the little "accept" button. Thanks! – Hannes Ledegen Aug 28 '17 at 7:57
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()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.