3
3
from osmnx import distance , features_from_bbox , geocoder , graph_from_bbox , routing
4
4
from streamlit_folium import st_folium
5
5
6
- bounding_box = {'min_lat' : 49.174522 , 'max_lat' : 50.526524 , 'min_lon' : 19.076385 , 'max_lon' : 21.432953 }
6
+ bbox = {'min_lat' : 49.880478 , 'max_lat' : 50.373496 ,
7
+ 'min_lon' : 20.478516 , 'max_lon' : 21.242065 }
7
8
8
9
9
10
@st .cache_resource
10
11
def get_graph ():
11
- return graph_from_bbox (
12
- bbox = (bounding_box ['max_lat' ], bounding_box ['min_lat' ], bounding_box ['min_lon' ], bounding_box ['max_lon' ]),
12
+ graph = graph_from_bbox (
13
+ bbox = (bbox ['max_lat' ], bbox ['min_lat' ], bbox ['min_lon' ], bbox ['max_lon' ]),
13
14
network_type = 'bike' ,
14
15
simplify = False ,
15
16
retain_all = True ,
16
17
)
18
+ print ('Constructed graph' )
19
+ return graph
20
+
17
21
18
22
19
23
@st .cache_data
@@ -26,21 +30,34 @@ def get_route_points(start, end):
26
30
# print(o, d)
27
31
route = routing .shortest_path (G , o , d )
28
32
points = [G .nodes [x ] for x in route ]
33
+ print ("Computed route" )
29
34
return [(x ['y' ], x ['x' ]) for x in points ]
30
35
31
36
32
- def plot_route (map_ , route_pts ):
33
- route = folium .PolyLine (locations = route_pts , weight = 2 )
34
- map_ .add_child (route )
35
- return map
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_
36
53
37
54
38
55
@st .cache_data
39
56
def get_localities ():
40
57
print ('Fetching localities' )
41
58
x = tuple (
42
59
features_from_bbox (
43
- bbox = (bounding_box ['max_lat' ], bounding_box ['min_lat' ], bounding_box ['min_lon' ], bounding_box ['max_lon' ]),
60
+ bbox = (bbox ['max_lat' ], bbox ['min_lat' ], bbox ['min_lon' ], bbox ['max_lon' ]),
44
61
tags = {'place' : ['town' , 'vilage' ]},
45
62
)['name' ].values
46
63
)
@@ -66,26 +83,11 @@ def run_server():
66
83
67
84
cont2 = st .container ()
68
85
69
- # center on Liberty Bell, add marker
70
- map_ = folium .Map (
71
- max_bounds = True ,
72
- location = [0.5 * (49.174522 + 50.526524 ), 0.5 * (19.944544 + 21.432953 )],
73
- zoom_start = 9 ,
74
- tiles = 'OpenStreetMap' ,
75
- min_lon = 19.076385 ,
76
- max_lon = 21.432953 ,
77
- min_lat = 49.174522 ,
78
- max_lat = 50.526524 ,
79
- )
80
- marker = folium .Marker ([50.049683 , 19.944544 ], popup = 'Kraków' , tooltip = 'Kraków' )
81
- map_ .add_child (marker )
82
-
83
- route_pts = get_route_points (origin , destination )
84
-
85
- plot_route (map_ , route_pts )
86
-
87
86
with cont2 :
88
- # call to render Folium map in Streamlit
87
+ map_ = get_map ()
88
+ route_pts = get_route_points (origin , destination )
89
+ route = folium .PolyLine (locations = route_pts , weight = 2 )
90
+ map_ .add_child (route )
89
91
_st_data = st_folium (map_ , width = 725 )
90
92
91
93
0 commit comments