-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.py
283 lines (205 loc) · 7.44 KB
/
parse.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from math import radians, cos, sin, asin, sqrt
import resource
import datetime
import random
import io
import os.path
import json
import sys
# Make Python 2 compliant with FileNotFoundError from Python 3
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
# Check if all files are accounted for
gtfs_files = ["shapes", "stop_times", "stops", "trips"]
for file_path in gtfs_files:
file_path = "data/gtfs/" + file_path + ".txt"
if not os.path.exists(file_path):
raise FileNotFoundError(str(file_path) + " path missing file.")
gtfs_files = None
# Utilities
global_start_time = datetime.datetime.now()
def logOps(msg=None):
current_time = datetime.datetime.now()
elapsed_time = current_time - global_start_time
if msg != None:
print "Logged: " + str(msg)
memStr = str(round((float(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) / 1000000), 2))
print "\tCurrent mem usage: " + memStr + " mb.\n\tElapsed time: " + str(elapsed_time)
def calculateShapeDistances(shape):
runningDistanceTally = float(0)
for i, pt in enumerate(shape):
if i > 0:
runningDistanceTally += haversine(shape[i]["loc"], shape[i-1]["loc"])
shape[i]["d"] = round(runningDistanceTally, 2)
return shape
def haversine(pt1, pt2):
# unpack latitude/longitude
lat1, lng1 = pt1
lat2, lng2 = pt2
if lat1 == lat2 and lng1 == lng2:
return float(0)
else:
# convert all latitudes/longitudes from decimal degrees to radians
lat1, lng1, lat2, lng2 = map(radians, (lat1, lng1, lat2, lng2))
lat = float(lat2) - float(lat1)
lng = float(lng2) - float(lng1)
d = sin(lat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(lng / 2) ** 2
h = 2 * 6369087 * asin(sqrt(d))
return round(h, 2)
def getAllShapeIDs():
shapes_file = "data/gtfs/shapes.txt"
col_names = open(shapes_file).readline().rstrip().split(",")
loc_of_shape_id = col_names.index("shape_id")
with io.open(shapes_file, "r") as stream:
all_shape_ids = list()
for row in stream:
val = row.rstrip().split(",")[loc_of_shape_id]
if val != "shape_id" and val not in all_shape_ids:
val = str(val)
all_shape_ids.append(val)
stream.close()
return all_shape_ids
def getTripsForShape(shape_id):
trips = dict()
stop_ids = list()
trips_file = "data/gtfs/trips.txt"
col_names = open(trips_file).readline().rstrip().split(",")
loc_of_route_id = col_names.index("route_id")
loc_of_shape_id = col_names.index("shape_id")
loc_of_trip_id = col_names.index("trip_id")
with io.open(trips_file, "r") as stream:
for row in stream:
row = row.rstrip().split(",")
if row[loc_of_route_id] != "route_id":
if str(row[loc_of_shape_id]) == shape_id:
trips[str(row[loc_of_trip_id])] = []
stream.close()
shapes_times_file = "data/gtfs/stop_times.txt"
col_names_st = open(shapes_times_file).readline().rstrip().split(",")
loc_of_stop_id_st = col_names_st.index("stop_id")
loc_of_stop_sequence_st = col_names_st.index("stop_sequence")
loc_of_trip_id_st = col_names_st.index("trip_id")
with io.open(shapes_times_file, "r") as stream:
for row in stream:
row = row.rstrip().split(",")
if row[loc_of_trip_id_st] != "trip_id":
if row[loc_of_trip_id_st] in trips:
trips[str(row[loc_of_trip_id_st])].append({
"seq": int(row[loc_of_stop_sequence_st]),
"id": int(row[loc_of_stop_id_st])
})
new_stop_id = int(row[loc_of_stop_id_st])
if new_stop_id not in stop_ids:
stop_ids.append(new_stop_id)
stream.close()
def addLoc(stop_id, latlng):
ok = False
for trip in trips:
for stop_index, stop in enumerate(trips[trip]):
if stop["id"] == stop_id:
latlng = (float(row[3]), float(row[4]))
trips[trip][stop_index]["loc"] = latlng
ok = True
return ok
stops_file = "data/gtfs/stops.txt"
col_names_sf = open(stops_file).readline().rstrip().split(",")
loc_of_stop_id_sf = col_names_sf.index("stop_id")
loc_of_stop_lat_sf = col_names_sf.index("stop_lat")
loc_of_stop_lon_sf = col_names_sf.index("stop_lon")
with io.open(stops_file, "r") as stream:
for row in stream:
row = row.rstrip().split(",")
if row[loc_of_stop_id_sf] != "stop_id":
stop_id = int(row[loc_of_stop_id_sf])
if stop_id in stop_ids:
latlng = (float(row[loc_of_stop_lat_sf]), float(row[loc_of_stop_lon_sf]))
success = addLoc(stop_id, latlng)
if success == True:
stop_ids.remove(stop_id)
else:
raise Exception("Stop ID (" + str(stop_id) + ") missing in stops.txt")
stream.close()
missing = len(stop_ids)
if missing == 0:
# sort each trip by sequence so results in order
for trip in trips:
trips[trip] = sorted(trips[trip], key=lambda k: k["seq"], reverse=False)
return trips
else:
raise Exception("Incomplete trip data, " + str(missing) + " bad objects")
def getShape(shape_id):
shapes_file = "data/gtfs/shapes.txt"
col_names = open(shapes_file).readline().rstrip().split(",")
loc_of_shape_id = col_names.index("shape_id")
loc_of_shape_pt_lat = col_names.index("shape_pt_lat")
loc_of_shape_pt_lon = col_names.index("shape_pt_lon")
loc_of_shape_pt_sequence = col_names.index("shape_pt_sequence")
shape = list()
with io.open("data/gtfs/shapes.txt", "r") as stream:
shape = list()
for row in stream:
row = row.rstrip().split(",")
sh_id = str(row[loc_of_shape_id])
if sh_id != "shape_id" and sh_id == shape_id:
shape.append({
"loc": (float(row[loc_of_shape_pt_lat]), float(row[loc_of_shape_pt_lon])),
"seq": int(row[loc_of_shape_pt_sequence])
})
stream.close()
# order by id increasing
shape = sorted(shape, key=lambda k: k["seq"], reverse=False)
shape = calculateShapeDistances(shape)
return shape
# Begin streaming the CSV
# line struct: shape_index, stop_sequence, trip_index, stop_id, stop_lat, stop_lon
logOps("Starting the get all shapes stream.")
allShapeIDs = getAllShapeIDs()
logOps("Finished the get all shapes stream.")
output = open('data/out.csv', 'w')
output.write("shape_index,stop_id,dist\n")
output.close()
for shape_index, shape_id in enumerate(allShapeIDs):
trips = getTripsForShape(shape_id)
shape = getShape(shape_id)
for tripID in trips:
tripshape = list(shape)
trip = trips[tripID]
for stop_i, stop_pt in enumerate(trip):
minDist = None
closest = None
for shape_i, shape_pt in enumerate(tripshape):
thisDist = None
if stop_i == 0:
thisDist = haversine(stop_pt["loc"], shape_pt["loc"])
else:
shape_prev = tripshape[shape_i-1]
thisDist = haversine(stop_pt["loc"], shape_pt["loc"]) + haversine(stop_pt["loc"], shape_prev["loc"])
if closest == None or closest > thisDist:
closest = thisDist
if stop_i == 0:
minDist = 0
else:
minDist = round(shape_prev["d"], 2)
trip[stop_i]["d"] = minDist
# DEBUG catch "backwards" points
if stop_i > 0:
if trip[stop_i-1]["d"] > trip[stop_i]["d"]:
print "\nIssue with shape " + str(shape_id) + " and trip id " + str(tripID)
print "Current stop: " + str(stop_i) + str(trip[stop_i]) + str(trip[stop_i]["d"])
print "Previous stop: " + str(stop_i-1) + str(trip[stop_i-1]) + str(trip[stop_i-1]["d"])
new_line = ",".join([str(shape_id), str(trip[stop_i]["id"]), str(trip[stop_i]["d"])])
new_line += "\n"
output = open('data/out.csv', 'a')
output.write(new_line)
output.close()
# Completed a tripID
trip = None
tripshape = None
# Completed trips and shape
trips = None
shape = None
logOps("Completed operations for shape_id " + str(shape_id) + " (" + str(shape_index + 1) + "/" + str(len(allShapeIDs)) + ")")
print "Done"
sys.exit()