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: 61 × 3
   incident begin                end                 
      <int> <chr>                <chr>               
 1        1 2022-01-09T04:56:30Z 2022-01-09T05:49:00Z
 2        2 2022-01-09T05:07:00Z 2022-01-09T05:45:00Z
 3        3 2021-01-04T07:27:00Z 2022-12-31T20:00:00Z
 4        4 2021-01-04T07:27:00Z 2022-12-31T20:00:00Z
 5        5 2021-12-26T11:45:00Z <NA>                
 6        6 2021-03-24T09:10:20Z 2022-12-31T20:00:00Z
 7        7 2021-03-24T09:10:20Z 2022-12-31T20:00:00Z
 8        8 2022-01-06T14:11:00Z <NA>                
 9        9 2021-03-25T11:49:33Z 2022-12-31T20:00:00Z
10       10 2021-03-25T11:49:33Z 2022-12-31T20:00:00Z
# … with 51 more rows

You can also get the type of the incident…

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

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

# A tibble: 3 × 2
  incident from                           
     <int> <chr>                          
1        1 Magoula (Attiki Odos/A6)       
2        2 Πύλη Ε2                        
3        3 Mavromihali Petrobei (Etolikou)
# A tibble: 3 × 2
  incident to                          
     <int> <chr>                       
1        1 Aspropyrgos (Attiki Odos/A6)
2        2 Πειραιάς                    
3        3 Kapetan Matapa (Etolikou)   

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!