@@ -17,39 +17,60 @@ def get_graph():
17
17
)
18
18
print ('Constructed graph' )
19
19
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 )
21
33
22
34
23
35
@st .cache_data
24
36
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
+
33
48
print ("Computed route" )
49
+
34
50
return [(x ['y' ], x ['x' ]) for x in points ]
35
51
36
52
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_
53
74
54
75
55
76
@st .cache_data
@@ -84,10 +105,21 @@ def run_server():
84
105
cont2 = st .container ()
85
106
86
107
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 )
90
118
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 )
91
123
_st_data = st_folium (map_ , width = 725 )
92
124
93
125
0 commit comments