Created
May 6, 2021 06:29
-
-
Save lmeulen/09b14752aa8662ab0e42df741bb584a8 to your computer and use it in GitHub Desktop.
traveltime_get_traveltimes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
URL = 'http://localhost:8080/otp/routers/default/plan?' | |
outputFile = open(OUTPUT, 'w') | |
writer = csv.writer(outputFile) | |
writer.writerow(['StartDate','StartTime','StartLat','StartLon','EndLat', | |
'EndLon', 'DurationMin']) | |
# Takes CSV input, creates URLs, stores data locally in row array | |
for c, d in df.iterrows(): | |
params = {'date' : d['Date'], | |
'time' : d['Time'], | |
'fromPlace' : '%s,%s' % (d['start_lat'], d['start_lon']), | |
'toPlace' : '%s,%s' % (d['stop_lat'], d['stop_lon']), | |
'maxWalkDistance' : 2000, | |
'mode' : 'WALK,TRANSIT', | |
'numItineraries' : 1, | |
'arriveBy' : 'false' } | |
req = urllib.request.Request(URL + urllib.parse.urlencode(params)) | |
req.add_header('Accept', 'application/json') | |
response = urllib.request.urlopen(req) | |
try : | |
content = response.read() | |
objs = json.loads(content) | |
i = objs['plan']['itineraries'][0] | |
outrow = d.tolist() + [ int(i['duration']/60) ] | |
print(str(c) + ' ' + str(outrow)) | |
writer.writerow(outrow) | |
except : | |
print ('Unreachable') | |
continue | |
outputFile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment