0

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?

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75

2 Answers2

2

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.

Community
  • 1
  • 1
  • Thanks, but, first off, unfortunately, the Trafiklab repository for Scandinavia I am working on, does not even have shapes, as you may see at https://www.trafiklab.se/api. In fact I had a feeling this could have been of help. I was also thinking of headsigns but that would also group together lines with the same destination but a different start and/or path. As a matter of fact, I am not so interested in which bus passes in a giving day, as I take this information from the real-time component. It beats me, though, how the standard did not cover such a simple information. – Fabrizio Bartolomucci Aug 18 '15 at 13:37
  • Also I am no sure about what the standard says about the real time information, given so far I am focusing at loading the information, and to which information they are linked. In the case they are linked to the full-blown trips, if I streamlined it, I would find real time buses with no line to connect to. – Fabrizio Bartolomucci Aug 18 '15 at 13:42
  • So what is the end result you're trying to produce? –  Aug 18 '15 at 13:43
  • The support for the in Arrivo HD app for Sweden. In that app, already available in Rome and London, I draw stops on the map dressed with the lines passing by them, upon clicking which the user may see the arriving buses. When a user clicks a line, the stops of the lines are shown with their location, as well the path of the bus on the metro. Moreover buses move on the map simulating the real situation. You may see better from the screen-shots on the iTunes Store. – Fabrizio Bartolomucci Aug 18 '15 at 13:53
0

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...

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75