From 9903cbee0a1112fb97c5ca70373682dc3882b065 Mon Sep 17 00:00:00 2001 From: "Charles P." Date: Fri, 31 May 2024 20:55:22 +0200 Subject: [PATCH] initial commit --- .forgejo/workflows/test.yml | 16 ++++++++++++++ Dockerfile | 11 ++++++++++ README.md | 20 +++++++++++++++++- action.yml | 18 ++++++++++++++++ modify_gtfs.py | 42 +++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 6 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 .forgejo/workflows/test.yml create mode 100644 Dockerfile create mode 100644 action.yml create mode 100644 modify_gtfs.py create mode 100644 requirements.txt diff --git a/.forgejo/workflows/test.yml b/.forgejo/workflows/test.yml new file mode 100644 index 0000000..fa3a856 --- /dev/null +++ b/.forgejo/workflows/test.yml @@ -0,0 +1,16 @@ +name: GTFS Route Modification + +on: [push] + +jobs: + modify_routes: + runs-on: docker + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Run GTFS Route Modifier + uses: gtfs-actions/route-type-modifier@v1 + with: + gtfs_file: 'path/to/your/gtfs.zip' + routes: '1:3,2:5' \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fd758cf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.12-slim + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY modify_gtfs.py . +COPY action.yml . + +ENTRYPOINT ["python", "modify_gtfs.py"] diff --git a/README.md b/README.md index 02bf09a..1159d0d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ -# route-type-modifier +# GTFS Route Type Modifier +### Usage +```yaml +name: GTFS Route Type Modification +on: [push] + +jobs: + modify_routes: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Run GTFS Route Modifier + uses: gtfs-actions/route-type-modifier@v1 + with: + gtfs_file: 'path/to/your/gtfs.zip' + routes: '1:3,2:5' +``` \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..a8804d0 --- /dev/null +++ b/action.yml @@ -0,0 +1,18 @@ +name: 'GTFS Route Type Modifier' +description: 'Change route type in a GTFS feed.' + +inputs: + gtfs_file: + description: 'Path to GTFS .zip file.' + required: true + routes: + description: 'List of routes and types.' + required: true + output_file: + description: 'Path to output .zip file.' + required: false + default: '' + +runs: + using: 'docker' + image: 'Dockerfile' diff --git a/modify_gtfs.py b/modify_gtfs.py new file mode 100644 index 0000000..5edc79f --- /dev/null +++ b/modify_gtfs.py @@ -0,0 +1,42 @@ +import os +import sys +import zipfile +import pandas as pd +import tempfile + + +def modify_routes(gtfs_file, routes, output_file=None): + if not output_file: + output_file = gtfs_file + + # unzip gtfs feed + with tempfile.TemporaryDirectory() as tmpdirname: + with zipfile.ZipFile(gtfs_file, 'r') as zip_ref: + zip_ref.extractall(tmpdirname) + + # load routes.txt in a DataFrame + 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(':') + routes_df.loc[routes_df['route_id'] == route_id, 'route_type'] = int(new_type) + + # save to routes.txt + routes_df.to_csv(routes_path, index=False) + + # recreate zip file + 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)) + + +if __name__ == "main": + gtfs_file = sys.argv[1] + routes = sys.argv[2] + output_file = sys.argv[3] + + modify_routes(gtfs_file, routes, output_file) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a9109e6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pandas~=2.2.2 \ No newline at end of file