gtfs_kit 2.1.0__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/PKG-INFO +1 -1
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/__init__.py +1 -1
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/cleaners.py +80 -50
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/pyproject.toml +1 -1
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/setup.py +1 -1
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/LICENSE.txt +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/README.rst +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/calendar.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/constants.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/feed.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/helpers.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/miscellany.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/routes.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/shapes.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/stop_times.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/stops.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/trips.py +0 -0
- {gtfs_kit-2.1.0 → gtfs_kit-2.2.0}/gtfs_kit/validators.py +0 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Functions about cleaning feeds.
|
|
3
3
|
"""
|
|
4
4
|
import math
|
|
5
|
-
from typing import TYPE_CHECKING
|
|
5
|
+
from typing import TYPE_CHECKING, Dict
|
|
6
6
|
|
|
7
7
|
import pandas as pd
|
|
8
8
|
import numpy as np
|
|
@@ -167,6 +167,36 @@ def clean_route_short_names(feed: "Feed") -> "Feed":
|
|
|
167
167
|
return feed
|
|
168
168
|
|
|
169
169
|
|
|
170
|
+
def build_aggregate_routes_dict(
|
|
171
|
+
routes: pd.DataFrame, by: str = "route_short_name", route_id_prefix: str = "route_"
|
|
172
|
+
) -> Dict[str, str]:
|
|
173
|
+
"""
|
|
174
|
+
Given a DataFrame of routes, group the routes by route short name, say,
|
|
175
|
+
and assign new route IDs using the given prefix.
|
|
176
|
+
Return a dictionary of the form <old route ID> -> <new route ID>.
|
|
177
|
+
Helper function for :func:`aggregate_routes`.
|
|
178
|
+
|
|
179
|
+
More specifically, group ``routes`` by the ``by`` column, and for each group make
|
|
180
|
+
one new route ID for all the old route IDs in that group based on the given
|
|
181
|
+
``route_id_prefix`` string and a running count, e.g. ``'route_013'``.
|
|
182
|
+
"""
|
|
183
|
+
if by not in routes.columns:
|
|
184
|
+
raise ValueError(f"Column {by} not in routes.")
|
|
185
|
+
|
|
186
|
+
# Create new route IDs
|
|
187
|
+
n = routes.groupby(by).ngroups
|
|
188
|
+
k = int(math.log10(n)) + 1 # Number of digits for padding IDs
|
|
189
|
+
nid_by_oid = dict()
|
|
190
|
+
i = 1
|
|
191
|
+
for col, group in routes.groupby(by):
|
|
192
|
+
nid = f"{route_id_prefix}{i:0{k}d}"
|
|
193
|
+
d = {oid: nid for oid in group.route_id.values}
|
|
194
|
+
nid_by_oid.update(d)
|
|
195
|
+
i += 1
|
|
196
|
+
|
|
197
|
+
return nid_by_oid
|
|
198
|
+
|
|
199
|
+
|
|
170
200
|
def aggregate_routes(
|
|
171
201
|
feed: "Feed", by: str = "route_short_name", route_id_prefix: str = "route_"
|
|
172
202
|
) -> "Feed":
|
|
@@ -174,87 +204,87 @@ def aggregate_routes(
|
|
|
174
204
|
Aggregate routes by route short name, say, and assign new route IDs using the
|
|
175
205
|
given prefix.
|
|
176
206
|
|
|
177
|
-
More specifically,
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
string and a running count, e.g. ``'route_013'``
|
|
183
|
-
3. Assign all the trips associated with routes in the group to
|
|
184
|
-
that first route
|
|
185
|
-
4. Update the route IDs in the other "Feed" tables
|
|
186
|
-
|
|
207
|
+
More specifically, create new route IDs with the function
|
|
208
|
+
:func:`build_aggregate_routes_dict` and the parameters ``by`` and
|
|
209
|
+
``route_id_prefix`` and update the old route IDs to the new ones in all the relevant
|
|
210
|
+
Feed tables.
|
|
211
|
+
Return the resulting Feed.
|
|
187
212
|
"""
|
|
188
|
-
if by not in feed.routes.columns:
|
|
189
|
-
raise ValueError(f"Column {by} not in feed.routes")
|
|
190
|
-
|
|
191
213
|
feed = feed.copy()
|
|
192
214
|
|
|
193
|
-
#
|
|
215
|
+
# Make new route IDs
|
|
194
216
|
routes = feed.routes
|
|
195
|
-
|
|
196
|
-
k = int(math.log10(n)) + 1 # Number of digits for padding IDs
|
|
197
|
-
nrid_by_orid = dict()
|
|
198
|
-
i = 1
|
|
199
|
-
for col, group in routes.groupby(by):
|
|
200
|
-
nrid = f"route_{i:0{k}d}"
|
|
201
|
-
d = {orid: nrid for orid in group["route_id"].values}
|
|
202
|
-
nrid_by_orid.update(d)
|
|
203
|
-
i += 1
|
|
217
|
+
nid_by_oid = build_aggregate_routes_dict(routes, by, route_id_prefix)
|
|
204
218
|
|
|
205
|
-
|
|
219
|
+
# Update to new route IDs
|
|
220
|
+
routes["route_id"] = routes["route_id"].map(lambda x: nid_by_oid[x])
|
|
206
221
|
routes = routes.groupby(by).first().reset_index()
|
|
207
222
|
feed.routes = routes
|
|
208
223
|
|
|
209
224
|
# Update route IDs of trips
|
|
210
225
|
trips = feed.trips
|
|
211
|
-
trips["route_id"] = trips["route_id"].map(lambda x:
|
|
226
|
+
trips["route_id"] = trips["route_id"].map(lambda x: nid_by_oid[x])
|
|
212
227
|
feed.trips = trips
|
|
213
228
|
|
|
214
229
|
# Update route IDs of transfers
|
|
215
230
|
if feed.transfers is not None:
|
|
216
231
|
transfers = feed.transfers
|
|
217
|
-
transfers["route_id"] = transfers["route_id"].map(lambda x:
|
|
232
|
+
transfers["route_id"] = transfers["route_id"].map(lambda x: nid_by_oid[x])
|
|
218
233
|
feed.transfers = transfers
|
|
219
234
|
|
|
220
235
|
return feed
|
|
221
236
|
|
|
222
237
|
|
|
223
|
-
def
|
|
224
|
-
|
|
225
|
-
) ->
|
|
238
|
+
def build_aggregate_stops_dict(
|
|
239
|
+
stops: pd.DataFrame, by: str = "stop_code", stop_id_prefix: str = "stop_"
|
|
240
|
+
) -> Dict[str, str]:
|
|
226
241
|
"""
|
|
227
|
-
|
|
228
|
-
given prefix.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
string and a running count, e.g. ``'stop_013'``
|
|
236
|
-
3. Assign all the stops associated with stops in the group to
|
|
237
|
-
that first stop
|
|
238
|
-
4. Update the stop IDs in the other "Feed" tables
|
|
239
|
-
|
|
242
|
+
Given a DataFrame of stops, group the stops by stop code, say,
|
|
243
|
+
and assign new stop IDs using the given prefix.
|
|
244
|
+
Return a dictionary of the form <old stop ID> -> <new stop ID>.
|
|
245
|
+
Helper function for :func:`aggregate_stops`.
|
|
246
|
+
|
|
247
|
+
More specifically, group ``stops`` by the ``by`` column, and for each group make
|
|
248
|
+
one new stop ID for all the old stops IDs in that group based on the given
|
|
249
|
+
``stop_id_prefix`` string and a running count, e.g. ``'stop_013'``.
|
|
240
250
|
"""
|
|
241
|
-
if by not in
|
|
242
|
-
raise ValueError(f"Column {by} not in
|
|
243
|
-
|
|
244
|
-
feed = feed.copy()
|
|
251
|
+
if by not in stops.columns:
|
|
252
|
+
raise ValueError(f"Column {by} not in stops.")
|
|
245
253
|
|
|
246
254
|
# Create new stop IDs
|
|
247
|
-
stops = feed.stops
|
|
248
255
|
n = stops.groupby(by).ngroups
|
|
249
256
|
k = int(math.log10(n)) + 1 # Number of digits for padding IDs
|
|
250
257
|
nid_by_oid = dict()
|
|
251
258
|
i = 1
|
|
252
259
|
for col, group in stops.groupby(by):
|
|
253
|
-
nid = f"
|
|
254
|
-
d = {oid: nid for oid in group
|
|
260
|
+
nid = f"{stop_id_prefix}{i:0{k}d}"
|
|
261
|
+
d = {oid: nid for oid in group.stop_id.values}
|
|
255
262
|
nid_by_oid.update(d)
|
|
256
263
|
i += 1
|
|
257
264
|
|
|
265
|
+
return nid_by_oid
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def aggregate_stops(
|
|
269
|
+
feed: "Feed", by: str = "stop_code", stop_id_prefix: str = "stop_"
|
|
270
|
+
) -> "Feed":
|
|
271
|
+
"""
|
|
272
|
+
Aggregate routes by route short name, say, and assign new route IDs using the
|
|
273
|
+
given prefix.
|
|
274
|
+
|
|
275
|
+
More specifically, create new stop IDs with the function
|
|
276
|
+
:func:`build_aggregate_stops_dict` and the parameters ``by`` and
|
|
277
|
+
``stop_id_prefix`` and update the old stop IDs to the new ones in all the relevant
|
|
278
|
+
Feed tables.
|
|
279
|
+
Return the resulting Feed.
|
|
280
|
+
"""
|
|
281
|
+
feed = feed.copy()
|
|
282
|
+
|
|
283
|
+
# Make new stop ID by old stop ID dict
|
|
284
|
+
stops = feed.stops
|
|
285
|
+
nid_by_oid = build_aggregate_stops_dict(stops, by, stop_id_prefix)
|
|
286
|
+
|
|
287
|
+
# Apply dict
|
|
258
288
|
stops["stop_id"] = stops.stop_id.map(nid_by_oid)
|
|
259
289
|
if "parent_station" in stops:
|
|
260
290
|
stops["parent_station"] = stops.parent_station.map(nid_by_oid)
|
|
@@ -19,7 +19,7 @@ install_requires = \
|
|
|
19
19
|
|
|
20
20
|
setup_kwargs = {
|
|
21
21
|
'name': 'gtfs-kit',
|
|
22
|
-
'version': '2.
|
|
22
|
+
'version': '2.2.0',
|
|
23
23
|
'description': 'A Python 3.6+ library for analyzing GTFS feeds.',
|
|
24
24
|
'long_description': 'GTFS Kit\n********\n.. image:: https://travis-ci.org/mrcagney/gtfs_kit.svg?branch=master\n :target: https://travis-ci.org/mrcagney/gtfs_kit\n\nGTFS Kit is a Python 3.6+ kit for analyzing `General Transit Feed Specification (GTFS) <https://en.wikipedia.org/wiki/GTFS>`_ data in memory without a database.\nIt uses Pandas and Shapely to do the heavy lifting.\n\nThis project supersedes `GTFSTK <https://github.com/mrcagney/gtfstk>`_.\n\n\nInstallation\n=============\n``pip install gtfs_kit``.\n\n\nExamples\n========\nYou can play with ``ipynb/examples.ipynb`` in a Jupyter notebook.\n\n\nAuthors\n=========\n- Alex Raichev, 2019-09\n\n\nDocumentation\n=============\nOn Github Pages `here <https://mrcagney.github.io/gtfs_kit_docs>`_.\n\n\nNotes\n=====\n- Development status is Alpha\n- This project uses semantic versioning\n- Thanks to `MRCagney <http://www.mrcagney.com/>`_ for donating to this project\n- Constructive feedback and code contributions welcome. Please issue pull requests into the ``develop`` branch and include tests.\n',
|
|
25
25
|
'author': 'Alex Raichev',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|