Drivetime analysis

A ‘drive time’ describes how far you can drive (i.e in a car on a public road) in a certain amount of time.

To get started, let’s install and load packages.

# Loads the required required packages
pacman::p_load(
  ggmap,
  tmaptools,
  RCurl,
  jsonlite,
  tidyverse,
  leaflet,
  writexl,
  readr,
  readxl,
  sf,
  mapview,
  rgdal,
  osrm
)

OSRM isochrones

Useful links for further reading: Source 1, Source 2

The OSRM package (Github) pulls from OpenStreetMap to find travel times based on location.

The downside is that the polygons it generates are pretty chunky… i.e. it doesn’t take into account major roads and streets as the key tributaries/arteries of a city area. We can get around this a bit by dialing up the ‘res’ (i.e. the resolution) in the osrmIsochrone function… but it’s only a partial solution.

Uncomment the leaflet function below to view an interactive map.

# Create a dataframe with the latitude and longitude
locations <- tibble::tribble(
  ~place, ~lon, ~lat,
  "Melbourne", 144.9631, -37.8136
)

# Run it through the osrm package to calculate isochrones
iso <- osrmIsochrone(
  loc = c(locations$lon, locations$lat),
  breaks = seq(from = 0, to = 30, by = 5),
  res = 50
)

# Create an interactive map
# leaflet() %>%
#   setView(mean(locations$lon), mean(locations$lat), zoom = 7) %>%
#   addProviderTiles("CartoDB.Positron", group = "Greyscale") %>%
#   addMarkers(lng = locations$lon, lat = locations$lat) %>%
#   addPolygons(
#     fill = TRUE, stroke = TRUE, color = "black",
#     weight = 0.5, fillOpacity = 0.2,
#     data = iso,
#     group = "Drive Time"
#   )