Introduction

Routing is the process of identifying routes that enable movement between two geographic locations along the shortest path (based on mode-specific routing profiles) or in some other ‘optimal’ way, based on route network data. Most open routing engines rely on OpenStreetMap (OSM) data.

We will use the example of the Isle of Wight to demonstrate routing engines. To get OSM data for the Isle of Wight you can run the following commands:

remotes::install_github("itsleeds/geofabrik")
library(geofabrik)
roads_iow = get_geofabrik(name = "Isle of Wight")
f = gf_filename("Isle of Wight")
file.copy(f, "iow.pbf")
options(osrm.server = "https://0.0.0.0:5000/", osrm.profile = "driving")

OSRM

Routing services such as OpenStreetMap Routing Machine (OSRM) require an input network, usually from OSM.

We will use the osrm package:

library(osrm)
#> Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
#> Routing: OSRM - http://project-osrm.org/

In the system terminal run the following commands to make the OSRM docker image work for you.

Now we can do routing in R!

On a single route:

l = pct::wight_lines_30
p = line2points(l)
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.

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)

# tidy up
f = list.files(pattern = "iow")
unlink(x = f, recursive = TRUE)

Shut down the docker container.