This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
generated from Malix-Labs/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (140 loc) · 5.62 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
import sqlite3
from starlette.middleware.cors import CORSMiddleware
from src.kruskal import Graph
from src.dijkstra import GraphDijkstra
app = FastAPI()
class Stop(BaseModel):
stop_id: str
stop_sequence: int
lon: float
lat: float
stop_name: str
class DijkstraResponse(BaseModel):
distance: float
path: List[int]
def get_db_connection():
conn = sqlite3.connect('src/mon_database.db')
conn.row_factory = sqlite3.Row
return conn
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Autoriser toutes les origines
allow_credentials=True,
allow_methods=["*"], # Autoriser toutes les méthodes
allow_headers=["*"], # Autoriser tous les en-têtes
)
@app.get("/stops/{line_name}", response_model=List[Stop])
def read_stops(line_name: str):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {line_name} ORDER BY stop_sequence")
stops = cursor.fetchall()
return [Stop(**dict(stop)) for stop in stops]
@app.get("/stop/{line_name}/{stop_id}", response_model=Stop)
def read_stop(line_name: str, stop_id: str):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {line_name} WHERE stop_id = ?", (stop_id,))
stop = cursor.fetchone()
if stop is None:
raise HTTPException(status_code=404, detail="Stop not found")
return Stop(**dict(stop))
@app.get("/acpm")
def get_kruskal():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM stations")
nb_vertices = cursor.fetchall()
nb_vertices = len(nb_vertices)
g = Graph(nb_vertices)
cursor.execute("SELECT * FROM concatligne")
liaisons = [list(row) for row in cursor.fetchall()]
for u, v, w in liaisons:
g.add_edge(int(u), int(v), int(w))
acpm = g.kruskal()
cursor.execute("SELECT stop_ids,id FROM stations")
stop_ids = cursor.fetchall()
stop_ids = {id: stop_id.split(',')[0] for stop_id, id in stop_ids}
acpm_id = [(stop_ids[u], stop_ids[v]) for u, v in acpm]
return acpm_id
@app.get("/acpm/points")
def get_kruskal_points():
kruskal = get_kruskal()
points = []
for u, v in kruskal:
if u not in points:
points.append(u)
if v not in points:
points.append(v)
lines = ["ligne1","ligne2","ligne3","ligne3b", "ligne4", "ligne5", "ligne6", "ligne7", "ligne7b", "ligne8", "ligne9", "ligne10", "ligne11", "ligne12", "ligne13", "ligne14"]
conn = get_db_connection()
cursor = conn.cursor()
stops = []
for line in lines:
cursor.execute(f"SELECT * FROM {line}")
stops += cursor.fetchall()
stops = [Stop(**dict(stop)) for stop in stops]
points = [stop for stop in stops if stop.stop_id in points]
return points
@app.get("/dijkstra/{src}/{dest}", response_model=DijkstraResponse)
def get_dijkstra(src: int, dest: int):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM stations")
nb_vertices = cursor.fetchone()[0]
g = GraphDijkstra(nb_vertices)
cursor.execute("SELECT * FROM concatligne")
liaisons = [list(row) for row in cursor.fetchall()]
for u, v, w in liaisons:
g.graph[int(u)][int(v)] = int(w)
g.graph[int(v)][int(u)] = int(w) # Assuming undirected graph
if src >= nb_vertices or src < 0 or dest >= nb_vertices or dest < 0:
raise HTTPException(status_code=400, detail="Invalid source or destination vertex")
distance, path = g.shortest_path(src, dest)
return DijkstraResponse(distance=distance, path=path)
@app.get("/stations/")
def read_stations():
lines = ["ligne1","ligne2","ligne3","ligne3b", "ligne4", "ligne5", "ligne6", "ligne7", "ligne7b", "ligne8", "ligne9", "ligne10", "ligne11", "ligne12", "ligne13", "ligne14"]
conn = get_db_connection()
cursor = conn.cursor()
stations = []
for line in lines:
cursor.execute(f"""
SELECT nt.*, l.lon, l.lat, l.stop_sequence, l.stop_id as line_stop_id
FROM stations nt
JOIN {line} l ON nt.stop_ids LIKE '%' || l.stop_id || '%'
ORDER BY l.stop_sequence
""")
line_stations = cursor.fetchall()
for i, station in enumerate(line_stations):
station_dict = dict(station)
station_dict["line"] = line
# Add the stop_id of the next station
if i < len(line_stations) - 1:
next_station_id = line_stations[i + 1]["line_stop_id"]
cursor.execute(f"""
SELECT id
FROM stations
WHERE stop_ids LIKE '%' || ? || '%'
""", (next_station_id,))
next_station_new_table_id = cursor.fetchone()
station_dict["next_stop_id"] = next_station_new_table_id["id"] if next_station_new_table_id else ""
else:
station_dict["next_stop_id"] = ""
# Add the stop_id of the previous station
if i > 0:
prev_station_id = line_stations[i - 1]["line_stop_id"]
cursor.execute(f"""
SELECT id
FROM stations
WHERE stop_ids LIKE '%' || ? || '%'
""", (prev_station_id,))
prev_station_new_table_id = cursor.fetchone()
station_dict["prev_stop_id"] = prev_station_new_table_id["id"] if prev_station_new_table_id else ""
else:
station_dict["prev_stop_id"] = ""
stations.append(station_dict)
return stations