gtfs_kit 1.0.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-1.0.0/LICENSE.txt +21 -0
- gtfs_kit-1.0.0/PKG-INFO +63 -0
- gtfs_kit-1.0.0/README.rst +37 -0
- gtfs_kit-1.0.0/gtfs_kit/__init__.py +15 -0
- gtfs_kit-1.0.0/gtfs_kit/calendar.py +99 -0
- gtfs_kit-1.0.0/gtfs_kit/cleaners.py +269 -0
- gtfs_kit-1.0.0/gtfs_kit/constants.py +154 -0
- gtfs_kit-1.0.0/gtfs_kit/feed.py +470 -0
- gtfs_kit-1.0.0/gtfs_kit/helpers.py +533 -0
- gtfs_kit-1.0.0/gtfs_kit/miscellany.py +1052 -0
- gtfs_kit-1.0.0/gtfs_kit/routes.py +843 -0
- gtfs_kit-1.0.0/gtfs_kit/shapes.py +212 -0
- gtfs_kit-1.0.0/gtfs_kit/stop_times.py +150 -0
- gtfs_kit-1.0.0/gtfs_kit/stops.py +757 -0
- gtfs_kit-1.0.0/gtfs_kit/trips.py +553 -0
- gtfs_kit-1.0.0/gtfs_kit/validators.py +1589 -0
- gtfs_kit-1.0.0/pyproject.toml +35 -0
- gtfs_kit-1.0.0/setup.py +35 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Alexander Raichev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
gtfs_kit-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: gtfs-kit
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A Python 3.6+ library for analyzing GTFS feeds.
|
|
5
|
+
Home-page: https://github.com/mrcagney/gtfs_kit
|
|
6
|
+
License: MIT
|
|
7
|
+
Author: Alex Raichev
|
|
8
|
+
Author-email: araichev@mrcagney.com
|
|
9
|
+
Requires-Python: >=3.6,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
+
Requires-Dist: folium (<1)
|
|
15
|
+
Requires-Dist: geopandas (<1)
|
|
16
|
+
Requires-Dist: json2table (>=1.1,<2.0)
|
|
17
|
+
Requires-Dist: pandas (<1)
|
|
18
|
+
Requires-Dist: pycountry (>=18.5,<19.0)
|
|
19
|
+
Requires-Dist: rtree (<1)
|
|
20
|
+
Requires-Dist: shapely (<2)
|
|
21
|
+
Requires-Dist: utm (<1)
|
|
22
|
+
Project-URL: Documentation, https://mrcagney.github.io/gtfs_kit_docs
|
|
23
|
+
Project-URL: Repository, https://github.com/mrcagney/gtfs_kit
|
|
24
|
+
Description-Content-Type: text/x-rst
|
|
25
|
+
|
|
26
|
+
GTFS Kit
|
|
27
|
+
********
|
|
28
|
+
.. image:: https://travis-ci.org/mrcagney/gtfs_kit.svg?branch=master
|
|
29
|
+
:target: https://travis-ci.org/mrcagney/gtfs_kit
|
|
30
|
+
|
|
31
|
+
GTFS 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.
|
|
32
|
+
It uses Pandas and Shapely to do the heavy lifting.
|
|
33
|
+
|
|
34
|
+
This project supersedes `GTFSTK <https://github.com/mrcagney/gtfstk>`_.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
Installation
|
|
38
|
+
=============
|
|
39
|
+
``pip install gtfs_kit``.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
Examples
|
|
43
|
+
========
|
|
44
|
+
You can play with ``ipynb/examples.ipynb`` in a Jupyter notebook.
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
Authors
|
|
48
|
+
=========
|
|
49
|
+
- Alex Raichev, 2019-09
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
Documentation
|
|
53
|
+
=============
|
|
54
|
+
On Github Pages `here <https://mrcagney.github.io/gtfs_kit_docs>`_.
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
Notes
|
|
58
|
+
=====
|
|
59
|
+
- Development status is Alpha
|
|
60
|
+
- This project uses semantic versioning
|
|
61
|
+
- Thanks to `MRCagney <http://www.mrcagney.com/>`_ for donating to this project
|
|
62
|
+
- Constructive feedback and code contributions welcome. Please issue pull requests into the ``develop`` branch and include tests.
|
|
63
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
GTFS Kit
|
|
2
|
+
********
|
|
3
|
+
.. image:: https://travis-ci.org/mrcagney/gtfs_kit.svg?branch=master
|
|
4
|
+
:target: https://travis-ci.org/mrcagney/gtfs_kit
|
|
5
|
+
|
|
6
|
+
GTFS 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.
|
|
7
|
+
It uses Pandas and Shapely to do the heavy lifting.
|
|
8
|
+
|
|
9
|
+
This project supersedes `GTFSTK <https://github.com/mrcagney/gtfstk>`_.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Installation
|
|
13
|
+
=============
|
|
14
|
+
``pip install gtfs_kit``.
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
Examples
|
|
18
|
+
========
|
|
19
|
+
You can play with ``ipynb/examples.ipynb`` in a Jupyter notebook.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
Authors
|
|
23
|
+
=========
|
|
24
|
+
- Alex Raichev, 2019-09
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
Documentation
|
|
28
|
+
=============
|
|
29
|
+
On Github Pages `here <https://mrcagney.github.io/gtfs_kit_docs>`_.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
Notes
|
|
33
|
+
=====
|
|
34
|
+
- Development status is Alpha
|
|
35
|
+
- This project uses semantic versioning
|
|
36
|
+
- Thanks to `MRCagney <http://www.mrcagney.com/>`_ for donating to this project
|
|
37
|
+
- Constructive feedback and code contributions welcome. Please issue pull requests into the ``develop`` branch and include tests.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .constants import *
|
|
2
|
+
from .helpers import *
|
|
3
|
+
from .calendar import *
|
|
4
|
+
from .routes import *
|
|
5
|
+
from .shapes import *
|
|
6
|
+
from .stops import *
|
|
7
|
+
from .stop_times import *
|
|
8
|
+
from .trips import *
|
|
9
|
+
from .miscellany import *
|
|
10
|
+
from .cleaners import *
|
|
11
|
+
from .validators import *
|
|
12
|
+
from .feed import *
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
__version__ = "1.0.0"
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Functions about calendar and calendar_dates.
|
|
3
|
+
"""
|
|
4
|
+
import dateutil.relativedelta as rd
|
|
5
|
+
from typing import List, TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from . import helpers as hp
|
|
8
|
+
|
|
9
|
+
# Help mypy but avoid circular imports
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from .feed import Feed
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_dates(feed: "Feed", *, as_date_obj: bool = False) -> List[str]:
|
|
15
|
+
"""
|
|
16
|
+
Return a list of YYYYMMDD date strings for which the given Feed is valid,
|
|
17
|
+
which could be the empty list if the Feed has no calendar information.
|
|
18
|
+
|
|
19
|
+
If ``as_date_obj``, then return datetime.date objects instead.
|
|
20
|
+
"""
|
|
21
|
+
dates = []
|
|
22
|
+
if feed.calendar is not None and not feed.calendar.empty:
|
|
23
|
+
if "start_date" in feed.calendar.columns:
|
|
24
|
+
dates.append(feed.calendar["start_date"].min())
|
|
25
|
+
if "end_date" in feed.calendar.columns:
|
|
26
|
+
dates.append(feed.calendar["end_date"].max())
|
|
27
|
+
if feed.calendar_dates is not None and not feed.calendar_dates.empty:
|
|
28
|
+
if "date" in feed.calendar_dates.columns:
|
|
29
|
+
start = feed.calendar_dates["date"].min()
|
|
30
|
+
end = feed.calendar_dates["date"].max()
|
|
31
|
+
dates.extend([start, end])
|
|
32
|
+
if not dates:
|
|
33
|
+
return []
|
|
34
|
+
|
|
35
|
+
start_date, end_date = min(dates), max(dates)
|
|
36
|
+
start_date, end_date = map(hp.datestr_to_date, [start_date, end_date])
|
|
37
|
+
num_days = (end_date - start_date).days
|
|
38
|
+
result = [
|
|
39
|
+
start_date + rd.relativedelta(days=+d) for d in range(num_days + 1)
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
# Convert dates back to strings if required
|
|
43
|
+
if not as_date_obj:
|
|
44
|
+
result = [hp.datestr_to_date(x, inverse=True) for x in result]
|
|
45
|
+
|
|
46
|
+
return result
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def get_week(feed: "Feed", k: int, *, as_date_obj: bool = False) -> List[str]:
|
|
50
|
+
"""
|
|
51
|
+
Given a Feed and a positive integer ``k``,
|
|
52
|
+
return a list of YYYYMMDD date strings corresponding to the kth Monday--Sunday week
|
|
53
|
+
(or initial segment thereof) for which the Feed is valid.
|
|
54
|
+
For example, k=1 returns the first Monday--Sunday week (or initial segment thereof).
|
|
55
|
+
If the Feed does not have k Mondays, then return the empty list.
|
|
56
|
+
|
|
57
|
+
If ``as_date_obj``, then return datetime.date objects instead.
|
|
58
|
+
"""
|
|
59
|
+
dates = feed.get_dates(as_date_obj=True)
|
|
60
|
+
n = len(dates)
|
|
61
|
+
|
|
62
|
+
# Get first Monday
|
|
63
|
+
monday_index = None
|
|
64
|
+
for (i, date) in enumerate(dates):
|
|
65
|
+
if date.weekday() == 0:
|
|
66
|
+
monday_index = i
|
|
67
|
+
break
|
|
68
|
+
|
|
69
|
+
# Get week k
|
|
70
|
+
if k < 1 or monday_index is None or monday_index + 7 * (k - 1) > n:
|
|
71
|
+
result = []
|
|
72
|
+
else:
|
|
73
|
+
result = dates[monday_index + 7 * (k - 1) : monday_index + 7 * k]
|
|
74
|
+
|
|
75
|
+
# Convert to date strings if requested
|
|
76
|
+
if not as_date_obj:
|
|
77
|
+
result = [hp.datestr_to_date(x, inverse=True) for x in result]
|
|
78
|
+
|
|
79
|
+
return result
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def get_first_week(feed: "Feed", *, as_date_obj: bool = False) -> List[str]:
|
|
83
|
+
"""
|
|
84
|
+
Return a list of YYYYMMDD date strings for the first Monday--Sunday
|
|
85
|
+
week (or initial segment thereof) for which the given Feed is valid.
|
|
86
|
+
If the feed has no Mondays, then return the empty list.
|
|
87
|
+
|
|
88
|
+
If ``as_date_obj``, then return date objects, otherwise return date strings.
|
|
89
|
+
"""
|
|
90
|
+
return get_week(feed, 1, as_date_obj=as_date_obj)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def subset_dates(feed: "Feed", dates: List[str]) -> List[str]:
|
|
94
|
+
"""
|
|
95
|
+
Given a Feed and a list of YYYYMMDD date strings,
|
|
96
|
+
return the sublist of dates that lie in the Feed's dates
|
|
97
|
+
(the output :func:`feed.get_dates`).
|
|
98
|
+
"""
|
|
99
|
+
return [d for d in dates if d in feed.get_dates()]
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Functions about cleaning feeds.
|
|
3
|
+
"""
|
|
4
|
+
import math
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
import pandas as pd
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
from . import constants as cs
|
|
11
|
+
|
|
12
|
+
# Help mypy but avoid circular imports
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from .feed import Feed
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def clean_column_names(df: pd.DataFrame) -> pd.DataFrame:
|
|
18
|
+
"""
|
|
19
|
+
Strip the whitespace from all column names in the given DataFrame
|
|
20
|
+
and return the result.
|
|
21
|
+
"""
|
|
22
|
+
f = df.copy()
|
|
23
|
+
f.columns = [col.strip() for col in f.columns]
|
|
24
|
+
return f
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def clean_ids(feed: "Feed") -> "Feed":
|
|
28
|
+
"""
|
|
29
|
+
In the given Feed, strip whitespace from all string IDs and
|
|
30
|
+
then replace every remaining whitespace chunk with an underscore.
|
|
31
|
+
Return the resulting Feed.
|
|
32
|
+
"""
|
|
33
|
+
# Alter feed inputs only, and build a new feed from them.
|
|
34
|
+
# The derived feed attributes, such as feed.trips_i,
|
|
35
|
+
# will be automatically handled when creating the new feed.
|
|
36
|
+
feed = feed.copy()
|
|
37
|
+
|
|
38
|
+
for table in cs.GTFS_REF["table"].unique():
|
|
39
|
+
f = getattr(feed, table)
|
|
40
|
+
if f is None:
|
|
41
|
+
continue
|
|
42
|
+
for column in cs.GTFS_REF.loc[cs.GTFS_REF["table"] == table, "column"]:
|
|
43
|
+
if column in f.columns and column.endswith("_id"):
|
|
44
|
+
try:
|
|
45
|
+
f[column] = f[column].str.strip().str.replace(r"\s+", "_")
|
|
46
|
+
setattr(feed, table, f)
|
|
47
|
+
except AttributeError:
|
|
48
|
+
# Column is not of string type
|
|
49
|
+
continue
|
|
50
|
+
|
|
51
|
+
return feed
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def clean_times(feed: "Feed") -> "Feed":
|
|
55
|
+
"""
|
|
56
|
+
In the given Feed, convert H:MM:SS time strings to HH:MM:SS time
|
|
57
|
+
strings to make sorting by time work as expected.
|
|
58
|
+
Return the resulting Feed.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def reformat(t):
|
|
62
|
+
if pd.isna(t):
|
|
63
|
+
return t
|
|
64
|
+
t = t.strip()
|
|
65
|
+
if len(t) == 7:
|
|
66
|
+
t = "0" + t
|
|
67
|
+
return t
|
|
68
|
+
|
|
69
|
+
feed = feed.copy()
|
|
70
|
+
tables_and_columns = [
|
|
71
|
+
("stop_times", ["arrival_time", "departure_time"]),
|
|
72
|
+
("frequencies", ["start_time", "end_time"]),
|
|
73
|
+
]
|
|
74
|
+
for table, columns in tables_and_columns:
|
|
75
|
+
f = getattr(feed, table)
|
|
76
|
+
if f is not None:
|
|
77
|
+
f[columns] = f[columns].applymap(reformat)
|
|
78
|
+
setattr(feed, table, f)
|
|
79
|
+
|
|
80
|
+
return feed
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def drop_zombies(feed: "Feed") -> "Feed":
|
|
84
|
+
"""
|
|
85
|
+
In the given Feed, do the following in order and return the resulting Feed.
|
|
86
|
+
|
|
87
|
+
1. Drop stops of location type 0 or NaN with no stop times.
|
|
88
|
+
2. Remove undefined parent stations from the ``parent_station`` column.
|
|
89
|
+
3. Drop trips with no stop times.
|
|
90
|
+
4. Drop shapes with no trips.
|
|
91
|
+
5. Drop routes with no trips.
|
|
92
|
+
6. Drop services with no trips.
|
|
93
|
+
|
|
94
|
+
"""
|
|
95
|
+
feed = feed.copy()
|
|
96
|
+
|
|
97
|
+
f = feed.stops.copy()
|
|
98
|
+
ids = feed.stop_times.stop_id.unique()
|
|
99
|
+
cond = f.stop_id.isin(ids)
|
|
100
|
+
if "location_type" in f.columns:
|
|
101
|
+
cond |= ~f.location_type.isin([0, np.nan])
|
|
102
|
+
feed.stops = f[cond].copy()
|
|
103
|
+
|
|
104
|
+
# Remove undefined parent stations from the ``parent_station`` column
|
|
105
|
+
if "parent_station" in feed.stops.columns:
|
|
106
|
+
f = feed.stops.copy()
|
|
107
|
+
ids = f.stop_id.unique()
|
|
108
|
+
f["parent_station"] = f.parent_station.map(
|
|
109
|
+
lambda x: x if x in ids else np.nan
|
|
110
|
+
)
|
|
111
|
+
feed.stops = f
|
|
112
|
+
|
|
113
|
+
# Drop trips with no stop times
|
|
114
|
+
ids = feed.stop_times["trip_id"].unique()
|
|
115
|
+
f = feed.trips
|
|
116
|
+
feed.trips = f[f["trip_id"].isin(ids)]
|
|
117
|
+
|
|
118
|
+
# Drop shapes with no trips
|
|
119
|
+
ids = feed.trips["shape_id"].unique()
|
|
120
|
+
f = feed.shapes
|
|
121
|
+
if f is not None:
|
|
122
|
+
feed.shapes = f[f["shape_id"].isin(ids)]
|
|
123
|
+
|
|
124
|
+
# Drop routes with no trips
|
|
125
|
+
ids = feed.trips["route_id"].unique()
|
|
126
|
+
f = feed.routes
|
|
127
|
+
feed.routes = f[f["route_id"].isin(ids)]
|
|
128
|
+
|
|
129
|
+
# Drop services with no trips
|
|
130
|
+
ids = feed.trips["service_id"].unique()
|
|
131
|
+
if feed.calendar is not None:
|
|
132
|
+
f = feed.calendar
|
|
133
|
+
feed.calendar = f[f["service_id"].isin(ids)]
|
|
134
|
+
if feed.calendar_dates is not None:
|
|
135
|
+
f = feed.calendar_dates
|
|
136
|
+
feed.calendar_dates = f[f["service_id"].isin(ids)]
|
|
137
|
+
|
|
138
|
+
return feed
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def clean_route_short_names(feed: "Feed") -> "Feed":
|
|
142
|
+
"""
|
|
143
|
+
In ``feed.routes``, assign 'n/a' to missing route short names and
|
|
144
|
+
strip whitespace from route short names.
|
|
145
|
+
Then disambiguate each route short name that is duplicated by
|
|
146
|
+
appending '-' and its route ID.
|
|
147
|
+
Return the resulting Feed.
|
|
148
|
+
"""
|
|
149
|
+
feed = feed.copy()
|
|
150
|
+
r = feed.routes
|
|
151
|
+
if r is None:
|
|
152
|
+
return feed
|
|
153
|
+
|
|
154
|
+
# Fill NaNs and strip whitespace
|
|
155
|
+
r["route_short_name"] = r["route_short_name"].fillna("n/a").str.strip()
|
|
156
|
+
|
|
157
|
+
# Disambiguate
|
|
158
|
+
def disambiguate(row):
|
|
159
|
+
rsn, rid = row
|
|
160
|
+
return rsn + "-" + rid
|
|
161
|
+
|
|
162
|
+
r["dup"] = r["route_short_name"].duplicated(keep=False)
|
|
163
|
+
r.loc[r["dup"], "route_short_name"] = r.loc[
|
|
164
|
+
r["dup"], ["route_short_name", "route_id"]
|
|
165
|
+
].apply(disambiguate, axis=1)
|
|
166
|
+
del r["dup"]
|
|
167
|
+
|
|
168
|
+
feed.routes = r
|
|
169
|
+
return feed
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def aggregate_routes(
|
|
173
|
+
feed: "Feed", by: str = "route_short_name", route_id_prefix: str = "route_"
|
|
174
|
+
) -> "Feed":
|
|
175
|
+
"""
|
|
176
|
+
Aggregate routes by route short name, say, and assign new route IDs using the
|
|
177
|
+
given prefix.
|
|
178
|
+
|
|
179
|
+
More specifically, the result is built from the given Feed as follows.
|
|
180
|
+
Group ``feed.routes`` by the ``by`` column, and for each group
|
|
181
|
+
|
|
182
|
+
1. Choose the first route in the group
|
|
183
|
+
2. Assign a new route ID based on the given ``route_id_prefix``
|
|
184
|
+
string and a running count, e.g. ``'route_013'``
|
|
185
|
+
3. Assign all the trips associated with routes in the group to
|
|
186
|
+
that first route
|
|
187
|
+
4. Update the route IDs in the other "Feed" tables
|
|
188
|
+
|
|
189
|
+
"""
|
|
190
|
+
if by not in feed.routes.columns:
|
|
191
|
+
raise ValueError(f"Column {by} not in feed.routes")
|
|
192
|
+
|
|
193
|
+
feed = feed.copy()
|
|
194
|
+
|
|
195
|
+
# Create new route IDs
|
|
196
|
+
routes = feed.routes
|
|
197
|
+
n = routes.groupby(by).ngroups
|
|
198
|
+
k = int(math.log10(n)) + 1 # Number of digits for padding IDs
|
|
199
|
+
nrid_by_orid = dict()
|
|
200
|
+
i = 1
|
|
201
|
+
for col, group in routes.groupby(by):
|
|
202
|
+
nrid = f"route_{i:0{k}d}"
|
|
203
|
+
d = {orid: nrid for orid in group["route_id"].values}
|
|
204
|
+
nrid_by_orid.update(d)
|
|
205
|
+
i += 1
|
|
206
|
+
|
|
207
|
+
routes["route_id"] = routes["route_id"].map(lambda x: nrid_by_orid[x])
|
|
208
|
+
routes = routes.groupby(by).first().reset_index()
|
|
209
|
+
feed.routes = routes
|
|
210
|
+
|
|
211
|
+
# Update route IDs of trips
|
|
212
|
+
trips = feed.trips
|
|
213
|
+
trips["route_id"] = trips["route_id"].map(lambda x: nrid_by_orid[x])
|
|
214
|
+
feed.trips = trips
|
|
215
|
+
|
|
216
|
+
# Update route IDs of transfers
|
|
217
|
+
if feed.transfers is not None:
|
|
218
|
+
transfers = feed.transfers
|
|
219
|
+
transfers["route_id"] = transfers["route_id"].map(
|
|
220
|
+
lambda x: nrid_by_orid[x]
|
|
221
|
+
)
|
|
222
|
+
feed.transfers = transfers
|
|
223
|
+
|
|
224
|
+
return feed
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def clean(feed: "Feed") -> "Feed":
|
|
228
|
+
"""
|
|
229
|
+
Apply the following functions to the given Feed in order and return the resulting
|
|
230
|
+
Feed.
|
|
231
|
+
|
|
232
|
+
1. :func:`clean_ids`
|
|
233
|
+
2. :func:`clean_times`
|
|
234
|
+
3. :func:`clean_route_short_names`
|
|
235
|
+
4. :func:`drop_zombies`
|
|
236
|
+
|
|
237
|
+
"""
|
|
238
|
+
feed = feed.copy()
|
|
239
|
+
ops = [
|
|
240
|
+
"clean_ids",
|
|
241
|
+
"clean_times",
|
|
242
|
+
"clean_route_short_names",
|
|
243
|
+
"drop_zombies",
|
|
244
|
+
]
|
|
245
|
+
for op in ops:
|
|
246
|
+
feed = globals()[op](feed)
|
|
247
|
+
|
|
248
|
+
return feed
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def drop_invalid_columns(feed: "Feed") -> "Feed":
|
|
252
|
+
"""
|
|
253
|
+
Drop all DataFrame columns of the given Feed that are not
|
|
254
|
+
listed in the GTFS.
|
|
255
|
+
Return the resulting Feed.
|
|
256
|
+
"""
|
|
257
|
+
feed = feed.copy()
|
|
258
|
+
for table, group in cs.GTFS_REF.groupby("table"):
|
|
259
|
+
f = getattr(feed, table)
|
|
260
|
+
if f is None:
|
|
261
|
+
continue
|
|
262
|
+
valid_columns = group["column"].values
|
|
263
|
+
for col in f.columns:
|
|
264
|
+
if col not in valid_columns:
|
|
265
|
+
print(f"{table}: dropping invalid column {col}")
|
|
266
|
+
del f[col]
|
|
267
|
+
setattr(feed, table, f)
|
|
268
|
+
|
|
269
|
+
return feed
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Constants useful across modules.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import pandas as pd
|
|
6
|
+
|
|
7
|
+
# Record some data from the GTFS reference at https://gtfs.org/reference/static.
|
|
8
|
+
columns = ["table", "table_required", "column", "column_required", "dtype"]
|
|
9
|
+
rows = [
|
|
10
|
+
["agency", True, "agency_id", False, "str"],
|
|
11
|
+
["agency", True, "agency_name", True, "str"],
|
|
12
|
+
["agency", True, "agency_url", True, "str"],
|
|
13
|
+
["agency", True, "agency_timezone", True, "str"],
|
|
14
|
+
["agency", True, "agency_lang", False, "str"],
|
|
15
|
+
["agency", True, "agency_phone", False, "str"],
|
|
16
|
+
["agency", True, "agency_fare_url", False, "str"],
|
|
17
|
+
["agency", True, "agency_email", False, "str"],
|
|
18
|
+
["calendar", False, "service_id", True, "str"],
|
|
19
|
+
["calendar", False, "monday", True, "int"],
|
|
20
|
+
["calendar", False, "tuesday", True, "int"],
|
|
21
|
+
["calendar", False, "wednesday", True, "int"],
|
|
22
|
+
["calendar", False, "thursday", True, "int"],
|
|
23
|
+
["calendar", False, "friday", True, "int"],
|
|
24
|
+
["calendar", False, "saturday", True, "int"],
|
|
25
|
+
["calendar", False, "sunday", True, "int"],
|
|
26
|
+
["calendar", False, "start_date", True, "str"],
|
|
27
|
+
["calendar", False, "end_date", True, "str"],
|
|
28
|
+
["calendar_dates", False, "service_id", True, "str"],
|
|
29
|
+
["calendar_dates", False, "date", True, "str"],
|
|
30
|
+
["calendar_dates", False, "exception_type", True, "int"],
|
|
31
|
+
["fare_attributes", False, "fare_id", True, "str"],
|
|
32
|
+
["fare_attributes", False, "price", True, "float"],
|
|
33
|
+
["fare_attributes", False, "currency_type", True, "str"],
|
|
34
|
+
["fare_attributes", False, "payment_method", True, "int"],
|
|
35
|
+
["fare_attributes", False, "transfers", True, "int"],
|
|
36
|
+
["fare_attributes", False, "transfer_duration", False, "int"],
|
|
37
|
+
["fare_rules", False, "fare_id", True, "str"],
|
|
38
|
+
["fare_rules", False, "route_id", False, "str"],
|
|
39
|
+
["fare_rules", False, "origin_id", False, "str"],
|
|
40
|
+
["fare_rules", False, "destination_id", False, "str"],
|
|
41
|
+
["fare_rules", False, "contains_id", False, "str"],
|
|
42
|
+
["feed_info", False, "feed_publisher_name", True, "str"],
|
|
43
|
+
["feed_info", False, "feed_publisher_url", True, "str"],
|
|
44
|
+
["feed_info", False, "feed_lang", True, "str"],
|
|
45
|
+
["feed_info", False, "feed_start_date", False, "str"],
|
|
46
|
+
["feed_info", False, "feed_end_date", False, "str"],
|
|
47
|
+
["feed_info", False, "feed_version", False, "str"],
|
|
48
|
+
["frequencies", False, "trip_id", True, "str"],
|
|
49
|
+
["frequencies", False, "start_time", True, "str"],
|
|
50
|
+
["frequencies", False, "end_time", True, "str"],
|
|
51
|
+
["frequencies", False, "headway_secs", True, "int"],
|
|
52
|
+
["frequencies", False, "exact_times", False, "int"],
|
|
53
|
+
["routes", True, "route_id", True, "str"],
|
|
54
|
+
["routes", True, "agency_id", False, "str"],
|
|
55
|
+
["routes", True, "route_short_name", True, "str"],
|
|
56
|
+
["routes", True, "route_long_name", True, "str"],
|
|
57
|
+
["routes", True, "route_desc", False, "str"],
|
|
58
|
+
["routes", True, "route_type", True, "int"],
|
|
59
|
+
["routes", True, "route_url", False, "str"],
|
|
60
|
+
["routes", True, "route_color", False, "str"],
|
|
61
|
+
["routes", True, "route_text_color", False, "str"],
|
|
62
|
+
["shapes", False, "shape_id", True, "str"],
|
|
63
|
+
["shapes", False, "shape_pt_lat", True, "float"],
|
|
64
|
+
["shapes", False, "shape_pt_lon", True, "float"],
|
|
65
|
+
["shapes", False, "shape_pt_sequence", True, "int"],
|
|
66
|
+
["shapes", False, "shape_dist_traveled", False, "float"],
|
|
67
|
+
["stops", True, "stop_id", True, "str"],
|
|
68
|
+
["stops", True, "stop_code", False, "str"],
|
|
69
|
+
["stops", True, "stop_name", True, "str"],
|
|
70
|
+
["stops", True, "stop_desc", False, "str"],
|
|
71
|
+
["stops", True, "stop_lat", True, "float"],
|
|
72
|
+
["stops", True, "stop_lon", True, "float"],
|
|
73
|
+
["stops", True, "zone_id", False, "str"],
|
|
74
|
+
["stops", True, "stop_url", False, "str"],
|
|
75
|
+
["stops", True, "location_type", False, "int"],
|
|
76
|
+
["stops", True, "parent_station", False, "str"],
|
|
77
|
+
["stops", True, "stop_timezone", False, "str"],
|
|
78
|
+
["stops", True, "wheelchair_boarding", False, "int"],
|
|
79
|
+
["stop_times", True, "trip_id", True, "str"],
|
|
80
|
+
["stop_times", True, "arrival_time", True, "str"],
|
|
81
|
+
["stop_times", True, "departure_time", True, "str"],
|
|
82
|
+
["stop_times", True, "stop_id", True, "str"],
|
|
83
|
+
["stop_times", True, "stop_sequence", True, "int"],
|
|
84
|
+
["stop_times", True, "stop_headsign", False, "str"],
|
|
85
|
+
["stop_times", True, "pickup_type", False, "int"],
|
|
86
|
+
["stop_times", True, "drop_off_type", False, "int"],
|
|
87
|
+
["stop_times", True, "shape_dist_traveled", False, "float"],
|
|
88
|
+
["stop_times", True, "timepoint", False, "int"],
|
|
89
|
+
["transfers", False, "from_stop_id", True, "str"],
|
|
90
|
+
["transfers", False, "to_stop_id", True, "str"],
|
|
91
|
+
["transfers", False, "transfer_type", True, "int"],
|
|
92
|
+
["transfers", False, "min_transfer_time", False, "int"],
|
|
93
|
+
["trips", True, "route_id", True, "str"],
|
|
94
|
+
["trips", True, "service_id", True, "str"],
|
|
95
|
+
["trips", True, "trip_id", True, "str"],
|
|
96
|
+
["trips", True, "trip_headsign", False, "str"],
|
|
97
|
+
["trips", True, "trip_short_name", False, "str"],
|
|
98
|
+
["trips", True, "direction_id", False, "int"],
|
|
99
|
+
["trips", True, "block_id", False, "str"],
|
|
100
|
+
["trips", True, "shape_id", False, "str"],
|
|
101
|
+
["trips", True, "wheelchair_accessible", False, "int"],
|
|
102
|
+
["trips", True, "bikes_allowed", False, "int"],
|
|
103
|
+
]
|
|
104
|
+
GTFS_REF = pd.DataFrame(rows, columns=columns)
|
|
105
|
+
|
|
106
|
+
#: Columns that must be formatted as integers when outputting GTFS
|
|
107
|
+
INT_COLS = GTFS_REF.loc[GTFS_REF["dtype"] == "int", "column"].values.tolist()
|
|
108
|
+
|
|
109
|
+
#: Columns that must be read as strings by Pandas
|
|
110
|
+
STR_COLS = GTFS_REF.loc[GTFS_REF["dtype"] == "str", "column"].values.tolist()
|
|
111
|
+
|
|
112
|
+
DTYPE = {col: str for col in STR_COLS}
|
|
113
|
+
|
|
114
|
+
#: Valid distance units
|
|
115
|
+
DIST_UNITS = ["ft", "mi", "m", "km"]
|
|
116
|
+
|
|
117
|
+
#: Primary feed attributes
|
|
118
|
+
FEED_ATTRS_1 = [
|
|
119
|
+
"agency",
|
|
120
|
+
"calendar",
|
|
121
|
+
"calendar_dates",
|
|
122
|
+
"fare_attributes",
|
|
123
|
+
"fare_rules",
|
|
124
|
+
"feed_info",
|
|
125
|
+
"frequencies",
|
|
126
|
+
"routes",
|
|
127
|
+
"shapes",
|
|
128
|
+
"stops",
|
|
129
|
+
"stop_times",
|
|
130
|
+
"trips",
|
|
131
|
+
"transfers",
|
|
132
|
+
"dist_units",
|
|
133
|
+
]
|
|
134
|
+
|
|
135
|
+
#: Secondary feed attributes; derived from primary ones
|
|
136
|
+
FEED_ATTRS_2 = ["_trips_i", "_calendar_i", "_calendar_dates_g"]
|
|
137
|
+
|
|
138
|
+
#:
|
|
139
|
+
FEED_ATTRS = FEED_ATTRS_1 + FEED_ATTRS_2
|
|
140
|
+
|
|
141
|
+
#: WGS84 coordinate reference system for Geopandas
|
|
142
|
+
WGS84 = {"init": "epsg:4326"}
|
|
143
|
+
|
|
144
|
+
#: Colorbrewer 8-class Set2 colors
|
|
145
|
+
COLORS_SET2 = [
|
|
146
|
+
"#66c2a5",
|
|
147
|
+
"#fc8d62",
|
|
148
|
+
"#8da0cb",
|
|
149
|
+
"#e78ac3",
|
|
150
|
+
"#a6d854",
|
|
151
|
+
"#ffd92f",
|
|
152
|
+
"#e5c494",
|
|
153
|
+
"#b3b3b3",
|
|
154
|
+
]
|