Added GTFS import and export support

This commit is contained in:
Johannes Randerath 2024-06-01 15:49:46 +02:00
parent 586302bb34
commit 0af451b3c2
2 changed files with 27 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import pandas as pd import pandas as pd
import os
class GTFS: class GTFS:
def __init__(self, folder_path): def __init__(self, folder_path):
@ -27,7 +28,7 @@ class GTFS:
try: try:
return pd.read_csv(self.file_path) return pd.read_csv(self.file_path)
except FileNotFoundError: except FileNotFoundError:
return None return pd.DataFrame()
class Agency(GTFSFile): class Agency(GTFSFile):
def __init__(self, folder_path): def __init__(self, folder_path):
@ -81,6 +82,29 @@ class GTFS:
def __init__(self, folder_path): def __init__(self, folder_path):
super().__init__(folder_path, 'feed_info') 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): def validate(self):
self.validate_agency() self.validate_agency()
self.validate_stops() self.validate_stops()

View File

@ -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 from .models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, FareAttribute, FareRule, Shape, Frequency, Transfer, FeedInfo
def index(request): def index(request):
context = {}
return HttpResponse("Test") return HttpResponse(request, "templates/map.html", context)