-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
99 lines (69 loc) · 3.16 KB
/
main.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
from flask import Flask, jsonify
from flask_cors import CORS
from models.sbb_station_parking import SBBStationParking
from models.sbb_lines import TransportOpenData
from models.sbb_parking_availability import ParkingAvailability
from utils.distance import compute_route
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return jsonify(SBBStationParking.get_info_for_station(4100))
@app.route('/data', methods=['GET'])
def get_data():
return jsonify(SBBStationParking.get_data())
@app.route('/geo/stations', methods=['GET'])
def get_geo_stations():
# Return all the stations contained in the data
return jsonify(SBBStationParking.get_geo_info_for_all_stations())
@app.route('/stations', methods=['GET'])
def get_stations():
# Return all the stations contained in the data
return jsonify(SBBStationParking.get_info_for_all_stations())
@app.route('/station/<int:didok>', methods=['GET'])
def get_station(didok):
# Return the station with the corresponding didok
return jsonify(SBBStationParking.get_info_for_station(didok))
@app.route('/route/start/<string:start>/end/<string:end>', methods=['GET'])
def get_route(start, end):
# Return the route between two stations
# Return the cost between two stations
transporter = TransportOpenData(start, end)
cost_dict = {'cost': transporter.get_cost()}
geo_dict = transporter.get_geo_json()
return jsonify({ **cost_dict, **geo_dict })
@app.route('/find-stations/lat/<string:lat>/lon/<string:lon>', methods=['GET'])
def get_closest_stations(lat, lon):
# Return the closest 10 stations based on the location passed
return jsonify(SBBStationParking.get_closest_stations(float(lat), float(lon)))
@app.route('/find-available-stations/lat/<string:lat>/lon/<string:lon>', methods=['GET'])
def get_closest_stations_with_availability(lat, lon):
# Return the closest 10 stations based on the location passed
stations = SBBStationParking.get_closest_stations(float(lat), float(lon))
station_names = []
for station in stations:
station_name = station['station_name']
station_names.append(station_name)
parking_inspector = ParkingAvailability()
availabilities = parking_inspector.get_stations_availability(station_names)
for i, station in enumerate(stations):
availability = availabilities[i]
station['station']['available_spots'] = availability
return jsonify(stations)
@app.route('/get-possible-routes/v1/lat/<string:lat>/lon/<string:lon>/destination/<string:destination>',
methods=['GET'])
def get_possible_routes(lat, lon, destination):
# Return the closest 10 stations based on the location passed
closest_stations = SBBStationParking.get_closest_stations(float(lat), float(lon))
routes = []
for station in closest_stations:
station_name = station['station']['station_name']
transporter = TransportOpenData(station_name, destination)
route = compute_route(transporter, station)
if not route:
continue
routes.append(route)
routes = sorted(routes, key=lambda x: x['total_cost'])
return jsonify(routes)
if __name__ == '__main__':
app.run(debug=True)