4

Using 3.10.1-A Coruña on Windows 10

I want to adjust my label placement options in a Python script.

I've had a look at the QgsPalLayerSettings Class Reference and similar questions at:

but can't find where I can set the Centroid options in PyQGIS like I can in my UI shown in the pic below.

enter image description here

My UI looks very different to the one in the similar question at Setting placement labels on "Offset From Point" with OffsetX,Y parameters in PyQGIS?

I'm guessing it will be in the settings class. I have added settings.placement = 0 as I understand that is Around, but not sure how to set placement to Centroid:Whole Polygon and Force visible point inside polygon with PyQGIS

settings = QgsPalLayerSettings()
settings.fieldName = 'POA_CODE16'
settings.placement = 0
5

The QgsPalLayerSettings class has the boolean attributes: centroidInside and centroidWhole.

See docs

So, you could add the following lines:

settings.centroidWhole = True
settings.centroidInside = True

This will calculate the centroid from the whole polygon and force the centroid positioned labels inside their corresponding polygon features.

2
  • 1
    Thanks again Ben W :) Works a treat. Dec 29 '19 at 22:51
  • You're welcome!
    – Ben W
    Dec 30 '19 at 0:04

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.