|
|
@ -4,6 +4,9 @@ import json |
|
|
|
from os.path import join |
|
|
|
import re |
|
|
|
import datetime |
|
|
|
from decimal import Decimal |
|
|
|
from django.contrib.gis.geos import Point |
|
|
|
import csv2kml |
|
|
|
|
|
|
|
def import_stations(): |
|
|
|
stations = json.load(open(join(DATA_DIR, "stations.json"))) |
|
|
@ -162,8 +165,110 @@ def getDate(s): |
|
|
|
date = int(matches[0][1]) |
|
|
|
return "%02d-%02d" % (monthInt, date,) |
|
|
|
|
|
|
|
def getMinutes(s): |
|
|
|
if s is None: |
|
|
|
return None |
|
|
|
s = s.replace('m', '').strip() |
|
|
|
if s == '': |
|
|
|
return None |
|
|
|
else: |
|
|
|
return s |
|
|
|
|
|
|
|
def getBool(s): |
|
|
|
if s == None or int(s) == 0: |
|
|
|
return False |
|
|
|
else: |
|
|
|
return True |
|
|
|
|
|
|
|
def import_schedule(): |
|
|
|
schedules = json.load(open(join(DATA_DIR, "schedule.json"))) |
|
|
|
errors = [] |
|
|
|
for s in schedules: |
|
|
|
schedule = Schedule() |
|
|
|
schedule.data_id = s['id'] |
|
|
|
schedule.arrival = getTime(s['arrival']) |
|
|
|
schedule.departure = getTime(s['departure']) |
|
|
|
schedule.halt = getMinutes(s['halt']) |
|
|
|
try: |
|
|
|
schedule.stop_number = Decimal(s['stop_number']) |
|
|
|
except: |
|
|
|
errors.append({ |
|
|
|
'typ': 'Invalid Stop Number', |
|
|
|
'data_id': s['id'], |
|
|
|
'stop_number': s['stop_number'] |
|
|
|
}) |
|
|
|
schedule.stop_number = Decimal('0') |
|
|
|
try: |
|
|
|
schedule.station = Station.objects.get(code=s['station_code']) |
|
|
|
except: |
|
|
|
schedule.station = Station.objects.filter(code=s['station_code'])[0] |
|
|
|
errors.append({ |
|
|
|
'typ': 'Invalid or non-unique station code (NOT IMPORTED)', |
|
|
|
'data_id': s['id'], |
|
|
|
'code': s['station_code'] |
|
|
|
}) |
|
|
|
continue |
|
|
|
try: |
|
|
|
schedule.train = Train.objects.get(number=s['train_number']) |
|
|
|
except: |
|
|
|
schedule.train = Train.objects.filter(number=s['train_number'])[0] |
|
|
|
errors.append({ |
|
|
|
'typ': 'Invalid or non-unique train code (NOT IMPORTED)', |
|
|
|
'data_id': s['id'], |
|
|
|
'train_number': s['train_number'] |
|
|
|
}) |
|
|
|
continue |
|
|
|
try: |
|
|
|
schedule.day = int(s['day']) |
|
|
|
except: |
|
|
|
errors.append({ |
|
|
|
'typ': 'Day Error', |
|
|
|
'data_id': s['id'], |
|
|
|
'day': s['day'] |
|
|
|
}) |
|
|
|
schedule.day = 0 |
|
|
|
|
|
|
|
try: |
|
|
|
schedule.distance_travelled = int(s['distance_travelled']) |
|
|
|
except: |
|
|
|
errors.append({ |
|
|
|
'typ': 'Day Error', |
|
|
|
'data_id': s['id'], |
|
|
|
'distance': s['distance_travelled'] |
|
|
|
}) |
|
|
|
schedule.distance_travelled = 0 |
|
|
|
try: |
|
|
|
schedule.save() |
|
|
|
except: |
|
|
|
errors.append({ |
|
|
|
'typ': 'Something foobarred on Save :(', |
|
|
|
'data_id': s['id'] |
|
|
|
}) |
|
|
|
errorsFile = open(join(DATA_DIR, "scheduleErrors.json"), "w") |
|
|
|
json.dump(errors, errorsFile, indent=2) |
|
|
|
errorsFile.close() |
|
|
|
|
|
|
|
def geolocate_stations(): |
|
|
|
errors = [] |
|
|
|
for s in Station.objects.filter(point=None)[0:10]: |
|
|
|
coords = csv2kml.get_coordinates(s.address) |
|
|
|
if coords is not None: |
|
|
|
pt = Point(coords[0], coords[1]) |
|
|
|
s.point = pt |
|
|
|
print s.name |
|
|
|
s.save() |
|
|
|
else: |
|
|
|
errors.append({ |
|
|
|
'typ': 'Failed to geolocate', |
|
|
|
'data_id': s.data_id, |
|
|
|
'address': s.address |
|
|
|
}) |
|
|
|
errorsFile = open(join(DATA_DIR, "geolocateErrors.json"), "w") |
|
|
|
json.dump(errors, errorsFile, indent=2) |
|
|
|
errorsFile.close() |
|
|
|
|
|
|
|
def import_all(): |
|
|
|
import_stations() |
|
|
|
import_trains() |
|
|
|
import_schedules() |
|
|
|
|
|
|
|