This repository has been archived by the owner on Apr 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper_07_otp_disk.py
71 lines (60 loc) · 2.16 KB
/
helper_07_otp_disk.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
#!/usr/bin/jython
from org.opentripplanner.scripting.api import OtpsEntryPoint
from datetime import datetime
import os
import time
import json
import csv
# Importing the current county and config vars
with open("config.json") as filename:
jsondata = json.load(filename)
maxtt = jsondata["otp_settings"]["maximum_travel_time"]
modes = ','.join(jsondata["otp_settings"]["transport_modes"])
dep_date = datetime.strptime(
jsondata["otp_settings"]["departure_date"],
"%Y-%m-%d")
dep_time = datetime.strptime(
jsondata["otp_settings"]["departure_time"],
"%H:%M:%S")
geoid = "17031"
# Instantiate an OtpsEntryPoint
otp = OtpsEntryPoint.fromArgs(['--graphs', 'otp/graphs', '--router', geoid])
# Start timing the code
start_time = time.time()
# Get the default router
router = otp.getRouter(geoid)
# Create a default request for a given departure time
req = otp.createRequest()
req.setDateTime(
dep_date.year,
dep_date.month,
dep_date.day,
dep_time.hour,
dep_time.minute,
dep_time.second
) # set departure time
req.setMaxTimeSec(maxtt) # set a limit to maximum travel time
req.setModes(modes) # define transport mode
req.maxWalkDistance = 10000 # set the maximum distance
# CSV containing the columns GEOID, Y and X
points = otp.loadCSVPopulation('points.csv', 'Y', 'X')
dests = otp.loadCSVPopulation('points.csv', 'Y', 'X')
# Writes directly to a CSV output file
with open('matrix.csv', "wb") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for origin in points:
print "Processing origin: ", origin
req.setOrigin(origin)
spt = router.plan(req)
if spt is None: continue
# Evaluate the SPT for all points
result = spt.eval(dests)
# Add a new row of result in the CSV output
for r in result:
writer.writerow([
int(origin.getFloatData('GEOID')),
int(r.getIndividual().getFloatData('GEOID')),
round(r.getTime() / 60.0, 2) # time in minutes
])
# Stop timing the code
print("Elapsed time was %g seconds" % (time.time() - start_time))