I would like to label the points of my point layer with the name attributes of the surrounding line features. The result should be a list, with the names of all lines next to the point (f.e. 10 map units). In the screenshot you can see, which result I do expect.
1 Answer
Assuming all your line features are on the same layer, try
aggregate('line_layer_name','concatenate',"line_name_field",intersects($geometry,buffer(geometry(@parent),10)),'\n')
in the label expression for your point feature.
Change 10
to whatever distance you want to pick up features from, and \n
to whatever you want to delimit each line name with (comma, full stop, comma+newline...)
2020 update:
As of QGIS 3.8 you can order the resulting features by any order you want (alphabetically by line_name_field
, or by id
...) by adding it as an extra parameter (order_field
below):
aggregate('line_layer_name','concatenate',"line_name_field",intersects($geometry,buffer(geometry(@parent),10)),'\n',"order_field")
As a bonus, another alternative for more complex list scenarios is to use array_agg
and access the full suite of array functions to slice and dice the list however you want.
For example if you have a very long list and want it to:
- show only distinct values
- be in alphabetical order
- showing just the first 3 and last items
You could use something like this:
array_to_string(
with_variable('label_array',
array_sort(array_distinct(
aggregate('line_layer_name','array_agg',
"line_name_field", intersects($geometry,
buffer(geometry(@parent),10))))),
array_cat(array_slice(@label_array,0,2),
array('...',@label_array[-1]))),
'\n' )`
You could then change array_slice(@label_array,0,2)
to 0,5
or any range you want to easily change the number of items displayed.