Thursday 3 October 2019

labeling - Reading and setting label settings in PyQGIS 3?


How do I read and set label style settings in QGIS 3.0.


For instance, in QGIS 2.18 i had done:


props = {
"labeling": "pal",
"labeling/enabled": "true",

"labeling/fontFamily": "Arial",
"labeling/fontSize": "12",
"labeling/fieldName": "Name",
"labeling/bufferDraw": True,
"labeling/bufferSize": "1",
"labeling/bufferColor": "white"
}
for k, v in props.items():
my_layer.setCustomProperty(k, v)


How do I do the equivalent in QGIS 3.0?



Answer



I found the answer for QGIS 3.0:


layer_settings  = QgsPalLayerSettings()
text_format = QgsTextFormat()

text_format.setFont(QFont("Arial", 12))
text_format.setSize(12)

buffer_settings = QgsTextBufferSettings()

buffer_settings.setEnabled(True)
buffer_settings.setSize(1)
buffer_settings.setColor(QColor("white"))

text_format.setBuffer(buffer_settings)
layer_settings.setFormat(text_format)

layer_settings.fieldName = "my_attribute"
layer_settings.placement = 2


layer_settings.enabled = True

layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
my_layer.setLabelsEnabled(True)
my_layer.setLabeling(layer_settings)
my_layer.triggerRepaint()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...