/
westpoint_commute.py
59 lines (48 loc) · 2.19 KB
/
westpoint_commute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from gmaps import Directions
import json
from parse import *
import sys
import time
# employee_id index
start_index = 871
# location
loc = 'West Point, PA'
#api_key = ''
key='AIzaSyDPU2PzqF9VK5g0fSEmrTjlc9C9T65ocnY'
#key='AIzaSyB7SerbrEj9rHCoedm02nE3bImBR_j4DtU'
api = Directions(api_key=key)
# input files library
dest_zip_file = 'data/merck_work_zip'
commuter_file = 'data/merck_zip_data'
# output file library
data_file_address = 'data/westpoint_commute'
# setup output file and create header
output_file = open(data_file_address, 'w')
output_file.write("EMPLOYEE ID, ORGANIZATION, ZIP CODE, DURATION, DISTANCE, POLYLINE\n")
# get dict of all destination zip codes
dest_zip = get_dest_zip(dest_zip_file)
# get dict of all commuters
commuters = get_commuter(commuter_file)
print "Retriving trip time, duration, and polyline to address:", loc
commuter_data = OrderedDict()
for index, commuter in enumerate(commuters[start_index:]):
print 'Employee:', commuter
# get the directions from the google api via the commuter file and destination file.
# and covert to dic
commute = api.directions( dest_zip[loc], str(commuter[2]) )
commute_dict = get_directions(commute)
# # create dictionary with employee id as key followed by [org, zip] , travel time, distance, polyline
# commuter_data.update({commuter[0]: [commuter[1], commuter[2],
# str( commute_dict['legs']['duration'][u'text'] ),
# str( commute_dict['legs']['distance'][u'text'] ),
# str( commute_dict['overview_polyline'][u'points'] )
# ]})
# print employee id, organization, zip code, duration, distance, polyline
output_file.write(str(commuter[0])+","+str(commuter[1])+","+str(commuter[2])
+","+str( commute_dict['legs']['duration'][u'text'] )+","
+str( commute_dict['legs']['distance'][u'text'] )+","
+str( commute_dict['overview_polyline'][u'points']))
output_file.write("\n")
time.sleep(0.5) # attempting to avoid query limits and stupid errors
output_file.close()
print "FINISHED"