Created
March 12, 2026 22:06
-
-
Save Jaycso/037b62f91bed7902a3895171619945d9 to your computer and use it in GitHub Desktop.
Script to add location data to stops.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import requests | |
| def main(): | |
| input_file = "gtfs/stops.txt" | |
| output_file = "gtfs/stops_updated.txt" | |
| base_url = "https://bustimes.org/api/stops/?atco_code=" | |
| not_found = [] | |
| with ( | |
| open(input_file, mode="r", newline="", encoding="utf-8") as infile, | |
| open(output_file, mode="w", newline="", encoding="utf-8") as outfile, | |
| ): | |
| reader = csv.DictReader(infile) | |
| fieldnames = reader.fieldnames | |
| writer = csv.DictWriter(outfile, fieldnames=fieldnames) | |
| writer.writeheader() | |
| for row in reader: | |
| stop_id = row["stop_id"] | |
| print(f"Fetching data for: {stop_id}...") | |
| try: | |
| response = requests.get(base_url + stop_id) | |
| data = response.json() | |
| if data.get("results"): | |
| lon_lat = data["results"][0]["location"] | |
| row["stop_lon"] = lon_lat[0] | |
| row["stop_lat"] = lon_lat[1] | |
| print(f" successfully updated: {lon_lat}") | |
| else: | |
| not_found.append(stop_id) | |
| print(f" no data found for {stop_id}") | |
| except Exception as e: | |
| print(f" error fetching {stop_id}: {e}") | |
| writer.writerow(row) | |
| print(f"\nupdated file saved as {output_file}") | |
| if len(not_found) > 0: | |
| print(f"failed to find data for {len(not_found)} stops: {not_found}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment