Isochrones are lines drawn on a map that represent an equal distance, measured in transportation time, from a single starting point. They can be used to create catchment areas that illustrate ranges of driving time from a point (e.g. 0-10 minutes, 10-20 minutes, etc.), which allows for quick analysis of roughly how far away other points of interest are from that starting point. This tutorial shows how to create isochrones using HERE’s isoline API (an isochrone is an isoline specifically applied to time measurements). HERE’s API for creating catchment areas allows for specifying if the areas should be drawn by distance (in meters), time, or by consumption in watt hours, and also allows a user to indicate the date and time at the starting location when creating an isochrone to simulate real-world traffic.
This step uses the pacman package to install and load all of the packages for this tutorial.
# Use pacman package to load packages used for exercise
library(pacman)
p_load(tidyverse, # Variety of packages for data manipulation
hereR, # Package for accessing HERE isoline API
leaflet) # For creating interactive maps
To illustrate how isochrones are built using the HERE isoline API, the example below uses three UPMC hospitals in the Southwestern Pennsylvania region as starting points. This page shows how those hospital addresses were geocoded. The three hospitals selected (UPMC Passvant in Cranberry, UPMC East in Monroeville, and UPMC McKeesport in McKeesport) are the three furthest from downtown Pittsburgh, so the catchment areas created by their isochrones will show which parts of the city can reach them within a 10-minute or 20-minute drive.
# Load UPMC hospitals data
upmc_hospitals <- read.csv("./data/UPMC Locations (Geocoded).csv")
# Select three hospitals furthest away from downtown
far_upmc <- upmc_hospitals %>%
filter(Location %in% c("UPMC Passavant - Cranberry",
"UPMC East",
"UPMC McKeesport"))
# Convert UPMC Hospital data to simple feature
far_upmc_points <- st_as_sf(x = far_upmc,
coords = c("Longitude", "Latitude"),
crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
The steps below create 10-minute driving time and 20-minute driving time isochrones for each of the three selected UPMC hospitals. Those isochrones are used to create a 0-10 minute catchment area and a 10-20 minute catchment area surrounding each of those hospitals. The first step to using the HERE API for building isolines is to request an API key and then set it in R.
# Store and set HERE API key
here_key <- "[insert your API key here]"
set_key(here_key)
The isoline function from the hereR package is used below to build the isochrones for this tutorial. The values included in the API function are as follows:
# 1. Download isochrones for UPMC Monroeville
isochrones_1 <- isoline(
poi = far_upmc_points[1,],
range = seq(10, 20, 10) * 60,
range_type = "time",
datetime <- as.POSIXct(paste0(Sys.Date()," 10:00"))
) %>%
mutate(name = paste0((range - 600) / 60," to ", range / 60, " mins"))
# Convert UPMC Monroeville isochrones to shapefile
isochrones_1.sp <- as(isochrones_1, "Spatial")
# Create a color palette for the UPMC Monroeville catchment areas
iso_1.colors <- c("#74c476", "#006d2c")
iso_1.pal <- colorFactor(iso_1.colors, isochrones_1.sp@data$name)
# 2. Download isochrones for UPMC McKeesport
isochrones_2 <- isoline(
poi = far_upmc_points[2,],
range = seq(10, 20, 10) * 60,
range_type = "time",
datetime <- as.POSIXct(paste0(Sys.Date()," 10:00"))
) %>%
mutate(name = paste0((range - 600) / 60," to ", range / 60, " mins"))
# Convert UPMC McKeesport isochrones to shapefile
isochrones_2.sp <- as(isochrones_2, "Spatial")
# Create a color palette for the UPMC McKeesport catchment areas
iso_2.colors <- c("#9e9ac8", "#54278f")
iso_2.pal <- colorFactor(iso_2.colors, isochrones_2.sp@data$name)
# 3. Download isochrones for UPMC Cranberry
isochrones_3 <- isoline(
poi = far_upmc_points[3,],
range = seq(10, 20, 10) * 60,
range_type = "time",
datetime <- as.POSIXct(paste0(Sys.Date()," 10:00"))
) %>%
mutate(name = paste0((range - 600) / 60," to ", range / 60, " mins"))
# Convert UPMC Cranberry isochrones to shapefile
isochrones_3.sp <- as(isochrones_3, "Spatial")
# Create a color palette for the UPMC Cranberry catchment areas
iso_3.colors <- c("#fd8d3c", "#a63603")
iso_3.pal <- colorFactor(iso_3.colors, isochrones_3.sp@data$name)
The code below demonstrates how to plot the isochrones and catchment areas on an interactive map. Each isochrone has its own color palette (defined above) and legend, and there is a final setting added so that users can toggle which elements they see displayed on the map.
# Plot with leaflet
leaflet() %>%
# Add background map
addProviderTiles("CartoDB.Positron", group="Greyscale") %>%
# Add UPMC Monroeville isochrones
addPolygons(data = isochrones_1.sp,
fill=TRUE,
fillColor = ~iso_1.pal(isochrones_1.sp@data$name),
fillOpacity=0.35,
stroke=TRUE,
color = "black",
weight=0.5,
popup = isochrones_1.sp@data$name,
group = "Monroeville") %>%
# Add UPMC McKeesport isochrones
addPolygons(data = isochrones_2.sp,
fill=TRUE,
fillColor = ~iso_2.pal(isochrones_2.sp@data$name),
fillOpacity=0.35,
stroke=TRUE,
color = "black",
weight=0.5,
popup = isochrones_2.sp@data$name,
group = "McKeesport") %>%
# Add UPMC Cranberry isochrones
addPolygons(data = isochrones_3.sp,
fill=TRUE,
fillColor = ~iso_3.pal(isochrones_3.sp@data$name),
fillOpacity=0.35,
stroke=TRUE,
color = "black",
weight=0.5,
popup = isochrones_3.sp@data$name,
group = "Cranberry") %>%
# Add circles for each hospital location
addCircles(far_upmc$Longitude,
far_upmc$Latitude,
radius = 5,
opacity = 1,
group = "Hospitals") %>%
# Add legends for each hospital's catchment areas
addLegend("bottomleft",
values = isochrones_1.sp@data$name,
pal = iso_1.pal,
opacity = 0.35,
title = "UPMC Monroeville range",
group = "Monroeville") %>%
addLegend("bottomleft",
values = isochrones_2.sp@data$name,
pal = iso_2.pal,
opacity = 0.35,
title = "UPMC McKeesport range",
group = "Cranberry") %>%
addLegend("bottomleft",
values = isochrones_3.sp@data$name,
pal = iso_3.pal,
opacity = 0.35,
title = "UPMC Cranberry range",
group = "McKeesport") %>%
# Layers control allows the user to turn layers on and off
addLayersControl(options = layersControlOptions(collapsed = FALSE),
overlayGroups = c("Monroeville",
"McKeesport",
"Cranberry",
"Hospitals"))
HERE’s isoline API can also be used to build isochrones around multiple points together. The example above generated isochrones for each of the three hospitals individually, which is useful for determining proximity to each individual hospital; however, a slight modification of the above code can generate a result for drive time to the nearest of those three hospitals. Below is an example that shows how to generate isochrones based off of all three hospitals.
# Download isochrones for all three
isochrones_all <- isoline(
poi = far_upmc_points,
range = seq(10, 30, 10) * 60,
range_type = "time",
datetime <- as.POSIXct(paste0(Sys.Date()," 10:00"))
) %>%
mutate(name = paste0((range - 600) / 60," to ", range / 60, " mins"))
# Convert to shapefile
isochrones_all.sp <- as(isochrones_all, "Spatial")
# Create a color palette for the catchment areas
iso_all.colors <- c("#c6dbef", "#4292c6", "#08306b")
iso_all.pal <- colorFactor(iso_all.colors, isochrones_all.sp@data$name)
# Plot with leaflet
leaflet() %>%
addProviderTiles("CartoDB.Positron", group="Greyscale") %>%
addPolygons(data = isochrones_all.sp,
fill=TRUE,
fillColor = ~iso_all.pal(isochrones_all.sp@data$name),
fillOpacity=0.35,
stroke=TRUE,
color = "black",
weight=0.5,
popup = isochrones_all.sp@data$name,
group = "Catchment Area") %>%
addCircles(far_upmc$Longitude,
far_upmc$Latitude,
radius = 5,
opacity = 1,
group = "Hospitals") %>%
# Add legends and layer control
addLegend("bottomleft",
values = isochrones_all.sp@data$name,
pal = iso_all.pal,
opacity = 0.35,
title = "UPMC hospitals range",
group = "All") %>%
addLayersControl(options = layersControlOptions(collapsed = FALSE),
overlayGroups = c("Catchment Area",
"Hospitals"))