DEV Community

Andrew B. Collier
Andrew B. Collier

Posted on • Originally published at datawookie.dev on

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
Enter fullscreen mode Exit fullscreen mode

Traffic Incidents

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

incidents <- incident_details(left, bottom, right, top)
Enter fullscreen mode Exit fullscreen mode

The expected duration and description of each incident is provided.

incidents %>% select(incident, begin, end)

# A tibble: 125 x 3
   incident begin end                 
      <int> <chr> <chr>               
 1 1 2021-07-26T10:44:30Z 2021-07-26T11:02:00Z
 2 2 2021-07-26T10:16:38Z 2021-07-26T11:18:00Z
 3 3 2021-07-26T10:33:30Z 2021-07-26T11:13:00Z
 4 4 2021-07-26T10:46:08Z 2021-07-26T11:12:00Z
 5 5 2021-07-26T10:43:00Z 2021-07-26T11:26:00Z
 6 6 2021-07-26T08:57:00Z 2021-07-26T11:14:00Z
 7 7 2021-07-26T10:30:30Z 2021-07-26T11:13:00Z
 8 8 2021-01-04T07:27:00Z 2021-12-31T20:00:00Z
 9 9 2021-07-20T09:14:13Z 2021-08-17T19:29:00Z
10 10 2021-01-04T07:27:00Z 2021-12-31T20:00:00Z
# … with 115 more rows
Enter fullscreen mode Exit fullscreen mode

You can also get the type of the incident…

# A tibble: 3 x 2
  incident description       
     <int> <fct>             
1 1 Slow traffic      
2 2 Stationary traffic
3 3 Slow traffic      
Enter fullscreen mode Exit fullscreen mode

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

# A tibble: 3 x 2
  incident from                                   
     <int> <chr>                                  
1 1 Nea Peramos (Olympia Odos/A8)          
2 2 Adrianou Ave (Perifereiaki Aigaleo/A65)
3 3 Γεωργίου Παπανδρέου                    

# A tibble: 3 x 2
  incident to                                                             
     <int> <chr>                                                          
1 1 Stathmos Diodion Eleysinas (Olympia Odos/A8)                   
2 2 Aspropyrgos-Biomihaniki Periohi (A8) (Perifereiaki Aigaleo/A65)
3 3 Θεοδώρου Βασιλάκη                                              
Enter fullscreen mode Exit fullscreen mode

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

# A tibble: 125 x 8
   incident begin end description from to length points 
      <int> <chr> <chr> <fct> <chr> <chr> <dbl> <list> 
 1 1 2021-07… 2021-07… Slow traffic Nea Pera… Stathmos Di… 132. <df[,2…
 2 2 2021-07… 2021-07… Stationary … Adrianou… Aspropyrgos… 799. <df[,2…
 3 3 2021-07… 2021-07… Slow traffic Γεωργίου… Θεοδώρου Βα… 978. <df[,2…
 4 4 2021-07… 2021-07… Queuing tra… Κρήτης Αττική Οδός… 1134. <df[,2…
 5 5 2021-07… 2021-07… Stationary … Αττική Ο… Κρήτης 866. <df[,2…
 6 6 2021-07… 2021-07… Queuing tra… Iera Odo… Aspropyrgos… 3717. <df[,2…
 7 7 2021-07… 2021-07… Stationary … Ougko Vi… Labraki Gr.… 204. <df[,2…
 8 8 2021-01… 2021-12… Restrictions Mavromih… Kapetan Mat… 81.7 <df[,2…
 9 9 2021-07… 2021-08… Closed Koumound… Gitchiou (E… 29.7 <df[,2…
10 10 2021-01… 2021-12… Restrictions Koumound… Mavromihali… 43.0 <df[,2…
# … with 115 more rows

# A tibble: 2,558 x 3
   incident lon lat
      <int> <dbl> <dbl>
 1 1 23.5 38.0
 2 1 23.5 38.0
 3 1 23.5 38.0
 4 1 23.5 38.0
 5 1 23.5 38.0
 6 1 23.5 38.0
 7 1 23.5 38.0
 8 2 23.6 38.0
 9 2 23.6 38.0
10 2 23.6 38.0
# … with 2,548 more rows
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)