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

:warning: You should be careful using the OSRM demo server, it is not always very stable :bangbang:

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 a SpatialPolygonsDataFrame of isochrones.

Demo

osrmTable

library(osrm)
# Load data
data("berlin")
# Inputs are data frames
# Travel time matrix
distA <- osrmTable(loc = apotheke.df[1:50, c("id","lon","lat")])
# First 5 rows and columns
distA$durations[1:5,1:5]
4403386665380576379776570793770254015364363337
4403386660.026.245.425.013.8
53805763726.80.044.417.120.8
97765707945.443.10.037.435.9
377025401528.817.835.00.014.5
36436333717.724.734.713.80.0

osrmRoute

library(osrm)
library(sp)
library(cartography)
# Load data
data("berlin")
route <- osrmRoute(src = c("A", 13.23889, 52.54250),
                   dst = c("B", 13.45363, 52.42926),
                   sp = TRUE, overview = "full")
# Display the path
osm <- getTiles(x = route, crop = TRUE, type = "osm")
tilesLayer(osm)
plot(route, lty = 1,lwd = 4, asp = 1, add=TRUE)
plot(route, lty = 1, lwd = 1, col = "white", add=TRUE)
points(x = c(13.23889, 13.45363), y = c(52.54250,52.42926), 
       col = "red", pch = 20, cex = 1.5)
text(x = c(13.23889, 13.45363), y = c(52.54250,52.42926), 
     labels = c("A","B"), pos = c(1,2))

osrmTrip

library(osrm)
library(sp)
library(cartography)
# Load data
data("berlin")
# Get a trip with a SpatialPointsDataFrame
trips <- osrmTrip(loc = apotheke.sp[10:20,])
# Map
osm <- getTiles(x = trips[[1]]$trip, crop = TRUE,
                type = "cartolight", zoom = 11)
tilesLayer(x = osm)
plot(trips[[1]]$trip, col = "black", lwd = 4, add=T)
plot(trips[[1]]$trip, col = c("red", "white"), lwd = 1, add=T)
plot(apotheke.sp[10:20,], pch = 21, bg = "red", cex = 1.5, add=T)

osrmIsochrone

library(osrm)
library(sp)
library(cartography)
# Load data
data("berlin")
# Get isochones with a SpatialPointsDataFrame, custom breaks
iso <- osrmIsochrone(loc = apotheke.sp[10,],
                     breaks = seq(from = 0, to = 14, by = 2), res = 50)
# Map
osm <- getTiles(x = iso, crop = FALSE, type = "osm", zoom = 13)
tilesLayer(x = osm)
bks <- sort(c(unique(iso$min), max(iso$max)))
cols <- paste0(carto.pal("turquoise.pal", n1 = length(bks)-1), 80)
choroLayer(spdf = iso,
           var = "center", breaks = bks,
           border = NA, col = cols,
           legend.pos = "topleft",legend.frame = TRUE,
           legend.title.txt = "Isochrones\n(min)",
           add = TRUE)
plot(apotheke.sp[10,], add=TRUE, col ="red", pch = 20)

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.2.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Timothée Giraud

Last Published

January 24th, 2019

Functions in osrm (3.2.0)

com

Communes Coordinates
apotheke.df

Coordinates of 100 Random Pharmacies in Berlin
apotheke.sp

SpatialPointsDataFrame of 100 Random Pharmacies in Berlin
osrmTrip

Get the Travel Geometry Between Multiple Unordered Points
src

SpatialPointsDataFrame of 10 Communes in France
osrm

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

Get a SpatialPolygonsDataFrame of Isochrones
osrmRoute

Get the Shortest Path Between Two Points
osrmTable

Get Travel Time Matrices Between Points
dst

SpatialPointsDataFrame of 10 Communes in France