thatsean commited on
Commit
03f5955
·
verified ·
1 Parent(s): 3e0d47c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import folium
3
+ import h3
4
+ import pandas as pd
5
+ import numpy as np
6
+ from branca.colormap import LinearColormap
7
+
8
+ # Simulate data
9
+ def generate_sample_data():
10
+ sf_center = (37.7749, -122.4194)
11
+ hexagons = h3.k_ring(h3.geo_to_h3(sf_center[0], sf_center[1], 9), 10)
12
+
13
+ time_slots = [
14
+ "Early Morning", "Mid-Morning", "Mid-Day", "Mid-Afternoon",
15
+ "Evening", "Night", "Late-Night", "Witching Hour"
16
+ ]
17
+
18
+ data = []
19
+ for hex_id in hexagons:
20
+ for slot in time_slots:
21
+ visits = np.random.poisson(100) # Random number of visits
22
+ data.append({"hex_id": hex_id, "time_slot": slot, "visits": visits})
23
+
24
+ df = pd.DataFrame(data)
25
+
26
+ # Calculate Rush Index
27
+ avg_visits = df["visits"].mean()
28
+ df["rush_index"] = df["visits"] / avg_visits
29
+
30
+ return df
31
+
32
+ # Create map
33
+ def create_map(df, selected_time_slot):
34
+ m = folium.Map(location=[37.7749, -122.4194], zoom_start=12)
35
+
36
+ filtered_df = df[df["time_slot"] == selected_time_slot]
37
+
38
+ colormap = LinearColormap(colors=['green', 'yellow', 'red'], vmin=0, vmax=2)
39
+
40
+ for _, row in filtered_df.iterrows():
41
+ hex_boundary = h3.h3_to_geo_boundary(row["hex_id"])
42
+ color = colormap(row["rush_index"])
43
+ folium.Polygon(
44
+ locations=hex_boundary,
45
+ color=color,
46
+ fill=True,
47
+ fillColor=color,
48
+ fillOpacity=0.7,
49
+ tooltip=f"Rush Index: {row['rush_index']:.2f}"
50
+ ).add_to(m)
51
+
52
+ return m
53
+
54
+ # Gradio interface
55
+ def update_map(time_slot):
56
+ map_html = create_map(df, time_slot)._repr_html_()
57
+ return map_html
58
+
59
+ df = generate_sample_data()
60
+
61
+ time_slots = [
62
+ "Early Morning (5am - 8am)", "Mid-Morning (8am - 11am)", "Mid-Day (11am - 1pm)",
63
+ "Mid-Afternoon (1pm - 4pm)", "Evening (4pm - 7pm)", "Night (7pm - 10pm)",
64
+ "Late-Night (10pm - 1am)", "Witching Hour (1am - 4am)"
65
+ ]
66
+
67
+ iface = gr.Interface(
68
+ fn=update_map,
69
+ inputs=gr.Dropdown(choices=time_slots, label="Peak Hours"),
70
+ outputs=gr.HTML(),
71
+ title="RushRadar",
72
+ description="Visualize rush hours in San Francisco using H3 hexagons",
73
+ live=True
74
+ )
75
+
76
+ iface.launch()