0

I am using the sfnetworks package of R to find the shortest path between points on a street network. The street network is imported from a shapefile and converted to a graph (using 'as_sfnetwork'). The output of my script is a dataframe (that I'm writing to a csv) which has a row for each pair of points that I am routing between.

The columns of the output dataframe are: | ID_from | ID_to | used_edges | used_nodes |

The script works and I'm getting the correct output (checked by plotting). used_edges gives me a list of the edges' ids that are part of the shortest path (as shown in the screenshot below). used_nodes works similarly but is not important to me.

Current output of the script with the graph's edges' id instead of the ids from the shapefile.


So, to get to my problem: the used_edges column contains information on the graph's edges' IDs while I actually require information on the edges' ID in the original shapefile. In the shapefile (my_shp.shp) the IDs I require are stored in the attribute 'LINK_ID'.

Is there a way I can replace the edges' ids from the graph with those from the shapefile? Might be a relatively simple thing, but I'm really biting my teeth out here.

Any help would be very welcome!

Py Grammer
  • 11
  • 1
  • Welcome. Please read [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to ask a good question on R. In particular, please edit your question to include the code that you've written so far and ouput of ``dput(data)`` - this will make it much easier for others to help you. Thanks. – user438383 Jun 20 '21 at 08:22
  • Hi, I agree with the comment above, the code you have tested and an excerpt of the data would really help. There are many ways I can think of to solve your issue, e.g. using the [to_spatial_shortest_paths morpher](https://luukvdmeer.github.io/sfnetworks/articles/morphers.html#to-spatial-shortest-paths) which subsets the network to only the nodes and edges part of the shortest path keeping the columns. Another way would be to use your resulting df to subset your network based on the used_edges column, first activating the edges, then slicing the network, and then pulling the column you want ;) – loreabad6 Jul 04 '21 at 10:28

0 Answers0