Skip to content

transitlos.map.routes

Build one map-ready line feature per GTFS route_id, for the map's transit-lines overlay.

Deliberately a small, dependency-light GTFS reader (plain pandas.read_csv over routes.txt/trips.txt/shapes.txt/stop_times.txt/stops.txt) rather than a pyGTFSHandler.feed.Feed load: this needs geometry only, no service-calendar filtering, no headway/speed analysis, and no stop grouping -- all of which is what makes a full Feed load expensive (tens of seconds to minutes per city). Nothing here is city-specific; a caller passes GTFS directories in and gets a GeoDataFrame back.

Geometry source, in priority order (per the "shapes if available, else straight lines between consecutive stops" requirement):

  1. shapes.txt polylines referenced by that route's trips (the real, on-street alignment) -- EXCEPT a shape whose own points turn out to just be that trip's stops connected (see _stop_derived_shape_ids), which is treated the same as having no shape at all, not a real alignment.
  2. If a route has no usable shape, the stop sequence of its longest trip (stop_times.txt ordered by stop_sequence, joined to stops.txt coordinates) connected by straight segments.

One feature per route_id (the explicit deliverable), not one per trip or shape: a route's many trip shapes are collapsed by a greedy "does this candidate add geometry the already-kept ones don't cover?" pass, so a route with genuinely separate branches (e.g. a light-rail trunk splitting into several tails) keeps them as a MultiLineString while the dozens of near-identical per-trip variants of an ordinary bus route collapse to one LineString.

build_route_lines(gtfs_dirs, aoi=None, exclude_no_shape_routes=True)

Build one line feature per GTFS route_id across one or more feeds.

Parameters:

Name Type Description Default
gtfs_dirs Union[str, Path, Iterable[Union[str, Path]]]

A GTFS directory, or an iterable of them (one per feed). Zipped feeds are not handled here -- pass extracted directories.

required
aoi Optional[GeoDataFrame]

Optional area of interest (any CRS); routes whose geometry does not intersect its union are dropped, so a metro-scale map isn't carrying intercity coach routes running hundreds of km away.

None
exclude_no_shape_routes bool

2026-09-01, user-requested default: a route with no real shapes.txt geometry for ANY of its trips (either the whole feed lacks shapes.txt, or every trip on that route has a null/missing shape_id) falls back to a straight-line connect-the-stops geometry (see _stop_sequence_lines) -- a straight line is not a real route alignment and can look actively wrong on a map (cutting through blocks/water/etc.). When True (the default -- "I want all the maps generated that way"), such routes are dropped from the returned GeoDataFrame entirely rather than drawn with fabricated geometry. Every row (including ones that WOULD be dropped) still carries a real has_real_shape boolean column when False, so a caller that wants the full route list regardless (e.g. for a downloadable routes list, as opposed to the map's own line layer) can pass False and filter on that column itself.

True

Returns:

Type Description
GeoDataFrame

A WGS84 GeoDataFrame with one row per (feed, route_id):

GeoDataFrame

route_uid (feed-qualified, unique across feeds), route_id,

GeoDataFrame

feed, route_label (route_short_name -> route_long_name ->

GeoDataFrame

route_id), route_short_name, route_long_name, mode

GeoDataFrame

(transitlos.scoring.mrc.mode_category of route_type), color

GeoDataFrame

(GTFS route_color or MODE_FALLBACK_COLOR[mode]), text_color

GeoDataFrame

(GTFS route_text_color or white), has_real_shape (True if any

GeoDataFrame

of the route's trips had real shapes.txt geometry, False if

GeoDataFrame

every trip fell back to the straight-line stop-sequence

GeoDataFrame

connect-the-dots geometry), plus LineString/MultiLineString

GeoDataFrame

geometry.