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.
1 Answer 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).
- Name the output of the field calculator
- In the
Shortest path
algorithm, change the input forEnd point
fromValue
toPre-calculated Value
(which allows an expression)
- Use the layer name and field name of the
Field calculator
output in the expression for theEnd point
parameter
-
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!Mark– Mark2022年01月25日 11:07:49 +00:00Commented 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.Matt– Matt2022年01月25日 11:23:01 +00:00Commented 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?Mark– Mark2022年01月25日 11:31:18 +00:00Commented 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 fromValue
toPre-calculated Value
). Apologies, I missed that in my answer intially.Matt– Matt2022年01月25日 12:51:59 +00:00Commented 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 getShortest path
to recognise the output directly wthout making a new layer.Matt– Matt2022年01月25日 13:01:16 +00:00Commented Jan 25, 2022 at 13:01
Explore related questions
See similar questions with these tags.