1

How do I put the coordinates (x,y) of the end point of the shortest path algorithm (from vector to point) which is part of a QGIS model?

I'd like to get the calculated coordinates of a point (with the field calculator), that is an input of the model, and use this in the end point of the shortest path algorithm.

I want to avoid to put the coordinates as a string input of the model, but use the calculated coordinates of the point in input.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 21, 2022 at 18:14

1 Answer 1

1

You can use this expression in the End point parameter in the Shortest path model algorithm:

attribute(get_feature_by_id('lyr_with_coords', 1), 'coordinates')

Where lyr_with_coords is the output of the Field calculator algorithm.

The expression gets the first (and only in my case) feature from the layer lyr_with_coords, and from that feature it gets the attribute value from the field coordinates (which is the result of an expression that generates the coordinates).

enter image description here

  1. Name the output of the field calculator

enter image description here

  1. In the Shortest path algorithm, change the input for End point from Value to Pre-calculated Value (which allows an expression)

enter image description here

  1. Use the layer name and field name of the Field calculator output in the expression for the End point parameter

enter image description here

enter image description here

answered Jan 21, 2022 at 21:10
10
  • Thanks for the reply....but it doesn't work in my model. Honestly, i don't know why. The process is clear, are you sure that all is correct? Thanks again! Commented Jan 25, 2022 at 11:07
  • Do you get an error? The End point geometry must be very precise, it must be exactly on the network for a route to be calculated. If your calculated coordinates are not, you will have to add a step to find the nearest point on the network from your calculated point. To test I used vertex snapping with a new point layer and just calculated the $x,$y as a shortcut to get come coordinates. Commented Jan 25, 2022 at 11:23
  • No error, simply the paths are not created. I used vertex snapping and it's very precise. I tested this coordinates value also out of the model and the paths are ok. Is it ok that the "shortest path algorithm" is not connected to the "field calculator" block? Commented Jan 25, 2022 at 11:31
  • I added a step in my answer which might solve it. (Change the input type of the End point parameter from Value to Pre-calculated Value). Apologies, I missed that in my answer intially. Commented Jan 25, 2022 at 12:51
  • It's fine that they are not connected. If the Shortest path algorithm was using the output of the Field calculator directly they would be connected, but in this case the model makes a new layer from the field calculator (lyr_with_coords) and adds it to the canvas before picking it up again to use. I couldn't get Shortest path to recognise the output directly wthout making a new layer. Commented Jan 25, 2022 at 13:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.