-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbatch.py
134 lines (122 loc) · 5.88 KB
/
batch.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
# -------------------------------------------------------
# Batch O-D Processing Script for OpenTripPlanner
# Written by: Andrew Byrd ([email protected]) and
# James Wong ([email protected])
# Last Updated: May 2012
#
# Useful links: www.opentripplanner.org/api-doc
# www.openplans.org
#
#--------------------------------------------------------
import urllib, urllib2, json, csv #import libraries
from datetime import date, datetime, timedelta
from time import sleep, strftime
INPUT = "cabi.csv" #Designate Input filename and path
OUTPUT = "bikeBatchOut-test.csv" #Designate Output filename and path
DECIMATE = 100 #Script samples every Nth record
URL = 'http://analyst.opentripplanner.org/opentripplanner-api-webapp/ws/plan?'
#URL and PLAN api from OTP deployment
SLEEP = 1 #Seconds between api calls
LIMIT = 3600 #Number of total executed requests
weekday = date.today().weekday() #Current day of the week
monday = date.today() - timedelta(days=weekday) #Num of days away from nearest Monday
# Universal linefeed reader for mac/win text files
outputFile = open(OUTPUT, 'w') #Overwrite a file
writer = csv.writer(outputFile, dialect='excel')
#Format for .csv
writer.writerow(['rowNum','CaBiTripTime','StartDate','StartTime','StartLat','StartLon','EndLat','EndLon','WalkTime', 'TransitTime','WaitingTime','NumTransfers','TotalTransitTripDist','PlannedBikeTime','PlannedBikeDist'])
#Write header row in .csv file
# Takes CSV input, creates URLs, stores data locally in row array
with open(INPUT, 'rU') as inputFile : #Continue using file
reader = csv.reader(inputFile, dialect='excel') #Read the input .csv file
rowNum = 1
for row in reader : #Samples file based on DECIMATE
rowNum += 1
if rowNum >LIMIT*DECIMATE :
break
if rowNum % DECIMATE != 0 :
continue
print "Row ", rowNum
try :
#assign input values directly into row
duration, date, time, o_lat, o_lon, d_lat, d_lon = row
# [t(f) for t, f in zip(types2, row)]
#Sets date to current equivalent weekday for use with up-to-date GTFS
date_new = datetime.strptime(date, "%m/%d/%Y")
date_new = monday + timedelta(days=date_new.weekday())
date_new = date_new.strftime("%m/%d/%Y")
row[1] = date_new
time_new = datetime.strptime(time, "%I:%M:%S %p").strftime("%T")
#creates time object
except Exception as e :
print e
print 'cannot parse input record, skipping'
continue
arriveBy = False #Using departure times for all calls
params = {'time' : '%s' % time_new, #Sets up the URL parameters
'fromPlace' : '%s,%s' % (o_lat, o_lon),
'toPlace' : '%s,%s' % (d_lat, d_lon),
'maxWalkDistance' : 2000, #Arbitrary
'mode' : 'WALK,TRANSIT', #See OTP API Documentation
'date' : date_new,
'numItineraries' : 1, #For simplicity, keep set at 1. Max=3
'bannedRoutes' : 'Test_Purple',
'arriveBy' : 'true' if arriveBy else 'false' }
# Tomcat server + spaces in URLs -> HTTP 505 confusion. urlencode percent-encodes parameters.
url = URL + urllib.urlencode(params) #Create a POST link using params
req = urllib2.Request(url)
req.add_header('Accept', 'application/json')
print url
try :
response = urllib2.urlopen(req)
except urllib2.HTTPError as e :
print e
continue
try :
content = response.read() #Store response from OTP
objs = json.loads(content)
itineraries = objs['plan']['itineraries']
#Access itinerary objects from content
except :
print 'no itineraries'
continue
for i in itineraries : #Calc total dist from a multi-leg trip
dist = 0
if i['transitTime'] != 0 :
for leg in i['legs'] :
dist += leg['distance']
#Within each itinerary, get specific metrics and store them in the output row
for i in itineraries :
outrow = [ rowNum ] + row + [ i['walkTime'], i['transitTime'], i['waitingTime'], i['transfers'], dist]
#Repeat API call to get a BIKE itinerary and store results
params = {'time' : '%s' % time_new,
'fromPlace' : '%s,%s' % (o_lat, o_lon),
'toPlace' : '%s,%s' % (d_lat, d_lon),
'maxWalkDistance' : 2000,
'mode' : 'BICYCLE', #Only diff is in mode
'date' : date_new,
'numItineraries' : 1,
'bannedRoutes' : 'Test_Purple',
'arriveBy' : 'true' if arriveBy else 'false' }
url = URL + urllib.urlencode(params)
req = urllib2.Request(url)
req.add_header('Accept', 'application/json')
print url
try :
response = urllib2.urlopen(req)
except urllib2.HTTPError as e :
print e
continue
try :
content = response.read()
objs = json.loads(content)
itineraries = objs['plan']['itineraries']
except :
print 'no itineraries'
continue
print outrow
for i in itineraries : #Walktime=Biketime when mode=BICYCLE
outrow = outrow + [i['walkTime'],i['walkDistance']]
writer.writerow(outrow) #Write full row to output file
sleep(SLEEP) #Delay to maintain OTP performance