Transport routing with stplanr


This vignette is work in progress - watch this space!

roads_iow = sf::read_sf("")


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

We will use the osrm package:

#> Data: (c) OpenStreetMap contributors, ODbL 1.0 -
#> Routing: OSRM -

To get OSM data for the Isle of Wight, for example, you can run the following commands:

iow = get_geofabrik(name = "Isle of Wight")
f = gf_filename("Isle of Wight")
file.copy(f, "iow.pbf")
options(osrm.server = "", osrm.profile = "driving")

Then in bash run the following commands to make the OSRM docker image work for you.

docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-extract -p /opt/car.lua /data/iow.pbf
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-partition /data/iow.osrm
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-customize /data/iow.osrm
docker run -t -i -p 5000:5000 -v "${PWD}:/data" osrm/osrm-backend osrm-routed --algorithm mld /data/iow.osrm
curl ",52.517037;13.385983,52.496891?steps=true"

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")

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.

docker ps
docker stop stupefied_hopper