-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigator.py
221 lines (186 loc) · 8.71 KB
/
navigator.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import json
import requests
import osmnx as ox
import networkx as nx
import urllib.parse
from prettytable import PrettyTable
class Navigator:
def __init__(self):
self.G = ox.graph_from_address(
"Via Capruzzi, Bari, Italy",
dist=3500,
network_type="drive",
truncate_by_edge=True,
)
def askDestination(self):
with open('Resources\meccanici.json', 'r') as f:
data = json.load(f)
x = PrettyTable()
x.field_names = ["Id", "Nome", "Indirizzo"]
for d in data:
x.add_row([d["_id"], d["nome"], d["indirizzo"]])
print("Ecco una lista delle officine di Bari: \n")
print(x)
flag = False
while(not(flag)):
userInput = ''
userInput = input("Inserire l'id dell'officina desiderata --> ")
try:
id = int(userInput)
if(id < 1 or id > 13):
raise Exception("Errore! Id non valido. L'id deve essere compreso tra 1 e 13\n")
flag = True
except (ValueError, Exception) as ex:
print("Errore! L'id inserito deve essere un intero compreso tra 1 e 13.\n")
return id
def askOrigin(self):
print("\nInserire l'indirizzo da cui vuoi partire nel formato:\n",
"via, numero, citta' (quest'ultima deve essere Bari).\n",
"Le virgole non sono obbligatorie ma aiutano nella ricerca di OSM\n",
"maggiori saranno i dettagli inseriti migliore sara' la precisione della ricerca.")
address = input("--> ")
return address
def askKindOfSearch(self):
question = "Quale algoritmo vuoi che venga usato per la ricerca del percorso?\n"
question += "\t(1) algoritmo di ricerca che minimizza la distanza euclidea\n"
question += "\t(2) algoritmo di Dijkstra\n\t(3) algoritmo A*\n"
question += "\t(4) tutti e tre i precedenti elencati"
print(question)
flag = False
while(not(flag)):
userInput = ''
userInput = input(" --> ")
try:
answer = int(userInput)
if(answer < 1 or answer > 4):
raise Exception()
flag = True
except (ValueError, Exception) as ex:
print("Errore! Risposta non valida. La risposta deve essere un INTERO compreso tra 1 e 4\n")
return answer
def getCoordinateFromId(self, id):
with open('Resources\meccanici.json', 'r') as f:
data = json.load(f)
coordinate = []
for d in data:
if(d["_id"] == id):
coordinate.append(d["latitudine"])
coordinate.append(d["longitudine"])
return coordinate
def getCoordinateFromAddress(self, address):
#address = 'Via Francesco Campione, 62, 70124 Bari BA'
url = 'https://nominatim.openstreetmap.org/search/' + urllib.parse.quote(address) +'?format=json'
response = requests.get(url).json()
coordinate = []
coordinate.append(float(response[0]["lat"]))
coordinate.append(float(response[0]["lon"]))
return coordinate
def getAllCoordinate(self):
with open('Resources\meccanici.json', 'r') as f:
data = json.load(f)
coordinate = []
for d in data:
coordinate.append((d['latitudine'], d['longitudine']))
return coordinate
def searchNearestMechanic(self):
minRouteDistance = 0
minRoute = []
flag = False
while(not(flag)):
try:
origin = self.getCoordinateFromAddress(self.askOrigin())
flag = True
except IndexError as ie:
print("Errore! Localita' non trovata")
originNode = ox.distance.nearest_nodes(self.G, origin[1], origin[0])
for c in self.getAllCoordinate():
pathDistance = 0
destinationNode = ox.distance.nearest_nodes(self.G, c[1], c[0])
route = ox.shortest_path(self.G, originNode, destinationNode, weight = 'length')
for distance in ox.utils_graph.get_route_edge_attributes(self.G, route, 'length', minimize_key='length'):
pathDistance += distance
if minRouteDistance == 0:
minRouteDistance = pathDistance
minRoute = route
if pathDistance < minRouteDistance:
minRouteDistance = pathDistance
minRoute = route
return minRoute
#euclidean distance
def dist(self, a, b):
x1 = self.G._node[a]['y']
y1 = self.G._node[a]['x']
x2 = self.G._node[b]['y']
y2 = self.G._node[b]['x']
return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
def searchMechanic(self):
flag = False
while(not(flag)):
try:
origin = self.getCoordinateFromAddress(self.askOrigin())
flag = True
except IndexError as ie:
print("Errore! Localita' non trovata")
destination = self.getCoordinateFromId(self.askDestination())
origin_node = ox.distance.nearest_nodes(self.G, origin[1], origin[0])
destination_node = ox.distance.nearest_nodes(self.G, destination[1], destination[0])
answer = self.askKindOfSearch()
if (answer == 1):
#euclidean distance search algorithm
route = ox.shortest_path(self.G, origin_node, destination_node, weight = 'length')
fig, ax = ox.plot_graph_route(self.G, route, route_color="c", node_size=0)
elif (answer == 2):
#dijkstra search algorithm
length,route=nx.single_source_dijkstra(self.G,origin_node, destination_node, weight='length')
fig, ax = ox.plot_graph_route(self.G, route, route_color="c", node_size=0)
elif (answer == 3):
#a* search algorithm ----definire funzione euristica
route = nx.astar_path(self.G, origin_node, destination_node, heuristic=self.dist, weight = 'length')
fig, ax = ox.plot_graph_route(self.G, route, route_color="c", node_size=0)
elif (answer == 4):
route = ox.shortest_path(self.G, origin_node, destination_node, weight = 'length')
length,route2=nx.single_source_dijkstra(self.G,origin_node, destination_node, weight='length')
route3 = nx.astar_path(self.G, origin_node, destination_node, heuristic=self.dist, weight = 'length')
print("In celeste: algoritmo che minimizza la distanza euclidea\n"+
"in blu: algoritmo di Dijkstra\n" + "in rosso: algoritmo A*")
fig, ax = ox.plot_graph_route(self.G, route, route_color="c", node_size=0)
fig, ax = ox.plot_graph_route(self.G, route2, route_color="b", node_size=0)
fig, ax = ox.plot_graph_route(self.G, route3, route_color="r", node_size=0)
#print(ox.utils_graph.get_route_edge_attributes(G2, route, 'length', minimize_key='length'))
def askOperation(self):
exit = False
while(not(exit)):
print("Quale operazione vuoi fare?")
print("\t(1) Cercare il percorso per un meccanico.")
print("\t(2) Cercare il percorso per il meccanico piu' vicino.")
print("\t(3) Terminare il programma.")
flag = False
while(not(flag)):
userInput = ''
userInput = input("Inserire il numero dell'operazione da effettuare --> ")
try:
operation = int(userInput)
if(operation < 1 or operation > 3):
raise Exception()
flag = True
except (ValueError, Exception) as ex:
print("Errore! L'input inserito deve essere un intero compreso tra 1 e 3.\n")
if(operation == 1):
self.searchMechanic()
elif(operation == 2):
minRoute = self.searchNearestMechanic()
fig, ax = ox.plot_graph_route(self.G, minRoute, route_color="c", node_size=0)
elif(operation == 3):
print("Programma terminato.")
exit = True
def main():
navigator = Navigator()
print("=======================================================================")
print("| Benvenuto nel navigatore |")
print("| |")
print("|Puoi trovare il percorso per un meccanico nella citta' di Bari |")
print("|attraverso l'uso di algoritmi di ricerca su grafo. |")
print("=======================================================================")
navigator.askOperation()
if __name__ == '__main__':
main()