UrbanAnalyst / dodgr

Distances on Directed Graphs in R
https://urbananalyst.github.io/dodgr/
127 stars 16 forks source link

dodgr_dists function returning a matrix with less rows than provided in 'from' argument #199

Closed sindysan closed 1 year ago

sindysan commented 1 year ago

Hi, apologies if this is a stupid question - been stumped on this all evening!

I am attempting to calculate a nearest distance matrix between schools and public libraries in London.

Here is my code:

# London road networks data downloaded from http://download.openstreetmap.fr/extracts/europe/united_kingdom/england/ 
london_roads_nodes <- oe_read("Data/greater_london-latest.osm.pbf", layer = "points")
london_roads_nodes <- london_roads_nodes[,"osm_id"]

london_roads_edges <- oe_vectortranslate("Data/greater_london-latest.osm.pbf", layer = "lines", extra_tags = c("maxspeed", "oneway"))
london_roads_edges <- oe_read(london_roads_edges)
london_roads_edges <- london_roads_edges[london_roads_edges$highway %in% c("primary", "secondary", "tertiary", "# London road networks data downloaded from http://download.openstreetmap.fr/extracts/europe/united_kingdom/england/ 
london_roads_nodes <- oe_read("Data/greater_london-latest.osm.pbf", layer = "points")
london_roads_nodes <- london_roads_nodes[,"osm_id"]

london_roads_edges <- oe_vectortranslate("Data/greater_london-latest.osm.pbf", layer = "lines", extra_tags = c("maxspeed", "oneway"))
london_roads_edges <- oe_read(london_roads_edges)
london_roads_edges <- london_roads_edges[london_roads_edges$highway %in% c("primary", "secondary", "tertiary", "residential", "path", "footway", "unclassified", "living_street", "pedestrian"),]
london_roads_edges <- london_roads_edges[, c("osm_id", "name", "highway", "oneway")]
london_roads_edges$oneway <- "no"

# get bounding box coordinates for London
getbb("london uk")
bbox <- c(-0.5103751,51.2867601,0.3340155,51.6918741)
# download London schools and libraries data from OSM
schools <- opq(bbox = bbox) %>%
  add_osm_feature(key = "amenity", value = "school") %>%
  osmdata_sf()
libraries<- opq(bbox = bbox) %>%
  add_osm_feature(key = "amenity", value = "library") %>%
  osmdata_sf()

# extract school and library points
london_libraries <- libraries$osm_points[, c("osm_id", "name")]
london_schools <- schools$osm_points[, c("osm_id", "name")]

graph <- weight_streetnet(london_roads_edges, wt_profile = "foot")
graph_connected <- graph[graph$component == 1, ]

school_to_library_calc <- dodgr_dists(graph_connected, from = st_coordinates(london_schools),
                                          to = st_coordinates(london_libraries), shortest = TRUE, 
                                          pairwise = FALSE, quiet = FALSE, parallel = TRUE)residential", "path", "footway", "unclassified", "living_street", "pedestrian"),]
london_roads_edges <- london_roads_edges[, c("osm_id", "name", "highway", "oneway")]
london_roads_edges$oneway <- "no"

# get bounding box coordinates for London
getbb("london uk")
bbox <- c(-0.5103751,51.2867601,0.3340155,51.6918741)
# download London schools and libraries data from OSM
schools <- opq(bbox = bbox) %>%
  add_osm_feature(key = "amenity", value = "school") %>%
  osmdata_sf()
libraries<- opq(bbox = bbox) %>%
  add_osm_feature(key = "amenity", value = "library") %>%
  osmdata_sf()

# extract school and library points
london_libraries <- libraries$osm_points[, c("osm_id", "name")]
london_schools <- schools$osm_points[, c("osm_id", "name")]

graph <- weight_streetnet(london_roads_edges, wt_profile = "foot")
graph_connected <- graph[graph$component == 1, ]

school_to_library_calc <- dodgr_dists(graph_connected, from = st_coordinates(london_schools),
                                          to = st_coordinates(london_libraries), shortest = TRUE, 
                                          pairwise = FALSE, quiet = FALSE, parallel = TRUE)

The school_to_library_calc matrix has nrows less than the nrows in london_schools - I can't tell if I'm doing something wrong!

mpadge commented 1 year ago

Thanks for the reproducible code @sindysan! I just ran it locally, and got the expected result. 53,171 schools to 2,965 libraries gives a [53171, 2965] distance matrix. Maybe just update your version of dodgr to the latest (remotes::install_github)?

sindysan commented 1 year ago

Thank you @mpadge - I was already using the latest version of dodgr however I've just run my code again and it worked... no idea what happened but happy it's sorted! Thanks for looking at this. :)

mpadge commented 1 year ago

Great news! Thanks for using the package - and really pushing it to extremes with a query of that size!! Impressive that it works at all!