I think you've arrived at your own answer here: Stops are ordered in sequence only along a specific trip, of which a route normally has many. This is meant to accommodate routes that have multiple branches or that change their path at certain times, such as a route that makes a diversion through an industrial park during rush hour.
What you'll need to do is first identify a trip that is typical of the route you intend to plot, and note its trip ID. To get a list of all the trips along a specific route, run a query like
SELECT id, headsign, short_name, direction_id
FROM trips
WHERE route_id = <route_id>;
Once you've selected a trip, getting the list of the stops it visits, in order, is straightforward:
SELECT code, name, lat, lon, arrival_time, departure_time
FROM stops
INNER JOIN stop_times ON stop_times.stop_id = stops.id
WHERE trip_id = <trip_id>
ORDER BY stop_sequence ASC;
(I've added a few extra fields here for clarity; it sounds like all you really need are the lat
and lon
fields included in the results.)
So how do you identify a "typical" trip for the route you want to plot? Often the headsign information for a trip indicates its branch or destination. If you need to be more specific—identifying trips that run between certain hours on certain days, for instance—the information in the calendars
and calendar_dates
tables can help you narrow these down.