osrm R package

Version Travis-CI Build Status

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. A public API exists but one can run its own instance. This package allows to compute distance (travel time and kilometric distance) between points and travel time matrices.

This package relies on the usage of a running OSRM service (tested with version 5.0.0 of the OSRM API). By default this service is the OSRM public API (http://router.project-osrm.org/). To change the OSRM server, change the osrm.server option: options(osrm.server = "http://address.of.the.server/")

Features

Demo

osrmTable

library(osrm)
# Load data
data("com")
# Travel time matrix
distCom <- osrmTable(loc = com[1:50, c("comm_id","lon","lat")])
# First 5 rows and columns
distCom$durattion[1:5,1:5]
FR3162292119 FR3162212035 BE241011 BE241024 FR3162270520
FR3162292119 0.0 5.4 95.1 91.6 7.5
FR3162212035 4.9 0.0 98.3 94.7 7.1
BE241011 94.2 97.3 0.0 10.4 93.5
BE241024 90.8 93.8 10.4 0.0 90.0
FR3162270520 7.0 6.9 93.5 90.0 0.0

osrmRoute

library(osrm)
# Load data
data("com")

# Travel path between SpatialPointsDataFrame
route <- osrmRoute(src = src[1,], 
                   dst = dst[1,],
                   sp = TRUE)
plot(route, lty = 2, lwd = 2)
plot(src[1,], pch = 20, col = "green", cex = 3, add = TRUE)             
plot(dst[1,], pch = 20, col = "red", cex = 3, add = TRUE) 

osrmTrip

library(osrm)
# Load data
data("com")

# Get a trip with a SpatialPointsDataFrame
trips <- osrmTripGeom(loc = src)

# Map
if(require("cartography")){
  osm <- getTiles(spdf = trips[[1]]$trip, crop = TRUE)
  tilesLayer(osm)
  plot(src, pch = 20, col = "red", cex = 2, add = TRUE)
  plot(trips[[1]]$trip, add = TRUE, lwd=2)
}

osrmIsochrone

library(osrm)
# Load data
data("com")

# Get isochones with a SpatialPointsDataFrame, custom breaks
iso <- osrmIsochrone(loc = src[6,], breaks = seq(from = 0,to = 30, by = 5))

# Map
if(require("cartography")){
  osm <- getTiles(spdf = iso, crop = TRUE)
  tilesLayer(osm)
  breaks <- sort(c(unique(iso$min), max(iso$max)))
  pal <- paste(carto.pal("taupe.pal", length(breaks)-1), "95", sep="")
  
  cartography::choroLayer(spdf = iso, df = iso@data,
                          var = "center", breaks = breaks,
                          border = "grey50", col = pal,
                          legend.pos = "topleft",legend.frame = TRUE, 
                          legend.title.txt = "Isochrones\n(min)", 
                          add = TRUE)
  plot(src[6,], add=T)
}

Installation