Learn R Programming

⚠️There's a newer version (4.1.1) of this package.Take me there.

osrm R package

Interface Between R and the OpenStreetMap-Based Routing Service OSRM

Description

OSRM is a routing service based on OpenStreetMap data. See http://project-osrm.org/ for more information. This package allows to compute routes, trips, isochrones and travel distances matrices (travel time and kilometric distance).

This package relies on the usage of a running OSRM service (tested with v5.23.0 of OSRM).

You can run your own instance of OSRM following guidelines provided here. The simplest solution is probably the one based on docker containers.

:warning: You must be careful using the OSRM demo server and read the about page of the service:
> One request per second max. No scraping, no heavy usage.

:heavy_exclamation_mark: To consider when using OSRM:
> “Most of the previously active core devs have either moved on to new roles, or are simply busy on different projects (…)”

Features

  • osrmTable Get travel time matrices between points.

  • osrmRoute Get the shortest path between two points.

  • osrmTrip Get the travel geometry between multiple unordered points.

  • osrmIsochrone Get polygons of isochrones.

Demo

osrmTable

library(osrm)
data("berlin")
# Travel time matrix
distA <- osrmTable(loc = apotheke.sf[1:5,])
distA$durations
4403386665380576379776570793770254015364363337
4403386660.021.534.119.711.8
53805763722.90.042.016.120.7
97765707934.042.60.030.527.3
377025401522.415.329.70.012.7
36436333712.120.226.712.00.0

osrmRoute

library(sf)
library(maptiles)
# Route
route <- osrmRoute(src = apotheke.sf[74,], dst = apotheke.sf[55,],
                   overview = "full", returnclass = "sf")
# Display
osm <- get_tiles(x = route, crop = TRUE, zoom = 13)
png("img/route.png", width = 693, height = 263)
par(mar = c(0,0,0,0))
plot_tiles(osm)
plot(st_geometry(route), lwd = 4, add = TRUE)
plot(st_geometry(route), lwd = 1, col = "white", add = TRUE)
plot(st_geometry(apotheke.sf[c(74,55),]), pch = 20, col = "red", add = TRUE)
mtext(get_credit("OpenStreetMap"), side = 1, line = -1, cex = .9, adj = .99)
dev.off()

osrmTrip

# Trip 
trips <- osrmTrip(loc = apotheke.sf[10:20,], returnclass="sf")
trip <- trips[[1]]$trip
# Display
osm2 <- get_tiles(x = trip, crop = TRUE, zoom = 11)
png("img/trip.png", width = 499, height = 420)
par(mar = c(0,0,0,0))
plot_tiles(osm2)
plot(st_geometry(trip), col = "black", lwd = 4, add = TRUE )
plot(st_geometry(trip), col = c("red", "white"), lwd = 1, add=TRUE)
plot(st_geometry(apotheke.sf[10:20,]), pch = 21, bg = "red", cex = 1.5, add=TRUE)
mtext(get_credit("OpenStreetMap"), side = 1, line = -1, cex = .9, adj = .99)
dev.off()

osrmIsochrone

iso <- osrmIsochrone(loc = apotheke.sf[87,], returnclass="sf",
                     breaks = seq(from = 0, to = 14, by = 2), res = 70)
osm3 <- get_tiles(x = iso, crop = TRUE, zoom = 12)
bks <- sort(c(unique(iso$min), max(iso$max)))
library(cartography)
cols <- paste0(carto.pal("turquoise.pal", n1 = length(bks)-1), 'BF')
png("img/iso.png", width = 604, height = 595)
par(mar = c(0,0,0,0))
plot_tiles(osm3)
choroLayer(x = iso, var = "center", breaks = bks,
           border = NA, col = cols,
           legend.pos = "topleft",legend.frame = TRUE,
           legend.title.txt = "Isochrones\n(min)",
           legend.title.cex = 1, legend.values.cex = .8,
           add = TRUE)
plot(st_geometry(apotheke.sf[87,]), pch = 21, bg = "red", 
     cex = 1.5, add=TRUE)
mtext(get_credit("OpenStreetMap"), side = 1, line = -1, cex = .9, adj = .99)
dev.off()

Installation

  • Development version on GitHub
remotes::install_github("rCarto/osrm")
  • Stable version on CRAN
install.packages("osrm")

Community Guidelines

One can contribute to the package through pull requests and report issues or ask questions here.

Copy Link

Version

Install

install.packages('osrm')

Monthly Downloads

2,175

Version

3.4.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Timothée Giraud

Last Published

December 9th, 2020

Functions in osrm (3.4.0)

osrmIsometric

Get Polygons of Isodistances
osrmIsochrone

Get Polygons of Isochrones
osrmTrip

Get the Travel Geometry Between Multiple Unordered Points
osrmRoute

Get the Shortest Path Between Two Points
apotheke.sf

sf POINT of 100 Random Pharmacies in Berlin
apotheke.df

Coordinates of 100 Random Pharmacies in Berlin
osrm

Shortest Paths and Travel Time from OpenStreetMap via an OSRM API
osrmTable

Get Travel Time Matrices Between Points
apotheke.sp

SpatialPointsDataFrame of 100 Random Pharmacies in Berlin