Programming · free software · the open webSubscribe via RSS

How Can You Count GTFS Trips for a Service Date?

From the earlier reference collection. Read the current software blog.

Determine which service IDs operate on the requested date, apply calendar exceptions, then count trips assigned to those IDs.

Start with the service date

A GTFS trip record refers to a service ID, and the service calendar determines when that ID runs. Counting every row in trips.txt therefore answers how many trip definitions exist, not how many are active on a particular date. Begin by parsing the requested date and identifying the active service IDs for that date.

The schedule reference describes calendar.txt and calendar_dates.txt as part of the feed structure. Their interaction matters: a date-specific exception can add or remove service compared with the recurring weekly calendar. [GTFS schedule reference](https://gtfs.org/documentation/schedule/reference/)

Resolve the regular calendar

For each row in calendar.txt, check that the date falls within its start and end range and that the weekday flag for that date is enabled. Add the row's service_id to the active set when both conditions hold. Use actual calendar dates to determine the weekday; do not assume that a date's position in the file or its numerical day can stand in for a weekday calculation.

For example, if service WEEKDAY is in range and its Tuesday flag is set, it contributes on a Tuesday in that range. A service whose range starts after the requested date does not contribute even if its weekday flag matches.

Apply date-specific exceptions

Next inspect calendar_dates.txt for rows matching the requested date. An exception type that adds service inserts that service ID into the active set; one that removes service deletes it. Apply these changes after computing the regular calendar so the date-specific record can override the regular pattern.

A useful diagnostic reports how many IDs came from the regular calendar and how many were changed by exceptions. If the feed has no regular calendar and relies on explicit dates, the exception table may still define service. Follow the feed's actual files rather than assuming a particular combination is always present.

Count matching trip records

Finally, count rows in trips.txt whose service_id belongs to the resolved active set. Parse CSV properly, since a simple split on commas can fail on quoted fields. Report the date and, ideally, the selected service IDs or a compact summary so the number can be audited.

State what the count means: it is matching trip records, not necessarily individual departures at a stop or vehicle movements observed in real time. A careful sequence—calendar range and weekday, date exceptions, then trip matching—makes the result reproducible and explains why a date-specific count differs from the total number of trip definitions.

References

  1. GTFS Schedule Reference

Suggest a correction · All posts