I have a set of hubs and spokes and want to connect each spoke to its three nearest hubs. I used the distance matrix method (Returning Multiple Nearest Neighbors) and now I have a (MultiPoint) shapefile that looks like this:

enter image description here

For each BezirkNr (spoke) I have 3 entries KnotenNr (hub) with their respective distance. Now I would like to draw a line between each of these pairs to get a shp-file in the end. I have been trying with Join by lines (hub lines) and MMQGIS (hub) but haven't been able to find a method to connect all 3 entries of each BezirkNr.


2 Answers 2


I assume it is not a multipoint layer, but a "normal"point and you have joined them before using "join by nearest", so you have Bezirke and Knoten as separate layers?

If so the fastest solution I could think of is the following (there may be other, more intuitive ones of course):

  1. Start geometry by expression tool
  2. Select your joined layer as input
  3. Select line as output geometry type
  4. Enter the following expression:

    make_line($geometry, geometry(get_feature_by_id('KnotenLayerName',"KnotenNr")))

replace 'KnotenLayerName' with the name of your Knoten-Layer and "KnotenNr" with the fieldname containing the ids. Alternatively to get_feature_by_id() you can also try get_feature(layer,attribute,value).

enter image description here

  • It actually was a MultiPoint layer because I used the "Distance Matrix" tool. I now tried your solution and it would most probably work fine, I just get the error in the expression, because it will not find my column: Column 'KNr_Visum' not found. I double checked the layer name and the column name, I have no idea where the error is to find. Commented Apr 22, 2020 at 7:25
  • Did you type it in single or doublequotes?
    – MrXsquared
    Commented Apr 22, 2020 at 8:10
  • Doublequotes as in your example (the error message shows it in single quotes though). Commented Apr 22, 2020 at 8:22
  • Hard to tell remotely. Another idea: Is it a joined column eventually?
    – MrXsquared
    Commented Apr 22, 2020 at 8:25
  • No it's not. Other columns are joined but not this one. When I change the input layer to "KnotenLayerName" I don't get the error message in the expression. It seems as if QGIS is expecting the layer name from the input layer? But then again, that's why we put "KnotenLayerName" into the 'get_feature_by_id' and you showed me that it should work like this... Commented Apr 22, 2020 at 9:47

I followed @MrXsquared first steps and used the "join by nearest" tool with "3" nearest set.

I assume it is not a multipoint layer, but a "normal"point and you have joined them before using "join by nearest", so you have Bezirke and Knoten as separate layers?

Afterwards I used the "Join by lines (hub lines)" tool with the "KnotenLayerName" as Hub and "KnotenNr" as Hub ID field.

As spoke layer I used the joined layer from the first step with the equivalent field as Spoke ID field and it worked.

Now I have 3 lines from each spoke to its 3 nearest hubs.

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.