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 distances (travel time and kilometric distance) between points and travel time matrices.

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

You can run your own instance of OSRM following guidelines provided here: https://github.com/Project-OSRM/osrm-backend.
The simplest solution is probably the one based on docker containers.

To set the OSRM server, use the osrm.server option: options(osrm.server = "http://address.of.the.server/").
To set the profile ("driving" is set by default and it is the only profile available on the demo server), use the osrm.profile option: options(osrm.profile = "name.of.the.profile").
A typical setup, corresponding to the Docker example, would be:
options(osrm.server = "http://0.0.0.0:5000/", osrm.profile = "driving")

:warning: You must be careful using the OSRM demo server:

"The demo server usage is restricted to reasonable, non-commercial use-cases. We provide no guarantees wrt. uptime, latency, or data updates."

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.024.248.026.915.0
53805763727.10.044.216.422.6
97765707947.641.70.035.036.2
377025401528.716.934.60.017.0
36436333715.420.734.415.90.0

osrmRoute

library(osrm)
library(sf)
library(cartography)
data("berlin")
route <- osrmRoute(src = apotheke.sf[74,], dst = apotheke.sf[55,],
                   overview = "full", returnclass = "sf")
# Display the path
osm <- getTiles(x = route, crop = TRUE, type = "osm", zoom = 13)
tilesLayer(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)

osrmTrip

library(osrm)
library(sf)
library(cartography)
data("berlin")
# Get a trip with a SpatialPointsDataFrame
trips <- osrmTrip(loc = apotheke.sf[10:20,], returnclass="sf")
trip <- trips[[1]]$trip
osm2 <- getTiles(x = trip, crop = TRUE, type = "cartolight", zoom = 11)
tilesLayer(x = 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)

osrmIsochrone

library(osrm)
library(sf)
library(cartography)
data("berlin")
iso <- osrmIsochrone(loc = apotheke.sf[87,], returnclass="sf",
                     breaks = seq(from = 0, to = 14, by = 2), res = 50)
osm3 <- getTiles(x = iso, crop = FALSE, type = "osm", zoom = 12)
tilesLayer(x = osm3)
bks <- sort(c(unique(iso$min), max(iso$max)))
cols <- paste0(carto.pal("turquoise.pal", n1 = length(bks)-1), 80)
choroLayer(x = iso, var = "center", breaks = bks,
           border = NA, col = cols,
           legend.pos = "topleft",legend.frame = TRUE,
           legend.title.txt = "Isochrones\n(min)",
           add = TRUE)
plot(st_geometry(apotheke.sf[87,]), pch = 21, bg = "red", 
     cex = 1.5, add=TRUE)

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,014

Version

3.3.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Timothée Giraud

Last Published

July 12th, 2019

Functions in osrm (3.3.1)

dst

SpatialPointsDataFrame of 10 Communes in France
osrmRoute

Get the Shortest Path Between Two Points
osrmIsochrone

Get Polygons of Isochrones
apotheke.sf

sf POINT of 100 Random Pharmacies in Berlin
osrmTable

Get Travel Time Matrices Between Points
apotheke.sp

SpatialPointsDataFrame of 100 Random Pharmacies in Berlin
apotheke.df

Coordinates of 100 Random Pharmacies in Berlin
com

Communes Coordinates
osrm

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

Get the Travel Geometry Between Multiple Unordered Points
src

SpatialPointsDataFrame of 10 Communes in France