From 0af451b3c21ef2e91a18e85ca1479f7f908752b606812ada0da955f0df552b5a Mon Sep 17 00:00:00 2001 From: Johannes Randerath Date: Sat, 1 Jun 2024 15:49:46 +0200 Subject: [PATCH] Added GTFS import and export support --- transport_accessibility/pt_map/gtfs.py | 26 ++++++++++++++++++++++++- transport_accessibility/pt_map/views.py | 4 ++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/transport_accessibility/pt_map/gtfs.py b/transport_accessibility/pt_map/gtfs.py index bc862e3..ca9b318 100644 --- a/transport_accessibility/pt_map/gtfs.py +++ b/transport_accessibility/pt_map/gtfs.py @@ -1,4 +1,5 @@ import pandas as pd +import os class GTFS: def __init__(self, folder_path): @@ -27,7 +28,7 @@ class GTFS: try: return pd.read_csv(self.file_path) except FileNotFoundError: - return None + return pd.DataFrame() class Agency(GTFSFile): def __init__(self, folder_path): @@ -81,6 +82,29 @@ class GTFS: def __init__(self, folder_path): super().__init__(folder_path, 'feed_info') + def get_files(self): + return [attr for attr in list(set(dir(self)) - set(dir(GTFS))) if isinstance(getattr(self,attr),self.GTFSFile)] + + def get_fields(self, name): + file = getattr(self, name) + if not file: + return None + return list(set(dir(file)) - set(dir(GTFSFile))) + + def export(self, path, dirname): + path = f"{os.path.normpath(path)}/{dirname}" + if not os.path.exists(path): + os.mkdir(path) + print(self.get_files()) + for name in self.get_files(): + df = getattr(self, name).data + fpath = f"{path}/{name}.txt" + # print(f"name: {name}") + print(name) + df.to_csv(fpath, index=False) + + + def validate(self): self.validate_agency() self.validate_stops() diff --git a/transport_accessibility/pt_map/views.py b/transport_accessibility/pt_map/views.py index 7e77fef..d2b940c 100644 --- a/transport_accessibility/pt_map/views.py +++ b/transport_accessibility/pt_map/views.py @@ -3,6 +3,6 @@ from django.http import HttpResponse from .models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, FareAttribute, FareRule, Shape, Frequency, Transfer, FeedInfo def index(request): - - return HttpResponse("Test") + context = {} + return HttpResponse(request, "templates/map.html", context)