-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_times.py
More file actions
141 lines (109 loc) · 3.43 KB
/
p_times.py
File metadata and controls
141 lines (109 loc) · 3.43 KB
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
135
136
137
138
139
140
141
# interpolates times vehicle passes Jarvis and Bathurst streets
import csv
import ast
from scipy.spatial import distance
# print and save header of table for reference
header = []
with open("o12.csv") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
header = row
break
print header
# loop over table, fixing up the data to do stuff
out_table = []
out_table.append(['trip_id','t1','t2'])
q = 0
p = 0
r = 0
with open("o12.csv") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# recoding the times to a python list
times = row["times"]
times = times.replace("{","[")
times = times.replace("}","]")
times = ast.literal_eval(times)
#print len(times)
# line string to a list of coords
allgeom = row["allgeom"]
allgeom = allgeom.replace("LINESTRING","")
allgeom = allgeom.replace("(","['[")
allgeom = allgeom.replace(")","]']")
allgeom = allgeom.replace(",","]','[")
allgeom = allgeom.replace(" ",",")
allgeom = ast.literal_eval(allgeom)
#print len(allgeom)
if len(times) != len(allgeom):
print row["trip_id"]
print "times and coords do not match :("
break
# get the time stamp for the first point
int1pt = row['int1pt']
int1pt = int1pt.replace("POINT","")
int1pt = int1pt.replace("(","[")
int1pt = int1pt.replace(")","]")
int1pt = int1pt.replace(" ",",")
int1pt = ast.literal_eval(int1pt)
mind = []
m = 0
for pts in allgeom:
pts = ast.literal_eval(pts)
e = distance.euclidean(pts,int1pt)
if e < 150:
mindo = pts + [m, e]
mind.append(mindo)
m += 1
t1 = 0
if len(mind) < 1:
q += 1
else:
a_n = 0
a_d = 0
for pts in mind:
#print pts
#print allgeom[pts[2]]
#print times[pts[2]]
a_n = a_n + float(times[pts[2]]) * (100 - float(pts[3]))
a_d = a_d + (100 - float(pts[3]))
t1 = a_n / a_d
# get the time stamp for the second point
int2pt = row['int2pt']
int2pt = int2pt.replace("POINT","")
int2pt = int2pt.replace("(","[")
int2pt = int2pt.replace(")","]")
int2pt = int2pt.replace(" ",",")
int2pt = ast.literal_eval(int2pt)
mind2 = []
m = 0
for pts in allgeom:
pts = ast.literal_eval(pts)
e = distance.euclidean(pts,int2pt)
if e < 150:
mindo = pts + [m, e]
mind2.append(mindo)
m += 1
t2 = 0
if len(mind2) < 1:
p += 1
else:
a_n = 0
a_d = 0
for pts in mind2:
#print pts
#print allgeom[pts[2]]
#print times[pts[2]]
a_n = a_n + float(times[pts[2]]) * (100 - float(pts[3]))
a_d = a_d + (100 - float(pts[3]))
t2 = a_n / a_d
if t1 > 0 and t2 > 0:
outrow = [row["trip_id"],t1,t2]
out_table.append(outrow)
else:
r += 1
print q, p, r
print len(out_table) - 1
with open("times12.csv", 'w') as csvfile:
writer = csv.writer(csvfile)
for row in out_table:
writer.writerow(row)