Android Studio公交应用:如何集成GTFS获取有轨电车发车时间
Got it, let's walk through exactly how to pull departure times from your local transit agency's GTFS file and get them into your static tram stop page—no fancy tools required, just some basic file parsing or a quick script.
First, Know Which GTFS Files Matter
GTFS has a bunch of files, but you only need 4 core ones for departure times:
stops.txt: Contains all transit stops (find your tram stop's unique ID here)trips.txt: Links each trip (a single run of a tram) to its routeroutes.txt: Has route names/numbers (so you can label departures with "Tram Line 7" instead of a random ID)stop_times.txt: The goldmine—lists every departure time for every stop, tied to a trip ID
Step 1: Find Your Stop's ID
Open stops.txt in a text editor or spreadsheet tool (Excel, Google Sheets). Search for your tram stop's name (look for the stop_name column). Once you find the row, note the stop_id value—this is your key to filtering departure times.
Step 2: Map Trips to Routes
Next, you need to connect each trip (from stop_times.txt) to its corresponding route. Open trips.txt and create a simple lookup: for each row, pair the trip_id with its route_id. If you're using a script, this is a dictionary; if manual, you can use VLOOKUP in a spreadsheet.
Then, open routes.txt to map route_id values to human-readable names (like route_short_name for "Line 5" or route_long_name for "Downtown to Riverfront").
Step 3: Extract Departure Times for Your Stop
Now head to stop_times.txt. Filter all rows where stop_id matches the one you found in Step 1. Each matching row has a departure_time—this is the time the tram leaves that stop.
A Quick Note on GTFS Time Format
GTFS uses a 24+ hour format (e.g., 25:15 means 1:15 AM the next day). You'll want to convert this to something user-friendly: split the time into hours/minutes, subtract 24 from hours if it's over 23, and add a "(Next Day)" label if needed.
Step 4: Integrate into Your Static Page
You have two paths here, depending on your comfort level:
Option 1: Manual Spreadsheet Work (Small Data Sets)
- Copy all filtered departure times into a spreadsheet
- Use VLOOKUP to add route names to each time
- Sort times by route and time
- Copy the final table into your HTML as a
<table>or<ul>list
Option 2: Automated Script (Better for Updates)
If you want to easily refresh data when the GTFS file updates, use a simple Python script to parse the files and output HTML directly. Here's a minimal example:
import csv from collections import defaultdict # Replace with your tram stop's name TARGET_STOP = "Main Street Tram Stop" # 1. Get stop ID stop_id = None with open("stops.txt", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: if row["stop_name"] == TARGET_STOP: stop_id = row["stop_id"] break if not stop_id: print("Error: Couldn't find your stop!") exit() # 2. Map trips to routes trip_to_route = {} with open("trips.txt", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: trip_to_route[row["trip_id"]] = row["route_id"] # 3. Map routes to names route_to_name = {} with open("routes.txt", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: route_to_name[row["route_id"]] = row["route_short_name"] # 4. Extract and format departures departures = defaultdict(list) with open("stop_times.txt", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: if row["stop_id"] != stop_id: continue route_name = route_to_name.get(trip_to_route[row["trip_id"]], "Unknown Route") raw_time = row["departure_time"] hours, mins, _ = raw_time.split(":") hours = int(hours) # Handle next-day times if hours >= 24: hours -= 24 formatted_time = f"{hours:02d}:{mins} (Next Day)" else: formatted_time = f"{hours:02d}:{mins}" departures[route_name].append(formatted_time) # 5. Generate HTML output print("<div class='tram-departures'>") for route in sorted(departures.keys()): print(f" <h3>{route}</h3>") print(" <ul>") for time in sorted(departures[route]): print(f" <li>{time}</li>") print(" </ul>") print("</div>")
Just save this as gtfs_parser.py, put it in the same folder as your GTFS files, replace TARGET_STOP with your stop's name, and run it with python gtfs_parser.py. The output is ready-to-use HTML you can paste into your static page.
Bonus: Keep Data Fresh
GTFS files are usually updated weekly or monthly by transit agencies. If you want your page to stay current, set up a simple cron job (Linux/macOS) or Task Scheduler (Windows) to run the script automatically and overwrite your static page's departure section.
内容的提问来源于stack exchange,提问作者Amic

