Reputation: 4948
I am trying to get the paths part of a transit service from a set of GTFS files. The trips.txt file actually presents the paths multiplied for the timetables so that many lines for each path as documented in the GTFS documentation. How may I get unique trips for path and especially unique links to stops?
Upvotes: 0
Views: 1651
Reputation: 4948
Given the unavailability of shapes, I ended up grouping the trips on the bus and destination by removing any second trip with the same bus and destination. When I load the stop_times I check if the line exists before inserting it. And I hope something remains in the net...
Upvotes: 0
Reputation:
By "path" I assume you're referring to what in GTFS parlance is a shape: The physical path followed by a transit vehicle in the real world.
For a list of unique trips for a shape (assuming you have your GTFS data loaded in a relational database), you need only a query like this:
SELECT * FROM trips WHERE shape_id = <shape_id>;
Note you may want to additionally filter by service_id
to produce a list of only trips on weekdays, for instance.
Generating a list of stops for a shape is harder, since there is no direct relationship between a shape and a stop in GTFS. Rather, stops are associated with individual trips, with the stop_times
table providing the association (see my answer to "How can I list all the stops associated with a route using GTFS?"). Once you've selected a trip of interest you can get a list of the stops it visits, in order, with a query like this:
SELECT DISTINCT stop_id, code, name
FROM stop_times
INNER JOIN stops ON stops.id = stop_times.stop_id
WHERE trip_id = <trip_id>
ORDER BY stop_sequence;
But I suspect none of this is what you want. I suspect you're thinking (based on your experience as a transit rider) there ought to be just a handful of distinct "paths" each transit route follows, and you should be able to get at this small number of paths and, say, automatically generate a map for each one with the stops clearly indicated. Unfortunately this does not line up well with either the way transit systems work in practice or the way they are modelled by GTFS.
To do what I believe it is you want to do, you will have to
Fetch a list of trips for the route in question (from trips.txt
).
From this list, apply heuristics to determine which trips are equivalent in a transit user's mind—trips that follow the same "path" (shape), travel in the same direction, visit the same or a similar set of stops in the same order and operate on the same days (from calendar.txt
), for instance—and group equivalent trips together into buckets.
For each bucket of equivalent trips, generate a list of stops together with maps, timetables or descriptions as desired.
Note that in general you can use a trip's headsign to determine which branch of a route it follows and therefore which path it will take and the stops it will visit. I wouldn't assume this is completely reliable, though; you'll have to check your data to see if you can reliably group trips together this way.
Upvotes: 2