Understanding and Extracting Graph Data from the TomTom API Using R

Understanding TomTom API and Extracting Graph Data

Introduction

The TomTom website provides various APIs for accessing traffic data, including hourly congestion level graphs. In this article, we will explore how to extract graph data from the TomTom website using R.

Background

The TomTom API uses JSON files to store data, which can be accessed through GET requests. The httr package in R is used to make HTTP requests and parse the response.

Getting Started with TomTom API

To start extracting graph data from the TomTom website, we need to obtain an API key. This can be done by creating an account on the TomTom website and applying for an API key.

Once we have our API key, we can use it to make GET requests to the TomTom API. The httr::GET function is used to make a GET request to the TomTom API.

res <- httr::GET("https://api.midway.tomtom.com/ranking/liveHourly/CHN_wuhan")

This will return a JSON response that can be parsed using the content() function in R.

Parsing JSON Response

The content() function returns a character vector containing the raw JSON data. We can use the tibble package to convert this data into a tibble, which is a data frame with column names and row labels.

library(tibble)

# Convert JSON response to a tibble
data <- as_tibble(apply(do.call(rbind, content(res, "parsed")$data), 2, unlist))

This will return a tibble with the extracted data from the TomTom API.

Extracting Hourly Congestion Level Data

To extract hourly congestion level data, we can use the update_time column in the tibble to filter out rows that do not correspond to hourly data. We can also use the jams_delay column to calculate the average congestion delay for each hour.

# Filter out rows that do not correspond to hourly data
hourly_data <- data %>%
  filter(update_time %in% seq(as.Date("2020-06-19"), as.Date("2020-06-20")))

# Calculate average congestion delay for each hour
average_delay <- hourly_data %>%
  group_by(hour) %>%
  summarise(avg_delay = mean(jams_delay))

This will return a tibble with the average congestion delay for each hour.

Conclusion

In this article, we have explored how to extract graph data from the TomTom website using R. We have used the httr package to make GET requests to the TomTom API and parsed the response using the tibble package. We have also extracted hourly congestion level data from the tibble and calculated the average congestion delay for each hour.

Further Reading

For more information on the TomTom API, please refer to the official documentation:

https://developer.tomtom.com/api/

For more information on R packages used in this article, please refer to the following links:

Note: This is a long-form technical blog post. Please let me know if you want any adjustments or changes.


Last modified on 2024-01-19