openrouteservice goes R

Great news for all R enthusiasts: we have developed a package to facilitate querying openrouteservice API from R. The latest version can be installed directly from GitHub with devtools::install_github("GIScience/openrouteservice-r").

It’s really easy to get started. For example, finding a route from Heidelberg to Kraków takes just a few lines of code. And with the help of leaflet it’s even possible to visualize the results directly in R. Neat!

library("openrouteservice")

# one-time API key set-up
# ors_api_key("<your-api-key>")

# query for coordinates
locations <- lapply(c("Heidelberg", "Kraków"), ors_geocode)
coordinates <- lapply(locations, function(x) x$features[[1]]$geometry$coordinates)

# find route 
route <- ors_directions(coordinates, format="geojson")

# route length in kilometres and duration in hours
unlist(route$features[[1]]$properties$summary) / c(1000, 3600)
##    distance    duration 
## 1051.861300    9.205167

# draw on map using leaflet
library(leaflet)

leaflet() %>%
  addTiles() %>%
  addGeoJSON(route, fill=FALSE) %>%
  fitBBox(route$bbox)

For more examples and an overview of the offered functionality see package vignette.