/
model.py
139 lines (95 loc) · 4.21 KB
/
model.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.orm import sessionmaker, scoped_session, relationship, backref
engine = create_engine("sqlite:///septa.db", echo=True)
session = scoped_session(sessionmaker(bind=engine,
autocommit = False,
autoflush = False))
Base = declarative_base()
Base.query = session.query_property()
############################################################
#CLASSES
class Paths(Base):
__tablename__ = "paths"
id = Column(Integer, primary_key=True)
start_stop = Column(Integer, ForeignKey('stops.id'), nullable=False)
end_stop = Column(Integer, ForeignKey('stops.id'), nullable=False)
cost = Column(Integer, nullable=False)
class Routes(Base):
__tablename__ = "routes"
id = Column(Integer, primary_key=True)
route_short_name = Column(Integer, nullable=False)
route_long_name = Column(String(64), nullable=False)
route_type = Column(String(64), nullable=False)
class Stops(Base):
__tablename__ = "stops"
id = Column(Integer, primary_key=True)
stop_name = Column(String(64), nullable=False)
stop_lat = Column(Float, nullable=True)
stop_lon = Column(Float, nullable=True)
stop_type = Column(String(64), nullable=False)
line_name = Column(String(64), nullable=False)
# stop_times = relationship("StopTimes", backref=backref("stop", order_by=id))
# trips = relationship("Trips", backref=backref("stops", order_by=id))
# paths = relationship("Paths", backref=backref("stops", order_by=id))
class StopTimes(Base):
__tablename__ = "stop_times"
id = Column(Integer, primary_key=True)
trip_id = Column(Integer, ForeignKey('trips.id'), nullable=False)
arrival_time = Column(String(64), nullable=False)
stop_id = Column(Integer, ForeignKey('stops.id'), nullable=False)
# stop = relationship("Stops", backref=backref("stop_times", order_by=id))
class Trips(Base):
__tablename__ = "trips"
id = Column(Integer, primary_key=True)
route_id = Column(Integer, ForeignKey('routes.id'), nullable=False)
trip_id = Column(Integer, ForeignKey('trips.id'), nullable=False)
direction_id = Column(Integer, nullable=False)
class ShortestRoute(Base):
__tablename__ = "shortest_routes"
id = Column(Integer, primary_key=True)
start_stop = Column(Integer, ForeignKey('stops.id'), nullable=False)
end_stop = Column(Integer, ForeignKey('stops.id'), nullable=False)
stops_hit = Column(String(200), nullable=False)
############################################################
#FUNCTIONS
#check the accessbility of stops found within the radius around the starting lat/long. If a stop is accessible, return that stops details. If it gets through the whole list and no stops are accessible, return False.
def access_check(stop_list):
for stop in stop_list:
stop = int(stop)
stop_access = session.query(Stops).filter_by(id=stop).all()
if stop_access:
print "WINNER: ", stop
return stop
return False
def get_paths():
paths = session.query(Paths).all()
return paths
def get_stops():
stops = session.query(Stops).all()
return stops
def get_stop(stop_id):
stop = session.query(Stops).filter_by(id=int(stop_id)).all()
return stop[0]
def get_shortest_route(start_stop, end_stop):
shortest_route = session.query(ShortestRoute).filter_by(start_stop=start_stop, end_stop=end_stop).all()
stop_names = []
if shortest_route:
route_string = shortest_route[0].stops_hit
route_string = route_string.split(",")
for stop in route_string:
stop_object = session.query(Stops).filter_by(id=int(stop)).all()
stop_name = stop_object[0].stop_name
stop_line = stop_object[0].line_name
name_line_string = stop_name + "(" + stop_line + ")"
stop_names.append(name_line_string)
return ", ".join(stop_names)
else:
return False
##########################################
#CREATE
def create_tables():
Base.metadata.create_all(engine)
if __name__ == "__main__":
create_tables()