-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodels.py
More file actions
132 lines (106 loc) · 4.42 KB
/
models.py
File metadata and controls
132 lines (106 loc) · 4.42 KB
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
"""
DB module for generating a Stop data model
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
from flask_sqlalchemy import SQLAlchemy
from utilities import get_distance, get_stops
db = SQLAlchemy()
class Stop(db.Model):
"""Generate data model for MBTA stops"""
__tablename__ = 'mbta_stops'
stop_id = db.Column(db.String(50), primary_key=True)
name = db.Column(db.String(100), primary_key=True)
description = db.Column(db.String(200), nullable=True)
address = db.Column(db.String(100), nullable=True)
longitude = db.Column(db.Float, nullable=False)
latitude = db.Column(db.Float, nullable=False)
platform_code = db.Column(db.String(10), nullable=True)
platform_name = db.Column(db.String(100), nullable=True)
location_type = db.Column(db.Integer, nullable=True)
wheelchair_boarding = db.Column(db.Integer, nullable=True)
def __init__(self, stop_id, name, description, address, longitude, latitude,
platform_code, platform_name, location_type, wheelchair_boarding):
"""
Initialize a Stop object
:param stop_id: The ID of the stop (Some values are integers while others are strings)
:type: str
:param name: Name of the stop
:type: str
:param description: Description given to the stop by the MBTA
:type: str
:param address: The address associated with the stop
:type: str
:param longitude: Longitude of the stop
:type: float
:param latitude: Latitude of the stop
:type: float
:param platform_code: The code given to the platform associated with the stop
:type: int or None
:param platform_name: The name given to the platform associated with the stop
:type: str or None
:param location_type: The MBTA type of location
:type: int or None
:param wheelchair_boarding: Whether it's wheelchair accessible (0=False; 1=True)
:type: int
:return: Returns a Stop object
:rtype: Stop
"""
self.stop_id = stop_id
self.name = name
self.description = description
self.address = address
self.longitude = longitude
self.latitude = latitude
self.platform_code = platform_code
self.platform_name = platform_name
self.location_type = int(location_type) if location_type else None
self.wheelchair_boarding = int(wheelchair_boarding) if wheelchair_boarding else None
self.distance = 0
def within_distance(self, point, distance=(1.609344)/2.0):
"""
Returns True if the distance between `self` and `point` is ≤0.5 miles.
:param point: a python dictionary with keys lat and lon corresponding to a geo position
:type: dict
:param distance: Default value is (1.609344)/2.0, which is half a mile
:type: float
:return: True or False
:rtype: bool
"""
d = {'lat': self.latitude, 'lon': self.longitude}
self.distance = get_distance(d, point)
return self.distance <= distance
@classmethod
def from_api(cls, url):
"""
Given the URL endpoint to the MBTA stops API, the function should fetch all stops
and generate Stop objects from stops.
:param url: The endpoint for the stops API
:type: str
:return: Returns a list of Stop objects
:rtype: list
"""
output = []
stops = get_stops(url)
for stop in stops:
if stop['attributes']['latitude'] and stop['attributes']['longitude']:
if stop['attributes']['location_type'] != 3:
d = {'stop_id': stop.get('id')}
for k, v in stop.get('attributes').items():
d[k] = v
output.append(cls(**d))
return output
def serialize(self):
"""Provide a method for serializing this object"""
distance = self.distance * 0.621371 if hasattr(self, 'distance') else 0
return {
'stop_id': self.stop_id,
'name': self.name,
'address': self.address,
'longitude': self.longitude,
'latitude': self.latitude,
'platform_code': self.platform_code,
'platform_name': self.platform_name,
'location_type': self.location_type,
'wheelchair_boarding': self.wheelchair_boarding,
'distance': distance,
}