Skip to content

Commit 6469af7

Browse files
Improve server
1 parent 1668867 commit 6469af7

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

hackyeah2024/server.py

+60-28
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,60 @@ def get_graph():
1717
)
1818
print('Constructed graph')
1919
return graph
20-
20+
21+
22+
@st.cache_data
23+
def get_coords(locality):
24+
coords = geocoder.geocode(locality)
25+
print(f"Getting coords for {locality}")
26+
return coords
27+
28+
29+
@st.cache_data
30+
def get_marker(locality):
31+
coords = get_coords(locality)
32+
return folium.Marker([coords[1], coords[0]], popup=locality, tooltip=locality)
2133

2234

2335
@st.cache_data
2436
def get_route_points(start, end):
25-
G = get_graph()
26-
orig = geocoder.geocode(start)
27-
dest = geocoder.geocode(end)
28-
o, _ = distance.nearest_nodes(G, orig[1], orig[0], return_dist=True)
29-
d, _ = distance.nearest_nodes(G, dest[1], dest[0], return_dist=True)
30-
# print(o, d)
31-
route = routing.shortest_path(G, o, d)
32-
points = [G.nodes[x] for x in route]
37+
g = get_graph()
38+
39+
orig = get_coords(start)
40+
dest = get_coords(end)
41+
42+
o, _ = distance.nearest_nodes(g, orig[1], orig[0], return_dist=True)
43+
d, _ = distance.nearest_nodes(g, dest[1], dest[0], return_dist=True)
44+
45+
route = routing.shortest_path(g, o, d)
46+
points = [g.nodes[x] for x in route]
47+
3348
print("Computed route")
49+
3450
return [(x['y'], x['x']) for x in points]
3551

3652

37-
@st.cache_resource
38-
def get_map():
39-
map_ = folium.Map(
40-
max_bounds=True,
41-
location=[0.5 * (bbox['min_lat'] + bbox['max_lat']), 0.5 * (bbox['min_lon'] + bbox['max_lon'])],
42-
zoom_start=9,
43-
tiles='OpenStreetMap',
44-
min_lon=19.076385,
45-
max_lon=21.432953,
46-
min_lat=49.174522,
47-
max_lat=50.526524,
48-
)
49-
marker = folium.Marker([50.01381, 20.98698], popup='Tarnów', tooltip='Tarnów')
50-
map_.add_child(marker)
51-
print('Constructed map')
52-
return map_
53+
@st.cache_data
54+
def get_route(origin, destination):
55+
route_pts = get_route_points(origin, destination)
56+
print("Plotted route")
57+
return folium.PolyLine(locations=route_pts, weight=2)
58+
59+
60+
# @st.cache_resource
61+
# def get_map():
62+
# map_ = folium.Map(
63+
# max_bounds=True,
64+
# location=[0.5 * (bbox['min_lat'] + bbox['max_lat']), 0.5 * (bbox['min_lon'] + bbox['max_lon'])],
65+
# zoom_start=9,
66+
# tiles='OpenStreetMap',
67+
# min_lon=19.076385,
68+
# max_lon=21.432953,
69+
# min_lat=49.174522,
70+
# max_lat=50.526524,
71+
# )
72+
# print('Constructed map')
73+
# return map_
5374

5475

5576
@st.cache_data
@@ -84,10 +105,21 @@ def run_server():
84105
cont2 = st.container()
85106

86107
with cont2:
87-
map_ = get_map()
88-
route_pts = get_route_points(origin, destination)
89-
route = folium.PolyLine(locations=route_pts, weight=2)
108+
map_ = folium.Map(
109+
max_bounds=True,
110+
location=[0.5 * (bbox['min_lat'] + bbox['max_lat']), 0.5 * (bbox['min_lon'] + bbox['max_lon'])],
111+
zoom_start=9,
112+
tiles='OpenStreetMap',
113+
min_lon=19.076385,
114+
max_lon=21.432953,
115+
min_lat=49.174522,
116+
max_lat=50.526524)
117+
route = get_route(origin, destination)
90118
map_.add_child(route)
119+
origin_marker = get_marker(origin)
120+
map_.add_child(origin_marker)
121+
dest_marker = get_marker(destination)
122+
map_.add_child(dest_marker)
91123
_st_data = st_folium(map_, width=725)
92124

93125

0 commit comments

Comments
 (0)