TomTom Traffic

In the previous post I introduced the {tomtom} package and showed how it can be used for geographic routing. Now we’re going to look at the traffic statistics returned by the TomTom API.

As before we’ll need to load the {tomtom} package and specify an API. Then we’re ready to roll.

Bounding Box 

We’ll be retrieving traffic incidents within a bounding box. We’ll define the longitude and latitude extrema of that box (in this case centred on Athens, Greece).

left <- 23.4
bottom <- 37.9
right <- 24.0
top <- 38.2

Traffic Incidents 

Now call the incident_details() function to retrieve information on all incidents within that area.

incidents <- incident_details(left, bottom, right, top)

The expected duration and description of each incident is provided.

incidents %>% select(incident, begin, end)
# A tibble: 192 × 3
   incident begin                end                 
      <int> <chr>                <chr>               
 1        1 2021-09-17T06:57:00Z 2021-09-17T07:18:00Z
 2        2 2021-09-17T03:48:30Z 2021-09-17T07:27:00Z
 3        3 2021-09-17T06:45:00Z 2021-09-17T07:18:00Z
 4        4 2021-09-17T06:35:30Z 2021-09-17T07:26:00Z
 5        5 2021-09-17T03:35:00Z 2021-09-17T07:19:00Z
 6        6 2021-09-17T06:58:00Z 2021-09-17T07:18:00Z
 7        7 2021-01-04T07:27:00Z 2021-12-31T20:00:00Z
 8        8 2021-01-04T07:27:00Z 2021-12-31T20:00:00Z
 9        9 2021-09-07T07:56:30Z <NA>                
10       10 2021-07-14T13:12:36Z 2021-11-30T20:23:00Z
# … with 182 more rows

You can also get the type of the incident…

# A tibble: 3 × 2
  incident description       
     <int> <fct>             
1        1 Stationary traffic
2        2 Stationary traffic
3        3 Stationary traffic

… along with a description of where it begins and ends.

# A tibble: 3 × 2
  incident from                                           
     <int> <chr>                                          
1        1 exit [2]                                       
2        2 Thalous (Shistou Ave)                          
3        3 Periferiaki Drapetsonas (Pireas D. Keratsiniou)
# A tibble: 3 × 2
  incident to                                 
     <int> <chr>                              
1        1 Μαγούλα                            
2        2 A8, Skaramangas (Shistou Ave)      
3        3 Bouboulinas (Pireas D. Keratsiniou)

Finally, the points field allows you to plot the locations of each incident.

Do yourself a favour and open the above map in a separate tab. The intricate details of the city of Athens are quite fascinating.

The current implementation of the {tomtom} package is really only scratching the surface of the TomTom API. If anybody is keen to collaborate on this, get in touch!