Hi folks
About 12 months I successfully used the following code to grab journey time and incident info from TomTom’s routing and incident API’s (there’s two config files that I’ve listed underneath that provide the api key and a few other settings).
Unfortunately, it now fails with an ‘IndexError’> ((‘list index out of range’,)). I’m obviously missing something really straightforward, but I just can’t see it.
Can anyone help point me in the right direction? Thanks in advance
import sys, googlemaps, logging, configparser, time, psycopg2, requests, json
import csv
import pandas as pd
from collections import OrderedDict
from datetime import datetime, timedelta
sys.path.append(r'C:\Users\garsi\OneDrive\Garsides_Stuff\Dad\Python\Test_Files\config')
import tomtomconfig
fg = fh = ft = fi = fj = ''
#----------------------------------------------------------------------
def getRoutes(dbcon=None):
# format of routes = [route no., start lat, start long, end lat, end long, "via lat, via long", 'link name']
routes=[[1,54.561523,-5.9097316,54.562059,-5.9098191,'',''],
[2,54.562059,-5.9098191,54.565045,-5.9119461,'',''],
[3,54.565045,-5.9119461,54.567968,-5.9137395,'',''],
[4,54.567968,-5.9137395,54.569721,-5.9139547,'',''],
[5,54.569721,-5.9139547,54.570885,-5.9140828,'',''],
[6,54.570885,-5.9140828,54.571715,-5.9143161,'',''],
[7,54.571715,-5.9143161,54.574461,-5.9163395,'',''],
[8,54.574461,-5.9163395,54.576399,-5.9177286,'',''],
[9,54.576399,-5.9177286,54.577931,-5.9188397,'',''],
[10,54.577931,-5.9188397,54.579498,-5.9199957,'',''],
[11,54.579498,-5.9199957,54.581296,-5.9212346,'',''],
[12,54.581296,-5.9212346,54.582275,-5.9217083,'',''],
[13,54.582275,-5.9217083,54.583216,-5.9221086,'',''],
[14,54.583216,-5.9221086,54.584319,-5.9225499,'',''],
[15,54.584319,-5.9225499,54.586312,-5.9234139,'',''],
[16,54.586312,-5.9234139,54.587591,-5.9239798,'',''],
[17,54.587591,-5.9239798,54.590280,-5.9252461,'',''],
[18,54.590280,-5.9252461,54.591847,-5.9258946,'',''],
[19,54.591937,-5.9258185,54.590538,-5.9252418,'',''],
[20,54.590538,-5.9252418,54.587696,-5.9239339,'',''],
[21,54.587696,-5.9239339,54.586666,-5.9234541,'',''],
[22,54.586666,-5.9234541,54.584405,-5.9224902,'',''],
[23,54.584405,-5.9224902,54.583296,-5.9220432,'',''],
[24,54.583296,-5.9220432,54.582604,-5.9217546,'',''],
[25,54.582604,-5.9217546,54.581375,-5.9211916,'',''],
[26,54.581375,-5.9211916,54.579580,-5.9199766,'',''],
[27,54.579580,-5.9199766,54.578013,-5.9188072,'',''],
[28,54.578013,-5.9188072,54.576462,-5.9176887,'',''],
[29,54.576462,-5.9176887,54.574541,-5.9162983,'',''],
[30,54.574541,-5.9162983,54.572025,-5.9143131,'',''],
[31,54.572025,-5.9143131,54.571143,-5.9140258,'',''],
[32,54.571143,-5.9140258,54.569806,-5.9138917,'',''],
[33,54.569806,-5.9138917,54.568267,-5.9136922,'',''],
[34,54.568267,-5.9136922,54.565454,-5.9121379,'',''],
[35,54.565454,-5.9121379,54.562206,-5.9096032,'',''],
[36,54.562206,-5.9096032,54.561946,-5.9092800,'',''],
]
if routes:
min_lat=[min(min([x[1] for x in routes]),min([x[3] for x in routes]))]
min_long=[min(min([x[2] for x in routes]),min([x[4] for x in routes]))]
max_lat=[max(max([x[1] for x in routes]),max([x[3] for x in routes]))]
max_long=[max(max([x[2] for x in routes]),max([x[4] for x in routes]))]
return (routes, min_lat, min_long, max_lat, max_long)
else:
return(None, None)
def getData(routes, min_lat, min_long, max_lat, max_long):
global fg, fh, ft, fi, fj
i_params = {"projection": "EPSG4326", "originalposition": "true", "key":tomtomconfig.TOMTOM_APIKEY}
apiurl2 = "https://api.tomtom.com/traffic/services/4/incidentDetails/s3/{},{},{},{}/15/-1/json?".format(min_lat[0],min_long[0],max_lat[0],max_long[0])#add boundary box coords/zoom level in here for incident reports
tnow = datetime.now()
req2 = requests.get(apiurl2, params=i_params)
ri = req2.json()
try:
if ri['tm']['poi'][0]:
sri = ri['tm']['poi'][0]
di=[tnow,sri['id'],sri['p'].get('x',''),sri['p'].get('y',''),sri['ic'],sri['ty'],sri['d'],sri['sd'],sri['ed'],sri['c'],sri['f'],sri['t'],sri['l']]
if tomtomconfig.WRITE_TO_FILE:
with open(fi, 'a', newline='') as f:
wi = csv.writer(f)
wi.writerow(di)
except KeyError:
logging.info("Route {}: TomTom data retrieval error".format)
return
for r in routes:
# TomTom
time.sleep(.25)
r_params = {"traffic":"true", "routeRepresentation":"polyline", "computeTravelTimeFor": "all", "routeType": "shortest", "key":tomtomconfig.TOMTOM_APIKEY}
if r[5]:
apiurl = "https://api.tomtom.com/routing/1/calculateRoute/{},{}:circle({},50):{},{}/json".format(r[1],r[2],r[5].replace("",""),r[3],r[4])
else:
apiurl = "https://api.tomtom.com/routing/1/calculateRoute/{},{}:{},{}/json".format(r[1],r[2],r[3],r[4])
tnow = datetime.now()
req = requests.get(apiurl, params=r_params)
rd = req.json()
try:
if rd['routes'][0]['summary']:
srt = rd['routes'][0]['summary']
if rd['routes'][0]['legs'][0]['points']:
points = rd['routes'][0]['legs'][0]['points']
route_points = [[point['latitude'],point['longitude']] for point in points]
d = [r[0],tnow,srt['lengthInMeters'],route_points,srt['historicTrafficTravelTimeInSeconds'],srt['liveTrafficIncidentsTravelTimeInSeconds'],srt['noTrafficTravelTimeInSeconds'],srt['trafficDelayInSeconds'],srt['travelTimeInSeconds']]
if tomtomconfig.WRITE_TO_FILE:
with open(ft, 'a', newline='') as f:
wr = csv.writer(f)
wr.writerow(d)
except KeyError:
logging.info("Route {}: TomTom data retrieval error".format(r[0]))
return
def main(echo=False):
global fg, fh, ft, fi, fj
if tomtomconfig.DEBUG:
loglevel = logging.DEBUG
else:
loglevel = logging.WARNING
logging.basicConfig(filename=tomtomconfig.LOGFILE,format=tomtomconfig.LOGFORMAT,datefmt=tomtomconfig.LOGDATEFORMAT,level=loglevel)
logging.debug(tomtomconfig.LOGFILE)
dbcon = None
try:
if tomtomconfig.WRITE_TO_FILE:
tn = datetime.now()
ft = '{}/tomtomdata{}{}{}{}{}{}.csv'.format(tomtomconfig.SCRIPT_ROOT,tn.day,tn.month,tn.year,tn.hour,tn.minute,tn.second)
fht = open(ft, 'a', newline='')
fht.close()
fi = '{}/incidentdata{}{}{}{}{}{}.csv'.format(tomtomconfig.SCRIPT_ROOT,tn.day,tn.month,tn.year,tn.hour,tn.minute,tn.second)
fhi = open(fi, 'a', newline='')
fhi.close()
(routes,min_lat,min_long,max_lat,max_long) = getRoutes(dbcon)
while True:
try:
tn = datetime.now()
getData(routes, min_lat, min_long, max_lat, max_long)
interval = tomtomconfig.INTERVAL - (datetime.now() - tn).seconds
logging.debug('Sleeping for %s seconds...', interval)
time.sleep(interval)
except Exception as inst:
logging.error('Unhandle execption during update: %s (%s)',(type(inst)), inst.args)
time.sleep(tomtomconfig.INTERVAL)
#print(inst.args)
except KeyboardInterrupt:
logging.info('Shutdown requested...exiting')
except BaseException as e:
logging.error('An unexpected exception was encountered: %s' , str(e))
sys.exit(1)
finally:
# Cleanup, e.g. close open files.
pass
sys.exit(0)
``'
if name==‘main’:
main()
#tomtomconfig
import sys
import os
import configparser
import pprint
SCRIPT_ROOT = r’C:\Users\garsi\OneDrive\Garsides_Stuff\Dad\Python\Test_Files’
_config_file =‘%s/config/service.cfg’ % (SCRIPT_ROOT,)
config = configparser.RawConfigParser()
config.read(_config_file)
print (_config_file)
_app_section_name = ‘TomTom’
INTERVAL = float(config.get(_app_section_name, ‘interval’))
WRITE_TO_FILE = config.getboolean(_app_section_name, ‘write_to_file’)
USE_TEST_ONLY = config.getboolean(_app_section_name, ‘use_test_only’)
TOMTOM_APIKEY = config.get(_app_section_name, ‘tomtom_apikey’)
USE_TOMTOM = config.getboolean(_app_section_name, ‘use_tomtom’)
DEBUG = config.getboolean(_app_section_name, ‘debug’)
LOGFILE = ‘%s/logs/service.log’ % (SCRIPT_ROOT,)
LOGFORMAT = ‘%(asctime)s - %(levelname)s: %(message)s’
LOGDATEFORMAT = ‘%m/%d/%Y %H:%M:%S’
#service.cpg
[DEFAULT]
[TomTom]
debug: 1
title: TomTom traffic data collector
description:Grabs journey timeinfo from the TomTom Directions API
#processing details
write_to_file: 1
use_test_only: 0
TomTom api details
use_tomtom: 1
tomtom_apikey: “”
polling frequency
interval: 600.0
retry: 60.0