-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_od_multi.py
124 lines (108 loc) · 3.74 KB
/
make_od_multi.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
#!/usr/bin/env python
#
# Make OD matrix between all unique station linking points
# With additional information on station to grid distances
import numpy as np
import time
import os
from graphserver.ext.gtfs.gtfsdb import GTFSDatabase
from graphserver.ext.osm.osmdb import OSMDB
from graphserver.graphdb import GraphDatabase
from graphserver.core import Graph, Street, State, WalkOptions
from PIL import Image
from multiprocessing import Pool
os.environ['TZ'] = 'US/Pacific'
time.tzset()
t0s = "Mon May 17 08:50:00 2010"
t0t = time.strptime(t0s)
d0s = time.strftime('%a %b %d %Y', t0t)
t0 = time.mktime(t0t)
print 'search date: ', d0s
print 'search time: ', time.ctime(t0), t0
gtfsdb = GTFSDatabase ('./trimet.gtfsdb')
gdb = GraphDatabase ('./test.gdb' )
osmdb = OSMDB ('./testgrid.osmdb' )
g = gdb.incarnate ()
# FOOT - would be better if i could specify 0 boardings not 0 transfers
wo = WalkOptions()
wo.max_walk = 2000
wo.walking_overage = 0.0
wo.walking_speed = 1.0 # trimet uses 0.03 miles / 1 minute - but it uses straight line distance as well
wo.transfer_penalty = 99999
wo.walking_reluctance = 1
wo.max_transfers = 0
# make much higher?
wo.transfer_slack = 60 * 5
wo_foot = wo
# TRANSIT
wo = WalkOptions()
wo.max_walk = 2000
wo.walking_overage = 0.0
wo.walking_speed = 1.0 # trimet uses 0.03 miles / 1 minute - but it uses straight line distance as well
wo.transfer_penalty = 60 * 10
wo.walking_reluctance = 1.5
wo.max_transfers = 5
wo.transfer_slack = 60 * 4
wo_transit = wo
print "Fetching grid from OSMDB..."
grid = list(osmdb.execute("SELECT x, y, vertex FROM grid"))
max_x, max_y = osmdb.execute("SELECT max(x), max(y) FROM grid").next()
print "Finding unique GTFS station linking points..."
stop_vertices = [e[0] for e in gtfsdb.execute("SELECT DISTINCT osm_vertex FROM osm_links")]
#test
#stop_vertices = stop_vertices[:10]
def save_image(spt, fname) :
print "saving grid image..."
im = Image.new("L", (max_x, max_y))
for (x, y, vertex) in grid :
v = spt.get_vertex('osm-%s'%vertex)
if v != None :
c = ((v.best_state.time - t0) / (120. * 60)) * 255
im.putpixel((x, max_y - y - 1), 255 - c)
# be careful that img dir exists
im.save('img/%s.png' % (fname))
def transit((i, ov)):
print i, ov
ret = []
spt = g.shortest_path_tree( 'osm-%s' % ov, None, State(1, t0), wo_transit )
if spt == None :
return None
for dv in stop_vertices :
v = spt.get_vertex('osm-%s' % dv)
if v is not None :
s = v.best_state
# subtract out initial wait to get trip time, not relative arrival time
ret.append(int(s.time - s.initial_wait - t0))
else :
ret.append(None)
#save_image(spt, '%d_%d' % (i, os.getpid()))
spt.destroy()
#print ret
return ret
def walk((i, ov)):
print i, ov
ret = []
spt = g.shortest_path_tree( 'osm-%s' % ov, None, State(1, t0), wo_foot )
for (x, y, vertex) in grid :
v = spt.get_vertex('osm-%s' % vertex)
if v is not None :
vp = v.best_state
t = vp.time - t0
# shouldn't this be num boardings, not transfers?
if t < 3600 and vp.num_transfers == 0 :
ret.append((x, y, int(t)))
#save_image(spt, '%d_%d' % (i, os.getpid()))
spt.destroy()
#print ret
return ret
p = Pool(4)
rt = p.map(transit, enumerate(stop_vertices))
rw = p.map(walk, enumerate(stop_vertices))
# for e in rt : print e
# -1s request highest pickle protocol version
# the newer binary protocols are faster and more compact
import cPickle as pickle
output = open('data.pkl', 'wb')
pickle.dump( stop_vertices, output, -1 )
pickle.dump( rt, output, -1 )
pickle.dump( rw, output, -1 )