gtfs_kit 5.0.2__tar.gz → 5.1.2__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-5.0.2 → gtfs_kit-5.1.2}/PKG-INFO +6 -6
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/README.rst +1 -1
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/__init__.py +1 -1
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/calendar.py +7 -8
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/cleaners.py +13 -12
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/feed.py +4 -4
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/helpers.py +17 -7
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/miscellany.py +28 -20
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/routes.py +25 -25
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/shapes.py +11 -4
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/stop_times.py +40 -3
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/stops.py +26 -25
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/trips.py +76 -34
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/validators.py +47 -92
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/pyproject.toml +7 -6
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/setup.py +6 -6
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/LICENSE.txt +0 -0
- {gtfs_kit-5.0.2 → gtfs_kit-5.1.2}/gtfs_kit/constants.py +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: gtfs-kit
|
|
3
|
-
Version: 5.
|
|
4
|
-
Summary: A Python 3.
|
|
3
|
+
Version: 5.1.2
|
|
4
|
+
Summary: A Python 3.7+ library for analyzing GTFS feeds.
|
|
5
5
|
Home-page: https://github.com/mrcagney/gtfs_kit
|
|
6
6
|
License: MIT
|
|
7
7
|
Author: Alex Raichev
|
|
8
8
|
Author-email: araichev@mrcagney.com
|
|
9
|
-
Requires-Python: >=3.
|
|
9
|
+
Requires-Python: >=3.7,<4.0
|
|
10
10
|
Classifier: License :: OSI Approved :: MIT License
|
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.7
|
|
@@ -17,8 +17,8 @@ Requires-Dist: geopandas (<1)
|
|
|
17
17
|
Requires-Dist: json2html (<2)
|
|
18
18
|
Requires-Dist: pandas (<2)
|
|
19
19
|
Requires-Dist: pycountry (<20)
|
|
20
|
-
Requires-Dist: requests (
|
|
21
|
-
Requires-Dist: rtree (
|
|
20
|
+
Requires-Dist: requests (<3)
|
|
21
|
+
Requires-Dist: rtree (<1)
|
|
22
22
|
Requires-Dist: shapely (<2)
|
|
23
23
|
Requires-Dist: utm (<1)
|
|
24
24
|
Project-URL: Documentation, https://mrcagney.github.io/gtfs_kit_docs
|
|
@@ -30,7 +30,7 @@ GTFS Kit
|
|
|
30
30
|
.. image:: https://travis-ci.org/mrcagney/gtfs_kit.svg?branch=master
|
|
31
31
|
:target: https://travis-ci.org/mrcagney/gtfs_kit
|
|
32
32
|
|
|
33
|
-
GTFS Kit is a Python 3.
|
|
33
|
+
GTFS Kit is a Python 3.7+ kit for analyzing `General Transit Feed Specification (GTFS) <https://en.wikipedia.org/wiki/GTFS>`_ data in memory without a database.
|
|
34
34
|
It uses Pandas and Shapely to do the heavy lifting.
|
|
35
35
|
|
|
36
36
|
This project supersedes `GTFSTK <https://github.com/mrcagney/gtfstk>`_.
|
|
@@ -3,7 +3,7 @@ GTFS Kit
|
|
|
3
3
|
.. image:: https://travis-ci.org/mrcagney/gtfs_kit.svg?branch=master
|
|
4
4
|
:target: https://travis-ci.org/mrcagney/gtfs_kit
|
|
5
5
|
|
|
6
|
-
GTFS Kit is a Python 3.
|
|
6
|
+
GTFS Kit is a Python 3.7+ kit for analyzing `General Transit Feed Specification (GTFS) <https://en.wikipedia.org/wiki/GTFS>`_ data in memory without a database.
|
|
7
7
|
It uses Pandas and Shapely to do the heavy lifting.
|
|
8
8
|
|
|
9
9
|
This project supersedes `GTFSTK <https://github.com/mrcagney/gtfstk>`_.
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Functions about calendar and calendar_dates.
|
|
3
3
|
"""
|
|
4
|
+
from __future__ import annotations
|
|
4
5
|
import dateutil.relativedelta as rd
|
|
5
|
-
from typing import
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
6
7
|
|
|
7
8
|
from . import helpers as hp
|
|
8
9
|
|
|
@@ -11,7 +12,7 @@ if TYPE_CHECKING:
|
|
|
11
12
|
from .feed import Feed
|
|
12
13
|
|
|
13
14
|
|
|
14
|
-
def get_dates(feed: "Feed", *, as_date_obj: bool = False) ->
|
|
15
|
+
def get_dates(feed: "Feed", *, as_date_obj: bool = False) -> list[str]:
|
|
15
16
|
"""
|
|
16
17
|
Return a list of YYYYMMDD date strings for which the given Feed is valid,
|
|
17
18
|
which could be the empty list if the Feed has no calendar information.
|
|
@@ -35,9 +36,7 @@ def get_dates(feed: "Feed", *, as_date_obj: bool = False) -> List[str]:
|
|
|
35
36
|
start_date, end_date = min(dates), max(dates)
|
|
36
37
|
start_date, end_date = map(hp.datestr_to_date, [start_date, end_date])
|
|
37
38
|
num_days = (end_date - start_date).days
|
|
38
|
-
result = [
|
|
39
|
-
start_date + rd.relativedelta(days=+d) for d in range(num_days + 1)
|
|
40
|
-
]
|
|
39
|
+
result = [start_date + rd.relativedelta(days=+d) for d in range(num_days + 1)]
|
|
41
40
|
|
|
42
41
|
# Convert dates back to strings if required
|
|
43
42
|
if not as_date_obj:
|
|
@@ -46,7 +45,7 @@ def get_dates(feed: "Feed", *, as_date_obj: bool = False) -> List[str]:
|
|
|
46
45
|
return result
|
|
47
46
|
|
|
48
47
|
|
|
49
|
-
def get_week(feed: "Feed", k: int, *, as_date_obj: bool = False) ->
|
|
48
|
+
def get_week(feed: "Feed", k: int, *, as_date_obj: bool = False) -> list[str]:
|
|
50
49
|
"""
|
|
51
50
|
Given a Feed and a positive integer ``k``,
|
|
52
51
|
return a list of YYYYMMDD date strings corresponding to the kth Monday--Sunday week
|
|
@@ -79,7 +78,7 @@ def get_week(feed: "Feed", k: int, *, as_date_obj: bool = False) -> List[str]:
|
|
|
79
78
|
return result
|
|
80
79
|
|
|
81
80
|
|
|
82
|
-
def get_first_week(feed: "Feed", *, as_date_obj: bool = False) ->
|
|
81
|
+
def get_first_week(feed: "Feed", *, as_date_obj: bool = False) -> list[str]:
|
|
83
82
|
"""
|
|
84
83
|
Return a list of YYYYMMDD date strings for the first Monday--Sunday
|
|
85
84
|
week (or initial segment thereof) for which the given Feed is valid.
|
|
@@ -90,7 +89,7 @@ def get_first_week(feed: "Feed", *, as_date_obj: bool = False) -> List[str]:
|
|
|
90
89
|
return get_week(feed, 1, as_date_obj=as_date_obj)
|
|
91
90
|
|
|
92
91
|
|
|
93
|
-
def subset_dates(feed: "Feed", dates:
|
|
92
|
+
def subset_dates(feed: "Feed", dates: list[str]) -> list[str]:
|
|
94
93
|
"""
|
|
95
94
|
Given a Feed and a list of YYYYMMDD date strings,
|
|
96
95
|
return the sublist of dates that lie in the Feed's dates
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Functions about cleaning feeds.
|
|
3
3
|
"""
|
|
4
|
-
from
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
5
6
|
|
|
6
7
|
import pandas as pd
|
|
7
8
|
import numpy as np
|
|
@@ -169,7 +170,7 @@ def clean_route_short_names(feed: "Feed") -> "Feed":
|
|
|
169
170
|
|
|
170
171
|
def build_aggregate_routes_dict(
|
|
171
172
|
routes: pd.DataFrame, by: str = "route_short_name", route_id_prefix: str = "route_"
|
|
172
|
-
) ->
|
|
173
|
+
) -> dict[str, str]:
|
|
173
174
|
"""
|
|
174
175
|
Given a DataFrame of routes, group the routes by route short name, say,
|
|
175
176
|
and assign new route IDs using the given prefix.
|
|
@@ -215,28 +216,28 @@ def aggregate_routes(
|
|
|
215
216
|
routes = feed.routes
|
|
216
217
|
nid_by_oid = build_aggregate_routes_dict(routes, by, route_id_prefix)
|
|
217
218
|
|
|
218
|
-
# Update
|
|
219
|
-
routes["route_id"] = routes
|
|
219
|
+
# Update route IDs in routes
|
|
220
|
+
routes["route_id"] = routes.route_id.map(lambda x: nid_by_oid[x])
|
|
220
221
|
routes = routes.groupby(by).first().reset_index()
|
|
221
222
|
feed.routes = routes
|
|
222
223
|
|
|
223
|
-
# Update route IDs
|
|
224
|
+
# Update route IDs in trips
|
|
224
225
|
trips = feed.trips
|
|
225
|
-
trips["route_id"] = trips
|
|
226
|
+
trips["route_id"] = trips.route_id.map(lambda x: nid_by_oid[x])
|
|
226
227
|
feed.trips = trips
|
|
227
228
|
|
|
228
|
-
# Update route IDs of
|
|
229
|
-
if feed.
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
feed.
|
|
229
|
+
# Update route IDs of fare rules
|
|
230
|
+
if feed.fare_rules is not None and "route_id" in feed.fare_rules.columns:
|
|
231
|
+
fr = feed.fare_rules
|
|
232
|
+
fr["route_id"] = fr.route_id.map(lambda x: nid_by_oid[x])
|
|
233
|
+
feed.fare_rules = fr
|
|
233
234
|
|
|
234
235
|
return feed
|
|
235
236
|
|
|
236
237
|
|
|
237
238
|
def build_aggregate_stops_dict(
|
|
238
239
|
stops: pd.DataFrame, by: str = "stop_code", stop_id_prefix: str = "stop_"
|
|
239
|
-
) ->
|
|
240
|
+
) -> dict[str, str]:
|
|
240
241
|
"""
|
|
241
242
|
Given a DataFrame of stops, group the stops by stop code, say,
|
|
242
243
|
and assign new stop IDs using the given prefix.
|
|
@@ -42,10 +42,9 @@ class Feed(object):
|
|
|
42
42
|
Primary instance attributes:
|
|
43
43
|
|
|
44
44
|
- ``dist_units``: a string in :const:`.constants.DIST_UNITS`;
|
|
45
|
-
specifies the distance units
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if present
|
|
45
|
+
specifies the distance units of the `shape_dist_traveled` column values,
|
|
46
|
+
if present; also effects whether to display trip and route stats in
|
|
47
|
+
metric or imperial units
|
|
49
48
|
- ``agency``
|
|
50
49
|
- ``stops``
|
|
51
50
|
- ``routes``
|
|
@@ -116,6 +115,7 @@ class Feed(object):
|
|
|
116
115
|
get_stop_times,
|
|
117
116
|
append_dist_to_stop_times,
|
|
118
117
|
get_start_and_end_times,
|
|
118
|
+
stop_times_to_geojson,
|
|
119
119
|
)
|
|
120
120
|
from .trips import (
|
|
121
121
|
is_active_trip,
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Functions useful across modules.
|
|
3
3
|
"""
|
|
4
|
+
from __future__ import annotations
|
|
4
5
|
import datetime as dt
|
|
5
|
-
from typing import Optional,
|
|
6
|
+
from typing import Optional, Union, Callable
|
|
6
7
|
import copy
|
|
7
8
|
from bisect import bisect_left, bisect_right
|
|
8
9
|
from functools import cmp_to_key
|
|
@@ -158,7 +159,7 @@ def get_max_runs(x) -> np.array:
|
|
|
158
159
|
# return run_starts[idx], run_ends[idx]
|
|
159
160
|
|
|
160
161
|
|
|
161
|
-
def get_peak_indices(times:
|
|
162
|
+
def get_peak_indices(times: list, counts: list) -> np.array:
|
|
162
163
|
"""
|
|
163
164
|
Given an increasing list of times as seconds past midnight and a
|
|
164
165
|
list of trip counts at those respective times,
|
|
@@ -176,6 +177,14 @@ def get_peak_indices(times: List, counts: List) -> np.array:
|
|
|
176
177
|
return max_runs[index]
|
|
177
178
|
|
|
178
179
|
|
|
180
|
+
def is_metric(dist_units: str) -> bool:
|
|
181
|
+
"""
|
|
182
|
+
Return True if the given distance units equals 'm' or 'km';
|
|
183
|
+
otherwise return False.
|
|
184
|
+
"""
|
|
185
|
+
return dist_units in ["m", "km"]
|
|
186
|
+
|
|
187
|
+
|
|
179
188
|
def get_convert_dist(
|
|
180
189
|
dist_units_in: str, dist_units_out: str
|
|
181
190
|
) -> Callable[[float], float]:
|
|
@@ -232,7 +241,7 @@ def is_not_null(df: pd.DataFrame, col_name: str) -> bool:
|
|
|
232
241
|
return False
|
|
233
242
|
|
|
234
243
|
|
|
235
|
-
def get_utm_crs(lat: float, lon: float) ->
|
|
244
|
+
def get_utm_crs(lat: float, lon: float) -> dict:
|
|
236
245
|
"""
|
|
237
246
|
Return a GeoPandas coordinate reference system (CRS) string
|
|
238
247
|
corresponding to the UTM projection appropriate to the given WGS84
|
|
@@ -241,9 +250,10 @@ def get_utm_crs(lat: float, lon: float) -> Dict:
|
|
|
241
250
|
Code inspired by https://github.com/Turbo87/utm/issues/51.
|
|
242
251
|
"""
|
|
243
252
|
zone = utm.from_latlon(lat, lon)[2]
|
|
244
|
-
result = f"EPSG:326{zone:02d}" if lat >= 0 else f"EPSG:327{zone:02d}"
|
|
253
|
+
result = f"EPSG:326{zone:02d}" if lat >= 0 else f"EPSG:327{zone:02d}"
|
|
245
254
|
return result
|
|
246
255
|
|
|
256
|
+
|
|
247
257
|
def linestring_to_utm(linestring: sg.LineString) -> sg.LineString:
|
|
248
258
|
"""
|
|
249
259
|
Given a Shapely LineString in WGS84 coordinates,
|
|
@@ -286,7 +296,7 @@ def get_active_trips_df(trip_times: pd.DataFrame) -> pd.Series:
|
|
|
286
296
|
|
|
287
297
|
|
|
288
298
|
def combine_time_series(
|
|
289
|
-
time_series_dict:
|
|
299
|
+
time_series_dict: dict[str, pd.DataFrame],
|
|
290
300
|
kind: str,
|
|
291
301
|
*,
|
|
292
302
|
split_directions: bool = False,
|
|
@@ -483,7 +493,7 @@ def restack_time_series(unstacked_time_series: pd.DataFrame) -> pd.DataFrame:
|
|
|
483
493
|
return g
|
|
484
494
|
|
|
485
495
|
|
|
486
|
-
def make_html(d:
|
|
496
|
+
def make_html(d: dict) -> str:
|
|
487
497
|
"""
|
|
488
498
|
Convert the given dictionary into an HTML table (string) with
|
|
489
499
|
two columns: keys of dictionary, values of dictionary.
|
|
@@ -493,7 +503,7 @@ def make_html(d: Dict) -> str:
|
|
|
493
503
|
)
|
|
494
504
|
|
|
495
505
|
|
|
496
|
-
def drop_feature_ids(collection:
|
|
506
|
+
def drop_feature_ids(collection: dict) -> dict:
|
|
497
507
|
"""
|
|
498
508
|
Given a GeoJSON FeatureCollection, remove the ``'id'`` attribute of each
|
|
499
509
|
Feature, if it exists.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Functions about miscellany.
|
|
3
3
|
"""
|
|
4
|
-
from
|
|
5
|
-
from typing import
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
from typing import Optional, TYPE_CHECKING
|
|
6
6
|
|
|
7
7
|
import pandas as pd
|
|
8
8
|
import numpy as np
|
|
@@ -103,7 +103,7 @@ def describe(feed: "Feed", sample_date: Optional[str] = None) -> pd.DataFrame:
|
|
|
103
103
|
"""
|
|
104
104
|
from . import calendar as cl
|
|
105
105
|
|
|
106
|
-
d =
|
|
106
|
+
d = dict()
|
|
107
107
|
dates = cl.get_dates(feed)
|
|
108
108
|
d["agencies"] = feed.agency["agency_name"].tolist()
|
|
109
109
|
d["timezone"] = feed.agency["agency_timezone"].iat[0]
|
|
@@ -142,7 +142,7 @@ def assess_quality(feed: "Feed") -> pd.DataFrame:
|
|
|
142
142
|
This function is odd but useful for seeing roughly how broken a feed is
|
|
143
143
|
This function is not a GTFS validator.
|
|
144
144
|
"""
|
|
145
|
-
d =
|
|
145
|
+
d = dict()
|
|
146
146
|
|
|
147
147
|
# Count duplicate route short names
|
|
148
148
|
r = feed.routes
|
|
@@ -329,7 +329,7 @@ def compute_feed_stats_0(
|
|
|
329
329
|
def compute_feed_stats(
|
|
330
330
|
feed: "Feed",
|
|
331
331
|
trip_stats: pd.DataFrame,
|
|
332
|
-
dates:
|
|
332
|
+
dates: list[str],
|
|
333
333
|
*,
|
|
334
334
|
split_route_types=False,
|
|
335
335
|
) -> pd.DataFrame:
|
|
@@ -356,9 +356,12 @@ def compute_feed_stats(
|
|
|
356
356
|
- ``'peak_end_time'``: end time of first longest period during
|
|
357
357
|
which the peak number of trips occurs on the date
|
|
358
358
|
- ``'service_distance'``: sum of the service distances for the
|
|
359
|
-
active routes on the date
|
|
359
|
+
active routes on the date;
|
|
360
|
+
measured in kilometers if ``feed.dist_units`` is metric;
|
|
361
|
+
otherwise measured in miles;
|
|
362
|
+
contains all ``np.nan`` entries if ``feed.shapes is None``
|
|
360
363
|
- ``'service_duration'``: sum of the service durations for the
|
|
361
|
-
active routes on the date
|
|
364
|
+
active routes on the date; measured in hours
|
|
362
365
|
- ``'service_speed'``: service_distance/service_duration on the
|
|
363
366
|
date
|
|
364
367
|
|
|
@@ -413,7 +416,7 @@ def compute_feed_stats(
|
|
|
413
416
|
def compute_feed_time_series(
|
|
414
417
|
feed: "Feed",
|
|
415
418
|
trip_stats: pd.DataFrame,
|
|
416
|
-
dates:
|
|
419
|
+
dates: list[str],
|
|
417
420
|
freq: str = "5Min",
|
|
418
421
|
*,
|
|
419
422
|
split_route_types: bool = False,
|
|
@@ -438,9 +441,13 @@ def compute_feed_time_series(
|
|
|
438
441
|
- ``'num_trip_ends'``: number of trips ending during the
|
|
439
442
|
time period, ignoring the trips the end past midnight
|
|
440
443
|
- ``'service_distance'``: distance traveled during the time
|
|
441
|
-
period by all trips active during the time period
|
|
444
|
+
period by all trips active during the time period;
|
|
445
|
+
measured in kilometers if ``feed.dist_units`` is metric;
|
|
446
|
+
otherwise measured in miles;
|
|
447
|
+
contains all ``np.nan`` entries if ``feed.shapes is None``
|
|
442
448
|
- ``'service_duration'``: duration traveled during the time
|
|
443
|
-
period by all trips active during the time period
|
|
449
|
+
period by all trips active during the time period;
|
|
450
|
+
measured in hours
|
|
444
451
|
- ``'service_speed'``: ``service_distance/service_duration``
|
|
445
452
|
|
|
446
453
|
Exclude dates that lie outside of the Feed's date range.
|
|
@@ -573,7 +580,7 @@ def create_shapes(feed: "Feed", *, all_trips: bool = False) -> "Feed":
|
|
|
573
580
|
return feed
|
|
574
581
|
|
|
575
582
|
|
|
576
|
-
def compute_bounds(feed: "Feed", stop_ids: Optional[
|
|
583
|
+
def compute_bounds(feed: "Feed", stop_ids: Optional[list[str]] = None) -> np.array:
|
|
577
584
|
"""
|
|
578
585
|
Return the bounding box (Numpy array [min longitude, min latitude, max longitude,
|
|
579
586
|
max latitude]) of the given Feed's stops or of the subset of stops
|
|
@@ -585,10 +592,10 @@ def compute_bounds(feed: "Feed", stop_ids: Optional[List[str]] = None) -> np.arr
|
|
|
585
592
|
|
|
586
593
|
|
|
587
594
|
def compute_convex_hull(
|
|
588
|
-
feed: "Feed", stop_ids: Optional[
|
|
595
|
+
feed: "Feed", stop_ids: Optional[list[str]] = None
|
|
589
596
|
) -> sg.Polygon:
|
|
590
597
|
"""
|
|
591
|
-
Return a convex hull (Shapely Polygon) representing the convex hull of the given
|
|
598
|
+
Return a convex hull (Shapely Polygon) representing the convex hull of the given
|
|
592
599
|
Feed's stops or of the subset of stops specified by the given stop IDs.
|
|
593
600
|
"""
|
|
594
601
|
from .stops import geometrize_stops
|
|
@@ -596,7 +603,7 @@ def compute_convex_hull(
|
|
|
596
603
|
return geometrize_stops(feed, stop_ids=stop_ids).unary_union.convex_hull
|
|
597
604
|
|
|
598
605
|
|
|
599
|
-
def compute_centroid(feed: "Feed", stop_ids: Optional[
|
|
606
|
+
def compute_centroid(feed: "Feed", stop_ids: Optional[list[str]] = None) -> sg.Point:
|
|
600
607
|
"""
|
|
601
608
|
Return the centroid (Shapely Point) of the convex hull the given Feed's stops
|
|
602
609
|
or of the subset of stops specified by the given stop IDs.
|
|
@@ -606,7 +613,7 @@ def compute_centroid(feed: "Feed", stop_ids: Optional[List[str]] = None) -> sg.P
|
|
|
606
613
|
return geometrize_stops(feed, stop_ids=stop_ids).unary_union.convex_hull.centroid
|
|
607
614
|
|
|
608
615
|
|
|
609
|
-
def restrict_to_dates(feed: "Feed", dates:
|
|
616
|
+
def restrict_to_dates(feed: "Feed", dates: list[str]) -> "Feed":
|
|
610
617
|
"""
|
|
611
618
|
Build a new feed by restricting this one to only the stops,
|
|
612
619
|
trips, shapes, etc. active on at least one of the given dates
|
|
@@ -624,7 +631,8 @@ def restrict_to_dates(feed: "Feed", dates: List[str]) -> "Feed":
|
|
|
624
631
|
trip_ids = []
|
|
625
632
|
else:
|
|
626
633
|
trip_ids = trip_activity.loc[
|
|
627
|
-
lambda x: x.filter(dates).sum(axis=1) > 0,
|
|
634
|
+
lambda x: x.filter(dates).sum(axis=1) > 0,
|
|
635
|
+
"trip_id",
|
|
628
636
|
]
|
|
629
637
|
|
|
630
638
|
# Slice trips
|
|
@@ -684,7 +692,7 @@ def restrict_to_dates(feed: "Feed", dates: List[str]) -> "Feed":
|
|
|
684
692
|
return feed
|
|
685
693
|
|
|
686
694
|
|
|
687
|
-
def restrict_to_routes(feed: "Feed", route_ids:
|
|
695
|
+
def restrict_to_routes(feed: "Feed", route_ids: list[str]) -> "Feed":
|
|
688
696
|
"""
|
|
689
697
|
Build a new feed by restricting this one to only the stops,
|
|
690
698
|
trips, shapes, etc. used by the routes with the given list of
|
|
@@ -829,7 +837,7 @@ def restrict_to_area(feed: "Feed", area: gp.GeoDataFrame) -> "Feed":
|
|
|
829
837
|
|
|
830
838
|
|
|
831
839
|
def compute_screen_line_counts(
|
|
832
|
-
feed: "Feed", screen_lines: gp.GeoDataFrame, dates:
|
|
840
|
+
feed: "Feed", screen_lines: gp.GeoDataFrame, dates: list[str]
|
|
833
841
|
) -> pd.DataFrame:
|
|
834
842
|
"""
|
|
835
843
|
Find all the Feed trips active on the given YYYYMMDD dates whose shapes
|
|
@@ -846,8 +854,8 @@ def compute_screen_line_counts(
|
|
|
846
854
|
- ``'shape_id'``: shape ID of the trip
|
|
847
855
|
- ``'screen_line_id'``: ID of the screen line as specified in ``screen_lines`` or as
|
|
848
856
|
assigned after the fact.
|
|
849
|
-
- ``'crossing_distance'``: distance
|
|
850
|
-
intersection
|
|
857
|
+
- ``'crossing_distance'``: distance (in the feed's distance units) along the trip
|
|
858
|
+
shape of the screen line intersection
|
|
851
859
|
``'crossing_time'``: time that the trip's vehicle crosses
|
|
852
860
|
the scren line; one trip could cross multiple times
|
|
853
861
|
- ``'crossing_direction'``: 1 or -1; 1 indicates trip travel from the
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Functions about routes.
|
|
3
3
|
"""
|
|
4
|
-
from
|
|
5
|
-
from typing import Optional, Iterable,
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
from typing import Optional, Iterable, TYPE_CHECKING
|
|
6
6
|
import json
|
|
7
7
|
|
|
8
8
|
import geopandas as gp
|
|
@@ -70,11 +70,11 @@ def compute_route_stats_0(
|
|
|
70
70
|
- ``'service_duration'``: total of the duration of each trip on
|
|
71
71
|
the route in the given subset of trips; measured in hours
|
|
72
72
|
- ``'service_distance'``: total of the distance traveled by each
|
|
73
|
-
trip on the route in the given subset of trips;
|
|
74
|
-
|
|
73
|
+
trip on the route in the given subset of trips;
|
|
74
|
+
measured in kilometers if ``feed.dist_units`` is metric;
|
|
75
|
+
otherwise measured in miles;
|
|
75
76
|
contains all ``np.nan`` entries if ``feed.shapes is None``
|
|
76
|
-
- ``'service_speed'``: service_distance/service_duration
|
|
77
|
-
measured in distance units per hour
|
|
77
|
+
- ``'service_speed'``: service_distance/service_duration
|
|
78
78
|
- ``'mean_trip_distance'``: service_distance/num_trips
|
|
79
79
|
- ``'mean_trip_duration'``: service_duration/num_trips
|
|
80
80
|
|
|
@@ -106,7 +106,7 @@ def compute_route_stats_0(
|
|
|
106
106
|
def compute_route_stats_split_directions(group):
|
|
107
107
|
# Take this group of all trips stats for a single route
|
|
108
108
|
# and compute route-level stats.
|
|
109
|
-
d =
|
|
109
|
+
d = dict()
|
|
110
110
|
d["route_short_name"] = group["route_short_name"].iat[0]
|
|
111
111
|
d["route_type"] = group["route_type"].iat[0]
|
|
112
112
|
d["num_trips"] = group.shape[0]
|
|
@@ -147,7 +147,7 @@ def compute_route_stats_0(
|
|
|
147
147
|
return pd.Series(d)
|
|
148
148
|
|
|
149
149
|
def compute_route_stats(group):
|
|
150
|
-
d =
|
|
150
|
+
d = dict()
|
|
151
151
|
d["route_short_name"] = group["route_short_name"].iat[0]
|
|
152
152
|
d["route_type"] = group["route_type"].iat[0]
|
|
153
153
|
d["num_trips"] = group.shape[0]
|
|
@@ -255,12 +255,13 @@ def compute_route_time_series_0(
|
|
|
255
255
|
the time bin
|
|
256
256
|
- ``num_trip_ends``: number of trips that end within the
|
|
257
257
|
time bin, ignoring trips that end past midnight
|
|
258
|
-
- ``service_distance``: sum of the service
|
|
258
|
+
- ``service_distance``: sum of the service distance accrued
|
|
259
|
+
during the time bin across all trips on the route;
|
|
260
|
+
measured in kilometers if ``feed.dist_units`` is metric;
|
|
261
|
+
otherwise measured in miles;
|
|
262
|
+
- ``service_duration``: sum of the service duration accrued
|
|
259
263
|
during the time bin across all trips on the route;
|
|
260
264
|
measured in hours
|
|
261
|
-
- ``service_distance``: sum of the service distance accrued
|
|
262
|
-
during the time bin across all trips on the route; measured
|
|
263
|
-
in kilometers
|
|
264
265
|
- ``service_speed``: ``service_distance/service_duration``
|
|
265
266
|
for the route
|
|
266
267
|
|
|
@@ -429,7 +430,7 @@ def get_routes(
|
|
|
429
430
|
def compute_route_stats(
|
|
430
431
|
feed: "Feed",
|
|
431
432
|
trip_stats_subset: pd.DataFrame,
|
|
432
|
-
dates:
|
|
433
|
+
dates: list[str],
|
|
433
434
|
headway_start_time: str = "07:00:00",
|
|
434
435
|
headway_end_time: str = "19:00:00",
|
|
435
436
|
*,
|
|
@@ -546,7 +547,7 @@ def build_zero_route_time_series(
|
|
|
546
547
|
def compute_route_time_series(
|
|
547
548
|
feed: "Feed",
|
|
548
549
|
trip_stats_subset: pd.DataFrame,
|
|
549
|
-
dates:
|
|
550
|
+
dates: list[str],
|
|
550
551
|
freq: str = "5Min",
|
|
551
552
|
*,
|
|
552
553
|
split_directions: bool = False,
|
|
@@ -636,7 +637,7 @@ def compute_route_time_series(
|
|
|
636
637
|
|
|
637
638
|
|
|
638
639
|
def build_route_timetable(
|
|
639
|
-
feed: "Feed", route_id: str, dates:
|
|
640
|
+
feed: "Feed", route_id: str, dates: list[str]
|
|
640
641
|
) -> pd.DataFrame:
|
|
641
642
|
"""
|
|
642
643
|
Return a timetable for the given route and dates (YYYYMMDD date strings).
|
|
@@ -747,7 +748,7 @@ def routes_to_geojson(
|
|
|
747
748
|
*,
|
|
748
749
|
split_directions: bool = False,
|
|
749
750
|
include_stops: bool = False,
|
|
750
|
-
) ->
|
|
751
|
+
) -> dict:
|
|
751
752
|
"""
|
|
752
753
|
Return a GeoJSON FeatureCollection of MultiLineString features representing this Feed's routes.
|
|
753
754
|
The coordinates reference system is the default one for GeoJSON,
|
|
@@ -760,17 +761,16 @@ def routes_to_geojson(
|
|
|
760
761
|
If the Feed has no shapes, then raise a ValueError.
|
|
761
762
|
If any of the given route IDs are not found in the feed, then raise a ValueError.
|
|
762
763
|
"""
|
|
763
|
-
if route_ids is not
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
764
|
+
if route_ids is None or not list(route_ids):
|
|
765
|
+
route_ids = feed.routes.route_id
|
|
766
|
+
|
|
767
|
+
D = set(route_ids) - set(feed.routes.route_id)
|
|
768
|
+
if D:
|
|
769
|
+
raise ValueError(f"Route IDs {D} not found in feed.")
|
|
767
770
|
|
|
768
771
|
# Get routes
|
|
769
772
|
g = geometrize_routes(feed, route_ids=route_ids, split_directions=split_directions)
|
|
770
|
-
|
|
771
|
-
collection = {"type": "FeatureCollection", "features": []}
|
|
772
|
-
else:
|
|
773
|
-
collection = json.loads(g.to_json())
|
|
773
|
+
collection = json.loads(g.to_json())
|
|
774
774
|
|
|
775
775
|
# Get stops if desired
|
|
776
776
|
if include_stops:
|
|
@@ -792,7 +792,7 @@ def routes_to_geojson(
|
|
|
792
792
|
def map_routes(
|
|
793
793
|
feed: "Feed",
|
|
794
794
|
route_ids: Iterable[str],
|
|
795
|
-
color_palette:
|
|
795
|
+
color_palette: list[str] = cs.COLORS_SET2,
|
|
796
796
|
*,
|
|
797
797
|
include_stops: bool = False,
|
|
798
798
|
):
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Functions about shapes.
|
|
3
3
|
"""
|
|
4
|
-
from
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
from typing import Optional, Iterable, TYPE_CHECKING
|
|
5
6
|
import json
|
|
6
7
|
|
|
7
8
|
import geopandas as gp
|
|
@@ -114,7 +115,13 @@ def ungeometrize_shapes_0(shapes_g: gp.GeoDataFrame) -> pd.DataFrame:
|
|
|
114
115
|
)
|
|
115
116
|
|
|
116
117
|
return pd.DataFrame(
|
|
117
|
-
F,
|
|
118
|
+
F,
|
|
119
|
+
columns=[
|
|
120
|
+
"shape_id",
|
|
121
|
+
"shape_pt_sequence",
|
|
122
|
+
"shape_pt_lon",
|
|
123
|
+
"shape_pt_lat",
|
|
124
|
+
],
|
|
118
125
|
)
|
|
119
126
|
|
|
120
127
|
|
|
@@ -144,7 +151,7 @@ def geometrize_shapes(
|
|
|
144
151
|
|
|
145
152
|
def build_geometry_by_shape(
|
|
146
153
|
feed: "Feed", shape_ids: Optional[Iterable[str]] = None, *, use_utm: bool = False
|
|
147
|
-
) ->
|
|
154
|
+
) -> dict:
|
|
148
155
|
"""
|
|
149
156
|
Return a dictionary of the form <shape ID> -> <Shapely LineString representing shape>.
|
|
150
157
|
"""
|
|
@@ -155,7 +162,7 @@ def build_geometry_by_shape(
|
|
|
155
162
|
)
|
|
156
163
|
|
|
157
164
|
|
|
158
|
-
def shapes_to_geojson(feed: "Feed", shape_ids: Optional[Iterable[str]] = None) ->
|
|
165
|
+
def shapes_to_geojson(feed: "Feed", shape_ids: Optional[Iterable[str]] = None) -> dict:
|
|
159
166
|
"""
|
|
160
167
|
Return a GeoJSON FeatureCollection of LineString features
|
|
161
168
|
representing ``feed.shapes``.
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Functions about stop times.
|
|
3
3
|
"""
|
|
4
|
-
from
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
from typing import Optional, Iterable, TYPE_CHECKING
|
|
6
|
+
import json
|
|
5
7
|
|
|
6
8
|
import pandas as pd
|
|
7
9
|
import numpy as np
|
|
@@ -44,7 +46,7 @@ def append_dist_to_stop_times(feed: "Feed") -> "Feed":
|
|
|
44
46
|
|
|
45
47
|
Set the first distance to 0, the last to the length of the trip shape,
|
|
46
48
|
and leave the remaining ones computed above.
|
|
47
|
-
Choose the longest increasing subsequence of that new set of
|
|
49
|
+
Choose the longest increasing subsequence of that new set of
|
|
48
50
|
distances and use them and their corresponding departure times to linearly
|
|
49
51
|
interpolate the rest of the distances.
|
|
50
52
|
"""
|
|
@@ -127,7 +129,7 @@ def append_dist_to_stop_times(feed: "Feed") -> "Feed":
|
|
|
127
129
|
return new_feed
|
|
128
130
|
|
|
129
131
|
|
|
130
|
-
def get_start_and_end_times(feed: "Feed", date: Optional[str] = None) ->
|
|
132
|
+
def get_start_and_end_times(feed: "Feed", date: Optional[str] = None) -> list[str]:
|
|
131
133
|
"""
|
|
132
134
|
Return the first departure time and last arrival time
|
|
133
135
|
(HH:MM:SS time strings) listed in ``feed.stop_times``, respectively.
|
|
@@ -135,3 +137,38 @@ def get_start_and_end_times(feed: "Feed", date: Optional[str] = None) -> List[st
|
|
|
135
137
|
"""
|
|
136
138
|
st = feed.get_stop_times(date)
|
|
137
139
|
return (st["departure_time"].dropna().min(), st["arrival_time"].dropna().max())
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def stop_times_to_geojson(
|
|
143
|
+
feed: "Feed",
|
|
144
|
+
trip_ids: Optional[Iterable[str]] = None,
|
|
145
|
+
) -> dict:
|
|
146
|
+
"""
|
|
147
|
+
Return a GeoJSON FeatureCollection of Point features
|
|
148
|
+
representing all the trip-stop pairs in ``feed.stop_times``.
|
|
149
|
+
The coordinates reference system is the default one for GeoJSON,
|
|
150
|
+
namely WGS84.
|
|
151
|
+
|
|
152
|
+
For every trip, drop duplicate stop IDs within that trip.
|
|
153
|
+
In particular, a looping trip will lack its final stop.
|
|
154
|
+
|
|
155
|
+
If an iterable of trip IDs is given, then subset to those trips.
|
|
156
|
+
If some of the given trip IDs are not found in the feed, then raise a ValueError.
|
|
157
|
+
"""
|
|
158
|
+
if trip_ids is None or not list(trip_ids):
|
|
159
|
+
trip_ids = feed.trips.trip_id
|
|
160
|
+
|
|
161
|
+
D = set(trip_ids) - set(feed.trips.trip_id)
|
|
162
|
+
if D:
|
|
163
|
+
raise ValueError(f"Trip IDs {D} not found in feed.")
|
|
164
|
+
|
|
165
|
+
st = feed.stop_times.loc[lambda x: x.trip_id.isin(trip_ids)]
|
|
166
|
+
|
|
167
|
+
g = (
|
|
168
|
+
feed.geometrize_stops(stop_ids=st.stop_id.unique())
|
|
169
|
+
.merge(st)
|
|
170
|
+
.sort_values(["trip_id", "stop_sequence"])
|
|
171
|
+
.drop_duplicates(subset=["trip_id", "stop_id"])
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
return hp.drop_feature_ids(json.loads(g.to_json()))
|