1

I have a standalone pyqgis3.2.1 (Bonn) application that can display maps (vectors, rasters). I tried to setup labeling of vector layers using the following codes, but it only works for point layers, not for line layers:

pal_layer = QgsPalLayerSettings()
pal_layer.fieldName = 'facilityid'
pal_layer.enabled = True
labels = QgsVectorLayerSimpleLabeling(pal_layer)
layer.setLabeling(labels)
layer.setLabelsEnabled(True)
layer.triggerRepaint()

I also tried setting XML properties approach like in this post, but it didn't work on line vector either. What did I do wrong?

  • have you tried adding pal_layer.placement = QgsPalLayerSettings.Line as suggested in below link gis.stackexchange.com/questions/122280/… – Shiko Sep 12 '18 at 22:20
  • @Shiko Yes, that did it. Please post it as an answer for me to accept it, and sorry I didn't see the other post, I did a search, guess it slipped my eye. But I've got to gripe a bit about this change from PyQgis 2, I really don't see why this is necessary, I mean why I don't have to do this for point? such geometry type detection can easily be done in any of the "set" method internally... – For Comment Sep 12 '18 at 22:39
  • Also, I actually tried pal_layer.placement = QgsPalLayerSettings.AboveLine and it didn't work, which is a much more sensible setting than 'Line' ! – For Comment Sep 12 '18 at 22:51
  • I think Above line is related to LinePlacementFlags and not Placement – Shiko Sep 12 '18 at 22:55
  • if you check the source code and the API class reference you will get it qgis.org/api/qgspallabeling_8h_source.html and qgis.org/api/… – Shiko Sep 12 '18 at 22:56
2

You need to add pal_layer.placement = QgsPalLayerSettings.Line as it seems Line feature needs placement property to be defined.

pal_layer = QgsPalLayerSettings()
pal_layer.fieldName = 'facilityid'
pal_layer.enabled = True
pal_layer.placement = QgsPalLayerSettings.Line
labels = QgsVectorLayerSimpleLabeling(pal_layer)
layer.setLabeling(labels)
layer.setLabelsEnabled(True)
layer.triggerRepaint()

Below are the available options:

enum Placement
     {
       AroundPoint, 
       OverPoint, 
       Line, 
       Curved, 
       Horizontal, 
       Free, 
       OrderedPositionsAroundPoint, 
       PerimeterCurved, 
     };

Enumerator

AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygon layers only.

OverPoint

Line
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point. Applies to point or polygon layers only.

Arranges candidates parallel to a generalised line representing the feature or parallel to a polygon's perimeter. Applies to line or polygon layers only.

Curved [NA]

Horizontal
Arranges candidates following the curvature of a line feature. Applies to line layers only.

Arranges horizontal candidates scattered throughout a polygon feature. Applies to polygon layers only.

Free
Arranges candidates scattered throughout a polygon feature. Candidates are rotated to respect the polygon's orientation. Applies to polygon layers only.

OrderedPositionsAroundPoint
Candidates are placed in predefined positions around a point. Preference is given to positions with greatest cartographic appeal, e.g., top right, bottom right, etc. Applies to point layers only.

PerimeterCurved [NA]

Reference:
https://qgis.org/api/classQgsPalLayerSettings.html#a5d449d8e9cb89e5d453c6e33fe1c66cc https://qgis.org/api/qgspallabeling_8h_source.html

| improve this answer | |
  • If AboveLine is for LinePlacementFlags, then naturally one would think AbovePoint or OverPoint are for PointPlacementFlags, not for Placement. Actually I am still kinda not clear what's the difference between LinePlacementFlags and Placement, it seems they are all for where to place the text label, right? – For Comment Sep 13 '18 at 3:26
  • @ForComment the word line and point not related to layer type as per explanation, because the same type can be applied to different feature types – Shiko Sep 13 '18 at 3:31

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.