Chapter 13 Drive time analysis

13.1 Background on drive times

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

  • Running a drive time analysis is useful to identify demographic catchments around a point (e.g. a school, hospital, road, or precinct.

  • This can assist in defining the ‘catchment’ of users for a particular infrastructure asset.

  • These polygons can then be overlayed with ABS census data (e.g. SA2) and spliced in with census variables (age, income, housing, SES status, etc).

  • There’s various companies that own drive time data. Most of these are map makers (e.g. google, ESRI, and Tom Tom).

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, webshot,osrm)

13.2 Method 1: OSRM package

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.

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

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

#iso@data$drive_times <- factor(paste(iso@data$min, "to", iso@data$max, "mins"))

#factpal <- colorFactor("RdYlBu", iso@data$drive_times)

leaflet() %>%
  setView(mean(locations$lon), mean(locations$lat), zoom = 7) %>%
  
  addProviderTiles("CartoDB.Positron", group="Greyscale") %>% 
  
  addMarkers(lng = locations$lon, locations$lat) %>% 
  
  addPolygons(fill=TRUE, stroke=TRUE, color = "black",
              #fillColor = ~factpal(iso@data$drive_times),
              weight=0.5, fillOpacity=0.2,
              data = iso, #popup = iso@data$drive_times,
              group = "Drive Time") #%>% 
  # addLegend("bottomright", pal = factpal, 
  #                           values = iso@data$drive_time,   
  #                           title = "Drive Time")

We can export these polygons as shapefiles for use in QGIS or other spatial programs.

#Write the points to a shapefile for use in QGIS
#sf::st_write(iso, "C:/Data/iso_polygons.shp")