forked from OSM-Interrail-Planner/osm_interrail_planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
140 lines (114 loc) · 5.29 KB
/
server.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
from flask import Flask, render_template, request, redirect
import geopandas as gpd
import folium
import main
import etl as e
import flask_folium as ff
import json
from datetime import datetime, time
from threading import Timer
import webbrowser
app = Flask(__name__)
@app.route("/")
def start():
return render_template('index.html')
@app.route("/countries")
def select_countries():
all_countries_list = ['Portugal', 'Spain', "France", "Nederland", "Norway", "Sweden", "United Kingdom", "Schweiz", "Austria", "Belgium", "Luxembourg", "Italia", "Germany", "Ireland", "Liechtenstein", "Danmark", "Polska", "Czechia", "Slovensko" ,"Hungary", "Slovenia", "Croatia", "Bosnia and Herzegovina", "Serbia", "Montenegro", "Albania", "Kosovo", "North Macedonia", "Greece", "Bulgaria", "Romania", "Moldova", "Lithuania", "Latvia", "Estonia", "Finland"]
all_countries_list.sort()
all_countries_list.append('None')
return render_template('country.html', option_list = all_countries_list)
@app.route("/city_selection_in/<str1>/<str2>/<str3>/<str4>/<str5>/<str6>")
def select_cities(str1, str2, str3, str4, str5, str6 ):
list_country = [str1, str2, str3, str4, str5, str6]
list_country = set(list_country)
if 'None' in list_country:
list_country.remove('None')
# Perform OSM data extraction from Overpass API
main.extraction(list_country)
# Perform the preprocessin of the data (including the routable network)
all_cities_list = main.network_preprocessing(list_country)
all_cities_list.sort()
all_cities_list.append('None')
# Save the list of all cities so they can be reused when the routing fails
all_cities_dict = {'list' : all_cities_list}
with open('data/original/all_cities_json.txt', 'w') as all_cities_json:
json.dump(all_cities_dict, all_cities_json)
return render_template('city.html', option_list = all_cities_list)
@app.route("/route_between/<str1>/<str2>/<str3>/<str4>/<str5>/<str6>")
def base(str1, str2, str3, str4, str5, str6):
list_city = [str1, str2, str3, str4, str5, str6]
if 'None' in list_city:
list_city.remove('None')
# Solve the TSP with shortest paths between all destinations
dict_distance_matrix = main.routing(list_city)
# Check if there is an error city and if true recall the city selection site
if "error_city" in dict_distance_matrix.keys():
# get the city list from file
with open('data/original/all_cities_json.txt') as all_cities_json:
all_cities_dict = json.load(all_cities_json)
all_cities_list = []
for element in all_cities_dict['list']:
all_cities_list.append(element)
# print hint of which cities cannot be used
head = f"""
<p style="font-family: Verdana, Geneva, Tahoma, sans-serif;color:rgb(102, 0, 0);float: center;text-align:center;font-size:30px;">
Sorry! We couldn't find a path to/from {dict_distance_matrix["error_city"]} </p>
"""
return head + render_template('city.html', option_list = all_cities_list)
# this is base map
map = folium.Map(
location=[45, 5],
zoom_start=4,
tiles="Stamen Terrain"
)
# Prepare route data
gdf_best_route = gpd.read_file("data/route/best_route")
gdf_best_route = gdf_best_route.to_crs("EPSG:4326")
# create lines from shapely (lon, lat), to folium (lat, lon)
gdf_best_route = ff.line_geom(gdf_best_route)
# Prepare close city data if not empty
try:
gdf_close_cities = gpd.read_file("data/route/close_cities").set_crs("EPSG:32629")
gdf_close_cities = gdf_close_cities.to_crs("EPSG:4326")
# create lines from shapely (lon, lat), to folium (lat, lon)
gdf_close_cities = ff.point_geom(gdf_close_cities)
except: pass
# Prepare close heritage data if not empty
try:
gdf_close_heris = gpd.read_file("data/route/close_heris").set_crs("EPSG:32629")
gdf_close_heris = gdf_close_heris.to_crs("EPSG:4326")
# create lines from shapely (lon, lat), to folium (lat, lon)
gdf_close_heris = ff.point_geom(gdf_close_heris)
except: pass
# Prepare close nature data if not empty
try:
gdf_close_natus = gpd.read_file("data/route/close_natus").set_crs("EPSG:32629")
gdf_close_natus = gdf_close_natus.to_crs("EPSG:4326")
except: pass
# add the nature parks
try:
ff.add_nature_to_map(gdf_close_natus, map)
except: pass
# add the corresponding close cities
try:
ff.add_close_cities_to_map(gdf_close_cities, map)
except: pass
# add the corresponding close heris
try:
ff.add_close_heris_to_map(gdf_close_heris, map)
except: pass
# add route to the basemap
ff.add_route_to_map(gdf_best_route, map)
# add the start marker
ff.add_starters_to_map(gdf_best_route, map)
# add the layer control for toggling the layers
map.add_child(folium.LayerControl())
head = """
<h1 style="font-family: Verdana, Geneva, Tahoma, sans-serif;color:rgb(0, 0, 0);float: center;text-align:center;font-size:30px;">
Here's your optomized route: </h1>
"""
return head + map._repr_html_()
if __name__ == "__main__":
Timer(1, webbrowser.open('http://127.0.0.1:5000/')).start()
app.run(debug=True, use_reloader=False)