Just wanted to add another answer in case anyone has a similar situation but wants to use the Person layer for some reason or wants a many-to-many join.
You need to use aggregate()
- which returns multiple matches from one point returned as a multigeometry (one to many join), instead of get_feature()
which will only ever return one feature each time (one to one or many to one join).
However, the expression that I got to work was a lot more complex than I thought. You can't use make_line()
to join a single point (from the Persons layer) to multipoints (matching points in Connections layer). So you have to convert the Connections multipoints to an array of single points and then make a line from each Person point to it. I did this using generate_series()
to create an array of number of points and looped through the multipoints using that and array_foreach()
.
collect_geometries(array_foreach(
generate_series(1,num_geometries(
aggregate('connections','collect',$geometry,
"Person_UID"=attribute(@parent,'Person_UID')))),
make_line($geometry,geometry_n(
aggregate('connections','collect',$geometry,
"Person_UID"=attribute(@parent,'Person_UID'))
,@element))))
The bonus is that this works for many-to-many joins as well, where get_feature()
won't work no matter which layer you use it on.
EDIT: The above works if your Persons layer is a Point geometry (not Multipoint).
Your Connections layer can be a Point or Multipoint geometry.
If your Persons layer is a Multipoint geometry then make_line()
using $geometry
won't work - you need to convert your Persons layer to single point either by creating a new layer, or using this even lengthier expression to loop through the multipoint geometries in each Person feature.
Would be nice to have a "promote to single parts" expression in the QGIS expression builder...
collect_geometries(array_foreach(generate_series(1,num_geometries(aggregate('connections','collect',$geometry,"Person_UID"=attribute(@parent,'Person_UID')))),make_line(array_foreach(generate_series(1,num_geometries($geometry)),geometry_n($geometry,@element)),geometry_n(aggregate('connections','collect',$geometry,"Person_UID"=attribute(@parent,'Person_UID')),@element))))