QGIS Clustering – How to Display Labels Based on Cluster Size in QGIS
clusteringlabelingqgisqgis-3
In QGIS 3.0, I am using the new clustering style.
I want to display labels based on cluster size.
What I tried in expression field:
case
when @cluster_size>5 then "label_name"
end
Best Answer
The variables @cluster_color and @cluster_size only exist in the context of the cluster renderer. They cannot be used in expressions for labeling. To label point clusters based on these variables, we can use a font marker as part of the cluster symbol.
Change the Cluster symbol in layer style panel:
Add a new symbol layer, symbol layer type: font marker (1)
Add an offset so the font marker appears to the side of the marker instead of directly on top of it (2)
Use data-defined settings to control the text of the font marker (3)
Use an expression like
if(@cluster_size>5, "label_name", '')
The third parameter in the above expression is two single quotation marks, not one double quotation mark. This represents an empty string, resulting in no label being displayed when the "label_name" field has a null value.
I originally used the expression if(@cluster_size>5, "label_name", null).
With this expression, a default text label (A) will be displayed when the field value is null.
It seems as though the point cluster implementation does not allow for this natively.
Consider the point cluster renderer - it essentially analyzes and clusters point data at render time, creating a new pseudo layer. This layer is not accessible to the user. One cannot query a clustered point. One cannot field calculate a @cluster_size attribute.
The point cluster renderer does inherit render order from the source using Control feature rendering order (QgsFeatureRequest::OrderByClause) leaving open the possibility of some clever workaround using attributes and expressions, however certain ordering schemes are not guaranteed to be consistent.
As an example, consider a point layer with rendering ordered ascending by a "Latitude" attribute. The more northerly points are rendered atop more southerly points. The point cluster renderer does not have a "Latitude" attribute for its new features, though it will inherit some semblance of that order from the source. There can be instances when a more southerly cluster renders atop a more northerly cluster, because of the order of the original points.
Best Answer
The variables
@cluster_color
and@cluster_size
only exist in the context of the cluster renderer. They cannot be used in expressions for labeling. To label point clusters based on these variables, we can use a font marker as part of the cluster symbol.Change the Cluster symbol in layer style panel:
Add a new symbol layer, symbol layer type: font marker (1)
Add an offset so the font marker appears to the side of the marker instead of directly on top of it (2)
Use data-defined settings to control the text of the font marker (3)
Use an expression like
The third parameter in the above expression is two single quotation marks, not one double quotation mark. This represents an empty string, resulting in no label being displayed when the "label_name" field has a null value.
I originally used the expression
if(@cluster_size>5, "label_name", null)
. With this expression, a default text label (A) will be displayed when the field value is null.