-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
77 lines (58 loc) · 3.57 KB
/
app.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
import os, shutil
from flask import Flask, render_template, request, redirect, url_for
import osmnx as ox
import heapq
import random
import sys
from src import dijkstra, astar
from utils import algorithmsUtils
from haversine import haversine, Unit
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True,
template_folder='templates')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
@app.route('/')
def hello():
return render_template('index.html')
@app.route('/compare', methods=['POST', 'GET'])
def compare():
if request.method == 'POST':
ox.config(use_cache=True)
startingPoint = ox.geocode(request.form['startingPoint'])
endingPoint = ox.geocode(request.form['endingPoint'])
astarRouteColor = request.form['astarRouteColor']
astarBgColor = request.form['astarBgColor']
dijkstraRouteColor = request.form['dijkstraRouteColor']
dijkstraBgColor = request.form['dijkstraBgColor']
north, east, south, west = 0, 0, 0, 0
if startingPoint[0] >= endingPoint[0]:
north, south = startingPoint[0], endingPoint[0]
else:
north, south = endingPoint[0], startingPoint[0]
if startingPoint[1] >= endingPoint[1]:
east, west = startingPoint[1], endingPoint[1]
else:
east, west = endingPoint[1], startingPoint[1]
G = ox.graph_from_bbox(north + 0.01, south - 0.01, east + 0.01, west - 0.01, network_type="drive", simplify=True)
orig = ox.get_nearest_node(G, startingPoint)
destination = ox.get_nearest_node(G, endingPoint)
astarGraphObject = astar.astarGraph(G)
dijkstraGraphObject = dijkstra.dijkstraGraph(G)
astarPath, astarVerticesExplored, astarEdgesExplored, astarCost = astarGraphObject.astar(orig, destination)
astarResult = f"Distance from [{request.form['startingPoint']} → {request.form['endingPoint']}] is {str(astarCost)} meters away 🎉 \n A* 🗾 calculated/explored {str(astarVerticesExplored)} vertices and {str(astarEdgesExplored)} edges."
fig, ax = ox.plot_graph_route(G, astarPath, route_linewidth=4, node_size=0, route_color=astarRouteColor, save=True, bgcolor=astarBgColor, edge_color="#161616", filepath="static/img/astar.jpg", show=False, close=True)
dijkstraPath, dijkstraVerticesExplored, dijkstraEdgesExplored, dijkstraCost = dijkstraGraphObject.dijkstra(orig, destination)
dijkstraResult = f"Distance from [{request.form['startingPoint']} → {request.form['endingPoint']}] is {str(dijkstraCost)} meters away 🎉 | \n Dijkstra's 🗾 calculated/explored {str(dijkstraVerticesExplored)} vertices and {str(dijkstraEdgesExplored)} edges."
# verticesDifference = f"The percentage difference of the calculated edges between between Dijkstra's and A* is {algorithmsUtils.percentage(dijkstraVerticesExplored, astarVerticesExplored)}"
fig, ax = ox.plot_graph_route(G, dijkstraPath, route_linewidth=4, node_size=0, route_color=dijkstraRouteColor, save=True, bgcolor=dijkstraBgColor, edge_color="#161616", filepath="static/img/dijkstra.jpg", show=False, close=True)
# shutil.rmtree('cache')
return render_template('index.html', astar="astar.jpg", dijkstra="dijkstra.jpg", astarResult = astarResult, dijkstraResult = dijkstraResult, request = request)
return app
if __name__ == "__main__":
app.run(host="0.0.0.0")