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.17.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), 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("com")
# Travel time matrix
distCom <- osrmTable(loc = com[1:50, c("name","lon","lat")])
# First 5 rows and columns
distCom$duration[1:5,1:5]
osrmRoute
library(osrm)
library(osrm)
# Load data
data("com")
# Travel path between SpatialPointsDataFrame
route <- osrmRoute(src = src[1,], dst = dst[1,], sp = TRUE)
if(require("cartography")){
osm <- getTiles(spdf = route, crop = TRUE, type = "osmtransport")
tilesLayer(osm)
plot(route, lwd = 5, col = "blue", add = TRUE)
plot(src[1,], pch = 20, col = "green", cex = 5, add = TRUE)
plot(dst[1,], pch = 20, col = "red", cex = 5, add = TRUE)
dev.off()
}
osrmTrip
library(osrm)
library(sp)
# Load data
data("com")
# Get a trip with a SpatialPointsDataFrame
trips <- osrmTrip(loc = src)
# Map
if(require("cartography")){
osm <- getTiles(spdf = trips[[1]]$trip, crop = TRUE, type = "osmtransport")
tilesLayer(osm)
plot(trips[[1]]$trip, add = TRUE, col = 1:5, lwd = 5)
plot(src, pch = 21, bg = "red", cex = 2, col = "black", add = TRUE)
}
osrmIsochrone
library(osrm)
library(sp)
# 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, type = "osmtransport")
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", lwd = 0.5, col = pal,
legend.pos = "topleft",legend.frame = TRUE,
legend.title.txt = "Driving Time\nto Renescure\n(min)",
add = TRUE)
plot(src[6,], cex = 2, pch = 20, col ="red", add=T)
text(src[6,], label = "Renescure", pos = 3)
}
Installation
- Development version on GitHub
require(devtools)
devtools::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.