Spaces:
Sleeping
Sleeping
Add geo map
Browse files
app.py
CHANGED
@@ -1,7 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
|
|
|
|
4 |
|
|
|
5 |
def load_and_preprocess_data(file_path):
|
6 |
# Read the data
|
7 |
df = pd.read_csv(file_path)
|
@@ -99,41 +103,90 @@ def get_top_violations(df, age_group):
|
|
99 |
|
100 |
return violations_df.head()
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
def main():
|
103 |
st.title('Traffic Crash Analysis')
|
104 |
|
105 |
# Load data
|
106 |
df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv')
|
107 |
|
108 |
-
# Create
|
109 |
-
|
110 |
-
selected_age = st.selectbox('Select Age Group:', age_groups)
|
111 |
-
|
112 |
-
# Create and display chart
|
113 |
-
fig = create_severity_violation_chart(df, selected_age)
|
114 |
-
st.plotly_chart(fig, use_container_width=True)
|
115 |
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
if __name__ == "__main__":
|
139 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
+
import folium
|
5 |
+
from folium.plugins import HeatMap, MarkerCluster
|
6 |
+
from streamlit_folium import st_folium
|
7 |
|
8 |
+
@st.cache_data
|
9 |
def load_and_preprocess_data(file_path):
|
10 |
# Read the data
|
11 |
df = pd.read_csv(file_path)
|
|
|
103 |
|
104 |
return violations_df.head()
|
105 |
|
106 |
+
@st.cache_data
|
107 |
+
def create_map(df, selected_year):
|
108 |
+
filtered_df = df[df['Year'] == selected_year]
|
109 |
+
|
110 |
+
if len(filtered_df) > 1000:
|
111 |
+
filtered_df = filtered_df.sample(n=1000, random_state=42)
|
112 |
+
|
113 |
+
m = folium.Map(
|
114 |
+
location=[33.4255, -111.9400],
|
115 |
+
zoom_start=12,
|
116 |
+
control_scale=True,
|
117 |
+
tiles='CartoDB positron'
|
118 |
+
)
|
119 |
+
|
120 |
+
marker_cluster = MarkerCluster().add_to(m)
|
121 |
+
|
122 |
+
for _, row in filtered_df.iterrows():
|
123 |
+
folium.Marker(
|
124 |
+
location=[row['Latitude'], row['Longitude']],
|
125 |
+
popup=f"Accident at {row['Longitude']}, {row['Latitude']}<br>Date: {row['DateTime']}<br>Severity: {row['Injuryseverity']}",
|
126 |
+
icon=folium.Icon(color='red')
|
127 |
+
).add_to(marker_cluster)
|
128 |
+
|
129 |
+
heat_data = filtered_df[['Latitude', 'Longitude']].values.tolist()
|
130 |
+
HeatMap(heat_data, radius=15, max_zoom=13, min_opacity=0.3).add_to(m)
|
131 |
+
|
132 |
+
return m
|
133 |
+
|
134 |
def main():
|
135 |
st.title('Traffic Crash Analysis')
|
136 |
|
137 |
# Load data
|
138 |
df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv')
|
139 |
|
140 |
+
# Create tabs for different visualizations
|
141 |
+
tab1, tab2 = st.tabs(["Crash Statistics", "Crash Map"])
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
+
with tab1:
|
144 |
+
# Age group selection
|
145 |
+
age_groups = ['All Ages', '16-25', '26-35', '36-45', '46-55', '56-65', '65+']
|
146 |
+
selected_age = st.selectbox('Select Age Group:', age_groups)
|
147 |
+
|
148 |
+
# Create and display chart
|
149 |
+
fig = create_severity_violation_chart(df, selected_age)
|
150 |
+
st.plotly_chart(fig, use_container_width=True)
|
151 |
+
|
152 |
+
# Display statistics
|
153 |
+
if selected_age == 'All Ages':
|
154 |
+
total_incidents = len(df)
|
155 |
+
else:
|
156 |
+
total_incidents = len(df[
|
157 |
+
(df['Age_Group_Drv1'] == selected_age) |
|
158 |
+
(df['Age_Group_Drv2'] == selected_age)
|
159 |
+
])
|
160 |
+
|
161 |
+
# Create two columns for statistics
|
162 |
+
col1, col2 = st.columns(2)
|
163 |
+
|
164 |
+
with col1:
|
165 |
+
st.markdown(f"### Total Incidents")
|
166 |
+
st.markdown(f"**{total_incidents:,}** incidents for {selected_age}")
|
167 |
+
|
168 |
+
with col2:
|
169 |
+
st.markdown("### Top Violations")
|
170 |
+
top_violations = get_top_violations(df, selected_age)
|
171 |
+
st.table(top_violations)
|
172 |
+
|
173 |
+
with tab2:
|
174 |
+
# Year selection for map
|
175 |
+
years = sorted(df['Year'].unique())
|
176 |
+
selected_year = st.selectbox('Select Year:', years)
|
177 |
+
|
178 |
+
# Create and display map
|
179 |
+
st.markdown("### Crash Location Map")
|
180 |
+
map_placeholder = st.empty()
|
181 |
+
with map_placeholder:
|
182 |
+
m = create_map(df, selected_year)
|
183 |
+
map_data = st_folium(
|
184 |
+
m,
|
185 |
+
width=800,
|
186 |
+
height=600,
|
187 |
+
key=f"map_{selected_year}",
|
188 |
+
returned_objects=["null_drawing"]
|
189 |
+
)
|
190 |
|
191 |
if __name__ == "__main__":
|
192 |
main()
|