-
Notifications
You must be signed in to change notification settings - Fork 73
/
utils.py
346 lines (266 loc) · 12.4 KB
/
utils.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import zipfile, io
import pandas as pd
import geopandas as gpd
import shapely.geometry as geo
import os
import numpy as np
REQUIRED_SLOTS = [
"agency", "stops", "routes", "trips", "stop_times"
]
OPTIONAL_SLOTS = [
"calendar", "calendar_dates", "fare_attributes", "fare_rules",
"shapes", "frequencies", "transfers", "pathways", "levels",
"feed_info", "translations", "attributions"
]
def read_feed(path):
feed = {}
with zipfile.ZipFile(path, "r") as zip:
available_slots = zip.namelist()
prefix = None
if "agency.txt" in available_slots:
prefix = ""
else:
for slot in available_slots:
if slot.endswith("agency.txt"):
prefix = slot.replace("agency.txt", "")
print("Warning: GTFS files seem to be located in: %s" % prefix)
break
if prefix is None:
raise RuntimeError("No GTFS data found in archive")
for slot in REQUIRED_SLOTS:
if not "%s%s.txt" % (prefix, slot) in available_slots:
raise RuntimeError("Missing GTFS information: %s" % slot)
if not "%scalendar.txt" % prefix in available_slots and not "%scalendar_dates.txt" % prefix in available_slots:
raise RuntimeError("At least calendar.txt or calendar_dates.txt must be specified.")
print("Loading GTFS data from %s ..." % path)
for slot in REQUIRED_SLOTS + OPTIONAL_SLOTS:
if "%s%s.txt" % (prefix, slot) in available_slots:
print(" Loading %s.txt ..." % slot)
with zip.open("%s%s.txt" % (prefix, slot)) as f:
feed[slot] = pd.read_csv(f, skipinitialspace = True)
else:
print(" Not loading %s.txt" % slot)
# Some cleanup
for slot in ("calendar", "calendar_dates", "trips"):
if slot in feed and "service_id" in feed[slot] and pd.api.types.is_string_dtype(feed[slot]["service_id"]):
initial_count = len(feed[slot])
feed[slot] = feed[slot][feed[slot]["service_id"].str.len() > 0]
final_count = len(feed[slot])
if final_count != initial_count:
print("WARNING Removed %d/%d entries from %s with empty service_id" % (
initial_count - final_count, initial_count, slot
))
if "stops" in feed:
df_stops = feed["stops"]
if not "parent_station" in df_stops:
print("WARNING Missing parent_station in stops, setting to NaN")
df_stops["parent_station"] = np.nan
if "transfers" in feed:
df_transfers = feed["transfers"]
if not "min_transfer_time" in df_transfers:
df_transfers["min_transfer_time"] = 0
f = df_transfers["min_transfer_time"].isna()
if np.any(f):
print("WARNING NaN numbers for min_transfer_time in transfers")
df_transfers = df_transfers[~f]
df_transfers["min_transfer_time"] = df_transfers["min_transfer_time"].astype(int)
feed["transfers"] = df_transfers
if "agency" in feed:
df_agency = feed["agency"]
df_agency.loc[df_agency["agency_id"].isna(), "agency_id"] = "generic"
if "routes" in feed:
df_routes = feed["routes"]
agency_id = feed["agency"]["agency_id"].values[0]
if not "agency_id" in df_routes:
df_routes["agency_id"] = agency_id
df_routes.loc[df_routes["agency_id"].isna(), "agency_id"] = agency_id
if "shapes" in feed: del feed["shapes"]
feed["trips"]["shape_id"] = np.nan
# Fixes for Nantes PDL
for item in feed.keys():
feed[item] = feed[item].drop(columns = [
c for c in feed[item].columns if c.startswith("ext_")
])
return feed
def write_feed(feed, path):
print("Writing GTFS data to %s ..." % path)
if path.endswith(".zip"):
with zipfile.ZipFile(path, "w") as zip:
for slot in REQUIRED_SLOTS + OPTIONAL_SLOTS:
if slot in feed:
print(" Writing %s.txt ..." % slot)
# We cannot write directly to the file handle as it
# is binary, but pandas only writes in text mode.
zip.writestr("%s.txt" % slot, feed[slot].to_csv(index = None))
else:
if not os.path.exists(path):
os.mkdir(path)
if not os.path.isdir(path):
raise RuntimeError("Should be a directory: %s" % path)
for slot in REQUIRED_SLOTS + OPTIONAL_SLOTS:
if slot in feed:
with open("%s/%s.txt" % (path, slot), "w+", encoding="utf-8") as f:
print(" Writing %s.txt ..." % slot)
feed[slot].to_csv(f, index = None, lineterminator='\n')
def cut_feed(feed, df_area, crs = None):
feed = copy_feed(feed)
df_stops = feed["stops"]
if np.count_nonzero(df_stops["location_type"] == 1) == 0:
print("Warning! Location types seem to be malformatted. Keeping all stops.")
df_stations = df_stops.copy()
else:
df_stations = df_stops[df_stops["location_type"] == 1].copy()
df_stations["geometry"] = [
geo.Point(*xy)
for xy in zip(df_stations["stop_lon"], df_stations["stop_lat"])
]
df_stations = gpd.GeoDataFrame(df_stations, crs = "EPSG:4326")
if not crs is None:
print("Converting stops to custom CRS", crs)
df_stations = df_stations.to_crs(crs)
elif not df_area.crs is None:
print("Converting stops to area CRS", df_area.crs)
df_stations = df_stations.to_crs(df_area.crs)
print("Filtering stations ...")
initial_count = len(df_stations)
df_stations = gpd.sjoin(df_stations, df_area, predicate = "within")
final_count = len(df_stations)
print("Found %d/%d stations inside the specified area" % (final_count, initial_count))
inside_stations = df_stations["stop_id"]
# 1) Remove stations that are not inside stations and not have a parent stop
df_stops = feed["stops"]
df_stops = df_stops[
df_stops["parent_station"].isin(inside_stations) |
(
df_stops["parent_station"].isna() &
df_stops["stop_id"].isin(inside_stations)
)
]
feed["stops"] = df_stops.copy()
remaining_stops = feed["stops"]["stop_id"].unique()
# 2) Remove stop times
df_times = feed["stop_times"]
df_times = df_times[df_times["stop_id"].astype(str).isin(remaining_stops.astype(str))]
feed["stop_times"] = df_times.copy()
# 3) Remove transfers
if "transfers" in feed:
df_transfers = feed["transfers"]
df_transfers = df_transfers[
df_transfers["from_stop_id"].isin(remaining_stops) &
df_transfers["to_stop_id"].isin(remaining_stops)
]
feed["transfers"] = df_transfers.copy()
# 4) Remove pathways
if "pathways" in feed:
df_pathways = feed["pathways"]
df_pathways = df_pathways[
df_pathways["from_stop_id"].isin(remaining_stops) &
df_pathways["to_stop_id"].isin(remaining_stops)
]
feed["pathways"] = df_pathways.copy()
# 5) Remove trips
trip_counts = feed["stop_times"]["trip_id"].value_counts()
remaining_trips = trip_counts[trip_counts > 1].index.values
df_trips = feed["trips"]
df_trips = df_trips[
df_trips["trip_id"].isin(remaining_trips)
]
feed["trips"] = df_trips.copy()
feed["stop_times"] = feed["stop_times"][
feed["stop_times"]["trip_id"].isin(df_trips["trip_id"].unique())
]
# 6) Remove frequencies
if "frequencies" in feed:
df_frequencies = feed["frequencies"]
df_frequencies = df_frequencies[
df_frequencies["trip_id"].isin(remaining_trips)
]
feed["frequencies"] = df_frequencies.copy()
return feed
SLOT_COLLISIONS = [
{ "slot": "agency", "identifier": "agency_id", "references": [
("routes", "agency_id"), ("fare_attributes", "agency_id")] },
{ "slot": "stops", "identifier": "stop_id", "references": [
("stops", "parent_station"), ("stop_times", "stop_id"),
("transfers", "from_stop_id"), ("transfers", "to_stop_id"),
("pathways", "from_stop_id"), ("pathways", "to_stop_id")] },
{ "slot": "routes", "identifier": "route_id", "references": [
("trips", "route_id"), ("fare_rules", "route_id"),
("attributions", "route_id")] },
{ "slot": "trips", "identifier": "trip_id", "references": [
("stop_times", "trip_id"), ("frequencies", "trip_id"),
("attributions", "trip_id")] },
{ "slot": "calendar", "identifier": "service_id", "references": [
("calendar_dates", "service_id"), ("trips", "service_id")] },
{ "slot": "calendar_dates", "identifier": "service_id", "references": [
("trips", "service_id"), ("calendar", "service_id")] },
{ "slot": "fare_attributes", "identifier": "fare_id", "references": [
("fare_rules", "fare_id")] },
{ "slot": "shapes", "identifier": "shape_id", "references": [
("trips", "shape_id")] },
{ "slot": "pathways", "identifier": "pathway_id", "references": [] },
{ "slot": "levels", "identifier": "level_id", "references": [
("stops", "level_id")] },
{ "slot": "attributions", "identifier": "attribution_id" },
]
def copy_feed(feed):
return {
slot: feed[slot].copy() for slot in feed
}
def merge_feeds(feeds):
result = {}
for k, feed in enumerate(feeds):
result = merge_two_feeds(result, feed, "_m{}".format(k + 1))
return result
def merge_two_feeds(first, second, suffix = "_merged"):
feed = {}
print("Merging GTFS data ...")
first = copy_feed(first)
second = copy_feed(second)
for collision in SLOT_COLLISIONS:
if collision["slot"] in first and collision["slot"] in second:
df_first = first[collision["slot"]]
df_second = second[collision["slot"]]
df_first[collision["identifier"]] = df_first[collision["identifier"]].astype(str)
df_second[collision["identifier"]] = df_second[collision["identifier"]].astype(str)
df_concat = pd.concat([df_first, df_second], sort = True).drop_duplicates()
duplicate_ids = list(df_concat[df_concat[collision["identifier"]].duplicated()][
collision["identifier"]].astype(str).unique())
if len(duplicate_ids) > 0:
print(" Found %d duplicate identifiers in %s" % (
len(duplicate_ids), collision["slot"]))
replacement_ids = [str(id) + suffix for id in duplicate_ids]
df_second[collision["identifier"]] = df_second[collision["identifier"]].replace(
duplicate_ids, replacement_ids
)
for ref_slot, ref_identifier in collision["references"]:
if ref_slot in first and ref_slot in second:
first[ref_slot][ref_identifier] = first[ref_slot][ref_identifier].astype(str)
second[ref_slot][ref_identifier] = second[ref_slot][ref_identifier].astype(str)
second[ref_slot][ref_identifier] = second[ref_slot][ref_identifier].replace(
duplicate_ids, replacement_ids
)
for slot in REQUIRED_SLOTS + OPTIONAL_SLOTS:
if slot in first and slot in second:
feed[slot] = pd.concat([first[slot], second[slot]], sort = True).drop_duplicates()
elif slot in first:
feed[slot] = first[slot].copy()
elif slot in second:
feed[slot] = second[slot].copy()
return feed
def despace_stop_ids(feed, replacement = ":::"):
feed = copy_feed(feed)
references = None
for item in SLOT_COLLISIONS:
if item["slot"] == "stops":
references = item["references"]
df_stops = feed["stops"]
df_stops["stop_id"] = df_stops["stop_id"].astype(str)
search_ids = list(df_stops[df_stops["stop_id"].str.contains(" ")]["stop_id"].unique())
replacement_ids = [item.replace(" ", replacement) for item in search_ids]
df_stops["stop_id"] = df_stops["stop_id"].replace(search_ids, replacement_ids)
for reference_slot, reference_field in references:
if reference_slot in feed:
feed[reference_slot][reference_field] = feed[reference_slot][reference_field].astype(str).replace(search_ids, replacement_ids)
print("De-spaced %d/%d stops" % (len(search_ids), len(df_stops)))
return feed