How to label only selected features in QGIS 2.8 and up
In 2011, I wrote “How to Label Only Selected Features in QGIS” which ends with the wish that
Another “data defined setting” like “show this label (true/false)” would be more intuitive.
… and now we have it!
It’s called Show label and you can find it in the Rendering section of the labeling dialog.
The following screenshot shows a quick example of how to label only airports starting with A by setting the expression
"NAME" LIKE 'A%'
This post was motivated by a question by Eduardo here on this blog. Hope it helps!
Thanks for answer my question, Anita!
It really helped a lot.
See ya. =D
This is great, thank you very much for all of your work on QGIS and answering people’s questions. Would you be willing/able to link to a syntax resource for the expression string builder?
Thanks Valerie! The right-hand side of the expression dialog shows help and examples for all available functions and variables.
Thanks for your post. I’m trying to make the same with PyQgis using PAL, but I don’t know how to make it. Could you show me the way?
This is my code:
from PyQt4.QtGui import QColor
layer = iface.activeLayer()
palyr = QgsPalLayerSettings()
palyr.readFromLayer(layer)
palyr.enabled = True
palyr.bufferDraw= True
palyr.bufferColor= QColor(“white”)
palyr.bufferSize = 1
palyr.scaleVisibility = True
palyr.scaleMax=2000
palyr.isExpression = True
# code is a fieldname
palyr.fieldName = ‘right( code ,3 )’
# palyr.fontMinPixelSize = 20
palyr.size = 20
# data defined label for query “code not like ‘%000’ ¿?
# next line is wrong
#palyr.setDataDefinedProperty(QgsPalLayerSettings.dataDefinedValue, True, True, ‘”code” NOT LIKE \’%000\”, ”)
palyr.textColor = QColor(“red”)
palyr.drawLabels = True
palyr.placement= QgsPalLayerSettings.OverPoint
palyr.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,’8’,”)
palyr.writeToLayer(layer)
iface.mapCanvas().refresh()
Please use http://gis.stackexchange.com for support because it’s a much better platform for Q&A than this blog comment section.