Also as a variation of an answer to How to count points within the current Print composer atlas feature in QGIS 2.8? , from QGIS 2.16 onwards you can list attributes intersecting an atlas feature within the Expression Editor, using the aggregate()
function with the concatenate
calculation and within($geometry,@atlas_geometry)
as the filter.
Click on the 'Insert Expression' button in your text box properties and use an expression like the one below, replacing the attribute/layer name/concatenator (,
) as required.:
aggregate('LAYER_NAME','concatenate',"ATTRIBUTE",
intersects($geometry,@atlas_geometry), ',')
If ATTRIBUTE
field is a numeric datatype you may need to use to_string("ATTRIBUTE")
instead of just plain "ATTRIBUTE"
to allow concatenation.
As a bonus you can add additional filters within the same expression, e.g. within($geometry,@atlas_geometry) AND "PPP" > 50000
for countries within the atlas geometry, where the PPP is also over 50k.
QGIS 3.8 onwards: Using additional aggregate array functions in QGIS 3 you have more flexibility with displaying attributes.
To have your list of concatenated attributes sorted by attribute name (see also this answer), showing only unique values, you could use:
aggregate('LAYER_NAME','concatenate_unique',"ATTRIBUTE",
intersects($geometry,@atlas_geometry), ',', "ATTRIBUTE")
Arrays will let you slice and dice the list which is useful if you have limited space. The expression below will return a comma-separated list of the selected attribute from the first three intersecting features (0,2
).
array_to_string(
array_slice(
aggregate('LAYER','array_agg',"ATTRIBUTE",
intersects($geometry,@atlas_geometry))
,0,2)
,',')
If you want to get attributes from features intersecting the extent of a layout map item (but not necessarily intersecting with the atlas geometry) - for example if your coverage layer is a point layer, or you have a 10% buffer around your atlas feature in Atlas settings - instead of @atlas_geometry
you can extract the extent of the layout map item using item_variables()
as below:
aggregate('LAYER_NAME','concatenate_unique',"ATTRIBUTE",
intersects($geometry,
map_get(item_variables('LAYOUT_MAP_NAME'),'map_extent')), ',', "ATTRIBUTE")
Note you may need to change the name of your layout map item from the default name (e.g. Map 1
)
See below example showing attributes and count of the line features intersecting with the inserts
map item - map_get(item_variables('inserts'),'map_extent')
- even though the atlas itself is driven by a point layer (and @atlas_geometry would be of no use here).
