3

I'm writing a plugin that extracts some data from a Postgres database, saves a point shapefile of the data and displays it in QGIS. As part of that process, I want to generate a QML style file.

I have this basically working - with the exception of the label placement. I want the label placed NE (above and right) of the point, offset X=1, Y=0.

I have the following code. It all works - but the label is placed 'Around point' rather than offset up and to the right.

    #Add labels.
    layer_settings = QgsPalLayerSettings()
    layer_settings.fieldName = "gr"
    layer_settings.isExpression = True

    layer_settings.OffsetType = QgsPalLayerSettings.FromPoint
    layer_settings.OffsetQuad = 'NE'
    layer_settings.OffsetXY = '1,0'
    layer_settings.OffsetUnits = QgsUnitTypes.RenderUnit.RenderMillimeters
    layer_settings.enabled = True

    text_format = QgsTextFormat()
    text_format.setFont(QFont("Arial Black", 6))
    text_format.setSize(6)
    layer_settings.setFormat(text_format)

    labeling = QgsVectorLayerSimpleLabeling(layer_settings)
    layer.setLabelsEnabled(True)
    layer.setLabeling(labeling)
    layer.triggerRepaint()

    # Save the style file for future use in QGIS.
    outputFile = os.path.join(self.SearchOutputFolder, searchCategoryFileName) + '.qml'
    layer.saveNamedStyle(outputFile)

I've had to guess how to use OffsetQuad and OffsetXY. I just can't seem to find any documentation on how to call/set them.

1 Answer 1

3

Probably a duplicate of this question but you could use the following to set up your label placements:

# Placement: Cartographic = 6; Around point = 0; Offset from point = 1
layer_settings.placement = 1
# Quadrant position: QuadrantAboveLeft = 0; QuadrantAbove = 1,...
layer_settings.quadOffset = 2
# Offset X
layer_settings.xOffset = 1.0
# Offset Y
layer_settings.yOffset = 0.0

So your code could look like:

#Add labels.
layer_settings = QgsPalLayerSettings()
layer_settings.fieldName = "gr"
layer_settings.isExpression = True

layer_settings.placement = 1
layer_settings.quadOffset = 2
layer_settings.xOffset = 1.0
layer_settings.yOffset = 0.0
layer_settings.enabled = True

text_format = QgsTextFormat()
text_format.setFont(QFont("Arial Black", 6))
text_format.setSize(6)
layer_settings.setFormat(text_format)

labeling = QgsVectorLayerSimpleLabeling(layer_settings)
layer.setLabelsEnabled(True)
layer.setLabeling(labeling)
layer.triggerRepaint()

# Save the style file for future use in QGIS.
outputFile = os.path.join(self.SearchOutputFolder, searchCategoryFileName) + '.qml'
layer.saveNamedStyle(outputFile)
4
  • 1
    Thanks @Joseph - that worked great. Where can i find the documentation for this? Commented Jun 20, 2019 at 9:13
  • @SteveGoddard - Most welcome, glad it helped! You can find some information in the class api for QgsPalLayerSettings. You can find some examples here.
    – Joseph
    Commented Jun 20, 2019 at 9:39
  • Great work @Joseph, really appreciate the layer_settings.placement = 1 comments
    – grego
    Commented Nov 26, 2021 at 0:52
  • For future readers ... .placement and .quadOffset do not work with integer anymore: gis.stackexchange.com/a/468518/16141 Commented Apr 27 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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