5

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

1 Answer 1

6

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.

0

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