Skip to contents

Overview

Google Maps displays information about traffic conditions across an area. This package provides functions to produce georeferenced rasters from real-time Google Maps traffic information. Having Google traffic information in a georeferenced data format facilitates analysis of traffic information (e.g., spatially merging traffic information with other data sources).

This package was inspired by (1) existing research that has used Google traffic information, including in New York City and Dar es Salaam, and (2) similar algorithms implemented in JavaScript and in a C shell script.

The below image shows an example raster produced using the package showing traffic within Washington, DC.

Example

Pixel values in rasters are derived from Google traffic colors and can be one of four values:

Google Traffic Color Description Raster Value
Green No traffic delays 1
Orange Medium traffic 2
Red High traffic 3
Dark Red Heavy traffic 4

The package provides function to query Google traffic data around a location, within a polygon, or using a grid. The main functions are the following:


Installation

The package can be installed via CRAN.

install.packages("googletraffic")

Google API Key

Querying Google traffic information requires a Google API key with the Maps Javascript API enabled. To create a Google API key, follow these instructions.


Setup

The below code should be run before running the following examples. We load packages, set the Google API key, and define a palette used for visualizing traffic data in leaflet.

## Load Google Traffic package
library(googletraffic)

## Load additional packages for working with and visualizing data
library(leaflet)
library(leaflet.providers)
library(raster)
library(dplyr)

## Set Google API Key
google_key <- "GOOGLE-API-KEY-HERE"

## Define Leaflet Palette and Legend
traffic_pal <- colorNumeric(c("green", "orange", "red", "#660000"), 
                            1:4,
                            na.color = "transparent")

Key parameters

The following are key parameters relevant across functions for querying Google Traffic data.

  • zoom: The zoom level defines the resolution of the traffic image. Values can range from 0 to 20. At the equator, with a zoom level 10, each pixel will be about 150 meters; with a zoom level 20, each pixel will be about 0.15 meters. Consequently, smaller zoom levels can be used if only larger roads are of interest (e.g., highways), while larger zoom levels will be needed for capturing smaller roads.
  • height/width: The height and width parameters define the height and width of the raster in terms of pixels. The kilometer height/width of pixels depends primarily on the zoom level (larger zoom levels correspond to the pixels having a smaller kilometer distance).

Large height/width and delay time: Google traffic data takes time to render on a map, and larger height and width values require more time for data to render. The functions automatically scale the delay time depending on the height and width values set, but the delay time can also be manually set using the webshot_delay parameter. Note that traffic data may fail to render for very large height and width values, no matter the webshot_delay set (we find that the function works well with a height and width of 2000 or less).

Default height/width: In the gt_make_raster_from_polygon() and gt_make_grid() functions, height and width do not need to be set. The function will first test a height and width of 2000 for each API query to cover the region of interest, but if a smaller height and width can be used where the same number of API calls are made, a smaller height and width will be used. However, the height and width can still be manually set in these functions.


Raster Around Point

The gt_make_raster() function produces a raster, using a centroid location and a height/width around the centroid to specify the location to query traffic information. The below example queries traffic for lower Manhattan, NYC.

## Make raster
r <- gt_make_raster(location   = c(40.712778, -74.006111),
                    height     = 1000,
                    width      = 1000,
                    zoom       = 16,
                    google_key = google_key)
## Map raster
leaflet(width = "100%") %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addRasterImage(r, colors = traffic_pal, opacity = 1, method = "ngb") 

By using a smaller zoom, we can capture a larger area; however, the pixels are more coarse.

## Make raster
r <- gt_make_raster(location   = c(41.384900, -78.891302),
                    height     = 1000,
                    width      = 1000,
                    zoom       = 7,
                    google_key = google_key)
## Map raster
leaflet(width = "100%") %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addRasterImage(r, colors = traffic_pal, opacity = 1, method = "ngb") %>%
  setView(lat = 41.384900, lng = -78.891302, zoom = 6) 

Raster Around Polygon

The above example shows querying traffic information for lower Manhattan. In this example, we show querying traffic information for all of Manhattan while still using a relatively high zoom level (that allows capturing traffic on smaller streets). The gt_make_raster_from_polygon() accepts a polygon as an input; if needed, multiple API queries are made to query traffic for the full polygon.

## Grab polygon of Manhattan
us_sp <- getData('GADM', country='USA', level=2)
ny_sp <- us_sp[us_sp$NAME_2 %in% "New York",]

## Make raster
r <- gt_make_raster_from_polygon(polygon    = ny_sp,
                                 zoom       = 15,
                                 google_key = google_key)
## Map raster
leaflet(width = "100%") %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addRasterImage(r, colors = traffic_pal, opacity = 1, method = "ngb") 

Raster Using Grid

Within gt_make_raster_from_polygon(), the function creates a grid that covers a polygon, creates a traffic raster for each grid, and merges the rasters together. Some may prefer to first create and see the grid, then create a traffic raster using this grid. For example, one could (1) create a grid that covers a polygon then (2) remove certain grid tiles that cover areas that may not be of interest. The gt_make_grid() and gt_make_raster_from_grid() functions facilitate this process; gt_make_grid() creates a grid, then gt_make_raster_from_grid() uses a grid as an input to create a traffic raster.

First, we create a grid using gt_make_grid().

grid_df <- gt_make_grid(polygon = ny_sp,
                        zoom    = 15)
leaflet(width = "100%") %>%
  addTiles() %>%
  addPolygons(data = grid_df, popup = ~as.character(id))

We notice that the tile in the bottom left corner just covers water and some land outside of Manhattan. To reduce the number of API queries we need to make, we can remove this tile.

grid_clean_df <- grid_df[-5,]

leaflet(width = "100%") %>%
  addTiles() %>%
  addPolygons(data = grid_clean_df)

Second, we use the grid to make a traffic raster using gt_make_raster_from_grid().

## Make raster
r <- gt_make_raster_from_grid(grid_param_df = grid_clean_df,
                              google_key    = google_key)
## Map raster
leaflet(width = "100%") %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addRasterImage(r, colors = traffic_pal, opacity = 1, method = "ngb")