i'd like to know how can i get in QGIS the shortest path from a points layer to points layer, according to field values. In QGIS, I've seen only the function "from vector to single point" and i need "from vector to vector" based on field values (field A). For example, the expected output:

enter image description here

enter image description here

Which geoprocessing, in QGIS, should i use to get the output layer in one shot?

2 Answers 2


If your points are drawn in your desired order (fid) then it's very simple using the points to path tool. If your data is like the screenshot, you don't need the field A, and using the id order you have everything should be fine and you should get your desired output.

If your IDs are not sorted correctly, first you need to re-order them using this topic, and then use the points to path tool.

  • 1
    Thanks for the reply! Using "points to path" i get the shortest path among start points, and not the paths following the route layer, reaching the correspoding end point.
    – Mark
    Commented Nov 26, 2021 at 17:16

What you want to do is not creating new connections, but creating a path using existing connections/line network. Thus this is a classical routing/network task. In QGIS, under Menu Processing > Toolbox > Network, you find several Shortest path algorithms for that. There is no algorithm for layer to layer tasks, but you can easily overcome this limitation by using batch mode.

  1. Open Shortest path (layer to point) algorithm and click batch mode.

  2. For End point, click on Autofill... > Add values by Expression and use this expression: aggregate ('end_layer', 'array_agg', $geometry)

  3. Fill in the first row for all other inputs and use Autofill... > Fill down to copy them to the other rows.

  4. In the resulting layers, delete those connections you don't need:, e.g. keep only the lines with attribute A = 1 and delete those with A = 2. For a bit more sophisticated solution, where only the correct connections (start1/end1) are created, see below.

enter image description here

Variant: creating only the correct connections

If you have a lot of different values for your attribtue A, you don't want to create unnecessary connections that you have to delete afterwards. In this case, with a bit more complex variant, you can only connect start-points 1 with end points 1, start points 2 with end points 2 etc.

  1. Split your start layer with Split Vector Layer algorithm, based on attribute A and be sure to have a consistent naming of the layers - here I use start_1, start_2 etc.

  2. Run Shortest path (layer to point) in batch mode as above

  3. To add the vector layers with start points, use Autofill > Add Values by Expression with this expression to load all layers whosw name begin with start

             @map_layers ,
             when left (layer_property (@element, 'name'),5) = 'start'  -- here you define the conditions which layers should be considered: in this case all layers with a name that starts with the 5 characters "start"
             then @element 
         ), @element is not NULL
  4. To add the vector layers with end points, use Autofill... > Calculate by Expression (attention: this time donf't select Add Values by Expression!) and insert this expression to get the end point with attribute value (number) corresponding to the number contained in the name of the input layer - so start_1 layer will connect to point feature with attribute A = 1 etc.:

             'end',  -- layer name
             'A',  -- attribute name
             regexp_matches (layer_property (@START_POINTS, 'name') , '(\\d)')[0]

The variable @START_POINTS is created automatically, based on the input you made in the start field.

Result, without showing the initial route/network layer for clarity:

enter image description here

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.