I have a road network shapefile and list of points. I have to create a route from the list of points and then overlay/ spatially join (integrate the attributes of points that are overlaying the road segments)
The sample road network shape file can be found here https://drive.google.com/drive/folders/103Orz6NuiWOaFoEkM18SlzFTjGYi1rju?usp=sharing
The following is the code for points with lat (x) and long (y) information. The "order" column means, the order of destinations in the route .
points <-tribble (
~x,~y, ~order,
78.14358, 9.921388,1,
78.14519, 9.921123,2,
78.14889, 9.916954,3,
78.14932, 9.912807,4,
78.14346, 9.913828,5,
78.13490, 9.916551,6,
78.12904, 9.918782,7
)
What I want as an output is a layer of the route joining all the points in the order as mentioned. And I also want to integrate/ do a spatial join of the route to the road segments.
Thanks in advance
The following answer is based on the
R
packagesfnetworks
which can be installed as follows:First of all, load packages
and data. The points object is converted to
sf
format.Plot network and points (just to understand the problem a little bit better)
Convert roads to sfnetwork object
Subdivide edges and select the main component. Check https://luukvdmeer.github.io/sfnetworks/articles/preprocess_and_clean.html for more details.
Now I want to estimate the shortest paths between each pair of consecutive points.
sfnetwork
does not support many-to-many routing, so we need to define a for-loop. If you need to repeat this operation for several points, I think you should check the R packagedodgr
.Extract the id of the edges that compose all shortest paths
Hence, if you want to extract the edges from the original network
Plot network and points
Created on 2021-03-07 by the reprex package (v0.3.0)