The vignette introduces some of the more advanced features of OTP, and gives some examples of the types of analysis that are possible when using OTP and R together.
For this vignette we will use the same data as the getting_started
vignette. If you have not yet created the example graph you can set it up with the following commands. If you are using non-default settings see the getting_started
vignette for full details.
library(opentripplanner) # Path to a folder containing the OTP.jar file, change to where you saved the file. path_otp <- "C:/otp" path_data <- system.file("extdata", package="opentripplanner") log <- otp_build_graph(otp = path_otp, dir = path_data) otp_setup(otp = path_otp, dir = path_data, memory = 1.5) otpcon <- otp_connect()
Now that we have a working instance of OTP, we'll use it to generate some travel-time isochrones. We are interested in visualising how long it takes to access Manchester Airport using public transport from different parts of Greater Manchester. We will do this by requesting isochrones from OTP for 15, 30, 45, 60, 75 and 90 minutes. This can be achieved with a single function otp_isochrone()
.
airport_current <- otp_isochrone(otpcon = otpcon, fromPlace = c(53.36367, -2.27190), # latlong of Manchester Airport mode = c("WALK","TRANSIT"), cutoffSec = c(15, 30, 45, 60, 75, 90) * 60 ) # Cut offs in seconds airport_current$minutes = airport_current$time / 60 # Convert back to minutes
We can visualise the isochrones on a map using the tmap
package.
library(tmap) # Load the tmap package tmap_mode("view") # Set tmap to interative viewing map <- tm_shape(airport_current) + # Build the map tm_fill("minutes", breaks = c(0, 15.001, 30.001, 45.001, 60.001, 75.001, 90.001), style = "fixed", palette = "-RdYlBu") + tm_borders() map # Plot the map
You should see a map like this.
```rIsochrones from Manchester Airport"} knitr::include_graphics("images/isochrone.jpg")
## Batch Routing In this final part of the tutorial, we are going to automate querying the OTP API to generate large quantities of route data - potentially for many thousands of origin:destination pairs. In this example, we will gather data on accessing Manchester Airport using public transport for each of the LSOAs in Greater Manchester. To do this we will be using the `otp_route_batch` function. We'll start by importing the sample data `gm-lsoa-centroids.Rds` ```r lsoa <- readRDS(system.file("data/gm-lsoa-centroids.Rds", package="opentripplanner")) head(lsoa)
otp_plan_batch
requires the fromPlace and the toPlace to each be 2 x m matrices where each row is a latitude/longitude pair.
toPlace <- matrix(rep(c(53.36367, -2.27190),nrow(lsoa)), ncol = 2, byrow = TRUE) head(toPlace) fromPlace <- matrix(c(lsoa$lat,lsoa$lng), ncol = 2, byrow = FALSE) head(fromPlace)
Now we can use the otp_plan_batch
to find the routes
routes <- otp_plan_batch(otpcon = otpcon, fromPlace = fromPlace, toPlace = toPlace)
You may get some error message returned as OTP is unable to find some of the routes. The otp_plan_batch
will skip over errors and return all the routes it can get. It will then print out any error messages. You will have also noticed the handy progress bar.
You can plot the routes using tmap
Note: you may find that plotting so many routes overloads your computer
If you do plot all the routes it should look something like this:
rDriving Routes to Manchester Airport"}
knitr::include_graphics("images/routes_to_airport.jpg")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.