-
Notifications
You must be signed in to change notification settings - Fork 11
/
gtfs_plus.py
66 lines (51 loc) · 2.02 KB
/
gtfs_plus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import csv
import transitfeed
import pandas as pd
def routesft_assume_mode(schedule, default_mode):
routeid_to_mode_dict = dict(zip(schedule.routes.keys(),[default_mode]*len(schedule.routes.keys())))
return routeid_to_mode_dict
def get_transfer_dist(schedule):
for k,v in schedule._transfers.iteritems():
from_stop_id,to_stop_id = k
from_lat = schedule.stops[from_stop_id].stop_lat
from_lon = schedule.stops[from_stop_id].stop_lon
to_lat = schedule.stops[to_stop_id].stop_lat
to_lon = schedule.stops[to_stop_id].stop_lon
def haversine((lat1, lon1), (lat2, lon2)):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
returns miles
"""
from math import radians, cos, sin, asin, sqrt
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
miles = 0.621371 * km
return miles
def create_tranfers(schedule,max_xfer_dist = 0.5):
from itertools import combinations
5 #miles
#create pairwise iterator
stops = {}
for key,val in schedule.stops.iteritems():
stops[key]=(val.stop_lat, val.stop_lon)
pair = list(set(list(combinations(stops, r=2))))
#print "PAIR",pair
#get xfer distance between each combination
#potential_xfer_dict = {(pair[0][0],pair[1][0]): haversine(pair[0][1],pair[1][1]) }
potential_xfer_dict = {p:haversine(stops[p[0]],stops[p[1]]) for p in pair }
#delete pairs that are greater than maximum xfer distance
del_keys = []
for k,v in potential_xfer_dict.iteritems():
if v > max_xfer_dist:
del_keys.append(k)
for k in del_keys:
del potential_xfer_dict[k]
#return dictionary with valid values
return potential_xfer_dict