Closed swaheera closed 1 year ago
Hi @swaheera,
After reviewing your code I see a couple of issues.
q1 <- st_transform(st_sfc(q1, crs = st_crs(4326)) , st_crs(a))
q2 <- st_transform(st_sfc(q2, crs = st_crs(4326)) , st_crs(a))
as_sfnetwork()
defaults to directed networks. This usually is a tricky setting knowing that many road networks are not correctly digitized. The algorithm doesnot find a path between your points because of the direction of the edges. To avoid this you can use:net <- as_sfnetwork(a, directed = FALSE)
st_network_cost(net, q1, q2)
. This will directly give you the shortest network distance in meters between your two points.If hope you don't mind that I convert this issue to a discussion. If you have any question we can discuss there 😄
I am trying to calculate the driving distance (e.g. based on road networks) between two sets of coordinates.
For example:
To solve this problem, I tried to download the shapefile for the Canadian Road Network and subset it for the Province of Ontario:
Then, by consulting different references (e.g. https://cran.r-project.org/web/packages/sfnetworks/vignettes/sfn01_structure.html) - I tried to calculate the distance between these two points:
But I get the following error:
Error in UseMethod("st_geometry") : no applicable method for 'st_geometry' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
Can someone please show me how to fix this problem? Thanks!