forked from rafapereirabr/otp-travel-time-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_script_loopHM.py
65 lines (47 loc) · 2.64 KB
/
python_script_loopHM.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
import gc
gc.collect()
#!/usr/bin/jython
from org.opentripplanner.scripting.api import OtpsEntryPoint
# Instantiate an OtpsEntryPoint
otp = OtpsEntryPoint.fromArgs(['--graphs', '.',
'--router', 'portland'])
# Start timing the code
import time
start_time = time.time()
# Get the default router
router = otp.getRouter('portland')
# Read Points of Destination - The file points.csv contains the columns GEOID, X and Y.
points = otp.loadCSVPopulation('points.csv', 'Y', 'X')
dests = otp.loadCSVPopulation('points.csv', 'Y', 'X')
for h in range(7, 13): # Loop every hour between 7h and 13h
for m in range(0,60,10): # Loop every 10 minutes
# Create a default request for a given time
req = otp.createRequest()
req.setDateTime(2015, 9, 15, h, m, 00) # set departure time
req.setMaxTimeSec(7200) # set a limit to maximum travel time (seconds)
req.setModes('WALK,TRANSIT') # define transport mode : ("WALK,CAR, TRANSIT, TRAM,RAIL,SUBWAY,FUNICULAR,GONDOLA,CABLE_CAR,BUS")
req.setClampInitialWait(0) # clamp the initial wait time to zero
# req.maxWalkDistance = 3000 # set the maximum distance (in meters) the user is willing to walk
# req.walkSpeed = walkSpeed # set average walking speed ( meters ?)
# req.bikeSpeed = bikeSpeed # set average cycling speed (miles per hour ?)
# ? ERROR req.setSearchRadiusM(500) # set max snapping distance to connect trip origin to street network
# for more routing options, check: http://dev.opentripplanner.org/javadoc/0.19.0/org/opentripplanner/scripting/api/OtpsRoutingRequest.html
# Create a CSV output
matrixCsv = otp.createCSVOutput()
matrixCsv.setHeader([ 'year','depart_time', 'origin', 'destination', 'walk_distance', 'travel_time' ])
# Start Loop
for origin in points:
print "Processing origin: ", str(h)+"-"+str(m)," ", origin.getStringData('GEOID')
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:
matrixCsv.addRow([ 2015, str(h) + ":" + str(m) + ":00", origin.getStringData('GEOID'), r.getIndividual().getStringData('GEOID'), r.getWalkDistance() , r.getTime()])
# Save the result
matrixCsv.save('traveltime_matrix_'+ str(h)+"-"+str(m) + '.csv')
gc.collect()
# Stop timing the code
print("Elapsed time was %g seconds" % (time.time() - start_time))