2

Problem Statement

I have a layer of multiple points, and I am trying to:

  • Aggregate/group the points by their attribute values, so that one point is drawn for each group of points with matching attributes
  • Label the aggregated points, with an aggregate label expression that combines attribute values from the individual points

Example

Here is a layer of several points, of a few cities in California. These points are colored by the greater metropolitan area they are part of (Sacramento, San Francisco, Los Angeles).

simple example dataset of what I'm working with

I would like to dynamically group these points by their metro area, and label these grouped points with each city's name and population, which would look like this:

desired result

Here is the example data/project: https://github.com/Marcus-Richmond/example-datasets/blob/main/Aggregate%20Points.zip

Obstacles

  1. The key here is that I would like to achieve this result dynamically. I would like to avoid creating new layers (although if I had to say, create a virtual layer that grouped the metro areas dynamically, that might be ok).
  2. I've tried the following expression with the geometry generator for the symbol and the labels, but cannot get it to work.

collect($geometry, group_by:="Metro Area")

1 Answer 1

1

Point symbols:

In your categorized renderer change the symbol to geometry generator and use centroid(collect($geometry,"Metro Area")) as expression:

enter image description here

Labels:

Use array_to_string(array_agg("Name" || ' (' || format_number("Population") || ')',group_by:="Metro Area",order_by:="Population"),'\n') as label value. And centroid(collect($geometry,"Metro Area")) as geometry generator point symbol:

enter image description here

Go to rendering and check "Show label" box as data defined override and use array_find(array_agg($id,"Metro Area",order_by:=$id),$id)=0 as expression to avoid duplicated labels:

enter image description here

Result:

enter image description here

1
  • Perfect! Thank you @MrXsquared. I'm hoping to do more complex aggregation of points/labels and this is a huge help in getting me started. I appreciate the help.
    – MarcusR
    Commented Mar 30 at 22:47

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.