Monday, August 26, 2013

Georouting with Rome2rio's API

As I was looking for transit routes for a trip, I stumbled on the Rome2Rio website, which provides tons of results when you query for a trip. It provides directions for several travel modes (flights, trains/buses, driving), and it currently gathers transit info from more providers than Google Maps.

The great thing is they also provide an API, so it's quite straightforward to access their data from R.

Just sign up for an API key on their website, and the following few lines are what you need for a first exploration.

library(RJSONIO)
r2rK <- "<your-API-key-here>"
r2rS <- "<server-address-here>"
 
from <- "<origin-location-here>"
to <- "<destination-location-here>"
 
url_string <- URLencode(paste0("http://", r2rS, "/api/1.2/json/Search?key=", r2rK, "&oName=", from, "&dName=", to, "&currency=EUR"))
 
trip <- fromJSON(paste(readLines(url_string), collapse = ""))
Created by Pretty R at inside-R.org


You need to provide your key and the server address (both provided by Rome2Rio when you sign up), and the origin and destination (simply type a city name, it will be geocoded!).

Check the API documentation if you need help to understand what's inside the trip object.
Note that paths are encoded Google Maps style... but you already know how to deal with that!

No comments:

Post a Comment