From 09456a399facad208c630e3d2a2bb78c1b60ecd5 Mon Sep 17 00:00:00 2001 From: Robin Lovelace Date: Wed, 8 Jul 2020 01:27:01 +0100 Subject: [PATCH] Update routing vignette --- vignettes/stplanr-routing.Rmd | 67 ++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/vignettes/stplanr-routing.Rmd b/vignettes/stplanr-routing.Rmd index ea1b71b3..c03b829c 100644 --- a/vignettes/stplanr-routing.Rmd +++ b/vignettes/stplanr-routing.Rmd @@ -35,12 +35,31 @@ piggyback::pb_upload("roads_iow.geojson") roads_iow = sf::read_sf("https://github.com/ropensci/stplanr/releases/download/0.3.1/roads_iow.geojson") ``` -# osrm +# OSRM + +Routing services such as OpenStreetMap Routing Machine (OSRM) require an input network, usually from OSM. + +We will use the `osrm` package: + +```{r} +library(osrm) +``` + +To get OSM data for the Isle of Wight, for example, you can run the following commands: + +```{r, eval=FALSE} +remotes::install_github("itsleeds/geofabrik") +library(geofabrik) +iow = get_geofabrik(name = "Isle of Wight") +f = gf_filename("Isle of Wight") +file.copy(f, "iow.pbf") +options(osrm.server = "http://0.0.0.0:5000/", osrm.profile = "driving") +``` + ```{r, echo=FALSE, eval=FALSE} if(!file.exists("iow.pbf")) download.file("http://download.geofabrik.de/europe/great-britain/england/isle-of-wight-latest.osm.pbf", "iow.pbf") -options(osrm.server = "http://0.0.0.0:5000/", osrm.profile = "driving") ``` Then in bash run the following commands to make the [OSRM docker image](https://hub.docker.com/r/osrm/osrm-backend/) work for you. @@ -55,27 +74,49 @@ curl "http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.49 Now we can do routing in R! -```{r, eval=FALSE} +On a single route: + +```{r} l = pct::wight_lines_30 p = line2points(l) -r = osrm::osrmRoute(src = p[1, ], dst = p[2, ], returnclass = "sf") +r = osrm::osrmRoute(src = p[1, ], dst = p[2, ], returnclass = "sf", overview = "full") plot(r) ``` +And to find many routes via the `route()` function, resulting in something like the figure below. + +```{r, eval=FALSE} +routes_osrm = route(l = l, route_fun = osrmRoute, returnclass = "sf", overview = "full") +rnet_osrm = overline(routes_osrm, attrib = "bicycle") +mapview::mapview(rnet_osrm, lwd = rnet_osrm$bicycle / 10) +``` + +```{r, eval=FALSE, echo=FALSE} +system.time({ + routes_osrm = route(l = l, route_fun = osrmRoute, returnclass = "sf", overview = "full") +}) +30 / 0.9 # around 30 routes per second +saveRDS(routes_osrm, "routes_osrm.Rds") +piggyback::pb_upload("routes_osrm.Rds") +``` + + +```{r, echo=FALSE} +knitr::include_graphics("https://user-images.githubusercontent.com/1825120/86858225-2970df80-c0b8-11ea-8394-07f98f1c8e8a.png") +``` + ```{r, eval=FALSE} -route_osrm2 = function(l) { - p = line2points(l) - s = (1:nrow(l)) * 2 - 1 - list_out = lapply(s, function(i) osrm::osrmRoute(p[i, ], dst = p[i + 1, ], returnclass = "sf")) - do.call(rbind, list_out) -} -routes_osrm = route_osrm2(l) -plot(routes_osrm) # tidy up -file.remove("iow.pbf") +f = list.files(pattern = "iow") +unlink(x = f, recursive = TRUE) ``` +Shut down the docker container. +```{r, eval=FALSE, engine='zsh'} +docker ps +docker stop stupefied_hopper +```