change-route-type/modify_gtfs.py

58 lines
1.9 KiB
Python
Raw Normal View History

2024-05-31 18:55:22 +00:00
import os
import sys
import zipfile
import pandas as pd
import tempfile
2024-05-31 19:26:58 +00:00
import logging
2024-05-31 18:55:22 +00:00
def modify_routes(gtfs_file, routes, output_file=None):
logging.info(f"Running GTFS Change Route Type")
2024-05-31 19:26:58 +00:00
logging.info(f"GTFS File: {gtfs_file}")
logging.info(f"Routes to Change: {routes}")
logging.info(f"Output File: {output_file}")
2024-05-31 18:55:22 +00:00
if not output_file:
output_file = gtfs_file
# unzip gtfs feed
with tempfile.TemporaryDirectory() as tmpdirname:
2024-05-31 19:26:58 +00:00
logging.info(f"Unzipping GTFS file to {tmpdirname}")
2024-05-31 18:55:22 +00:00
with zipfile.ZipFile(gtfs_file, 'r') as zip_ref:
zip_ref.extractall(tmpdirname)
# load routes.txt in a DataFrame
2024-05-31 19:26:58 +00:00
logging.info(f"Reading routes.txt")
2024-05-31 18:55:22 +00:00
routes_path = os.path.join(tmpdirname, 'routes.txt')
routes_df = pd.read_csv(routes_path)
# edit route types
for route_change in routes.split(','):
route_id, new_type = route_change.split(':')
2024-05-31 19:26:58 +00:00
logging.info(f"Changing route {route_id} to type {new_type}")
2024-05-31 18:55:22 +00:00
routes_df.loc[routes_df['route_id'] == route_id, 'route_type'] = int(new_type)
# save to routes.txt
2024-05-31 19:26:58 +00:00
logging.info(f"Saving routes.txt")
2024-05-31 18:55:22 +00:00
routes_df.to_csv(routes_path, index=False)
# recreate zip file
2024-05-31 19:26:58 +00:00
logging.info(f"Recreating GTFS file at {output_file}")
2024-05-31 18:55:22 +00:00
with zipfile.ZipFile(output_file, 'w') as zip_ref:
for foldername, subfolders, filenames in os.walk(tmpdirname):
for filename in filenames:
file_path = os.path.join(foldername, filename)
zip_ref.write(file_path, os.path.relpath(file_path, tmpdirname))
2024-05-31 19:26:58 +00:00
logging.info(f"Done!")
2024-05-31 18:55:22 +00:00
2024-05-31 20:23:12 +00:00
if __name__ == "__main__":
2024-05-31 18:55:22 +00:00
gtfs_file = sys.argv[1]
routes = sys.argv[2]
output_file = sys.argv[3]
2024-05-31 19:26:58 +00:00
logging.basicConfig(level=logging.INFO)
2024-05-31 18:55:22 +00:00
modify_routes(gtfs_file, routes, output_file)