wanwanlin0521 commited on
Commit
cfee38a
·
verified ·
1 Parent(s): 1d6667b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +40 -20
src/streamlit_app.py CHANGED
@@ -263,27 +263,47 @@ years = sorted(df['year'].unique())
263
  year_dropdown = st.selectbox("Year: ", years)
264
  crime_dropdown = st.selectbox("Crime Type: ", top_10_crimes)
265
 
266
-
267
- # Create the map.
268
- def crime_map(year, crime):
269
- df_filtered = df[(df['year'] == year) & (df['crm_cd_desc'] == crime)].sample(n=300, random_state=1)
270
- gdf_points = gpd.GeoDataFrame(
271
- df_filtered,
272
- geometry=gpd.points_from_xy(df_filtered['lon'], df_filtered['lat']),
273
- crs="EPSG:4326"
274
- )
 
 
 
 
 
275
 
276
- fig, ax = plt.subplots(figsize=(10, 10))
277
- gdf_counties.plot(ax=ax, color='lightgray', edgecolor='white')
278
- gdf_points.plot(ax=ax, color='red', markersize=10, alpha=0.6)
279
- ax.set_title(f"{crime} - {year}")
280
- ax.set_xlabel("Longitude")
281
- ax.set_ylabel("Latitude")
282
- plt.grid(True)
283
- st.pyplot(fig)
284
-
285
- # Call the function with selected values
286
- crime_map(year_dropdown, crime_dropdown)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
 
288
  st.markdown("""
289
  This visualization shows the distribution of different years versus crime types in geospatial space by overlaying crime event points on a map of county boundaries. We chose a static map combined with GeoPandas and Matplotlib in order to clearly present county boundaries relative to crime events in space, suitable for reporting or analyzing output. The background of the map is coded with the shape of the county boundaries in gray, and the foreground is coded with red dots to indicate specific crime events, with the latitude and longitude position of each point coding its geographic coordinates.
 
263
  year_dropdown = st.selectbox("Year: ", years)
264
  crime_dropdown = st.selectbox("Crime Type: ", top_10_crimes)
265
 
266
+ # Create the new folium map to make the map more interactive.
267
+ new_map = folium.Map(location=[df_filtered['lat'].mean(), df_filtered['lon'].mean()], zoom_start=10)
268
+
269
+ # Add county boundary
270
+ folium.GeoJson(geojson_data, name="County Boundaries").add_to(new_map)
271
+
272
+ # # Create the map.
273
+ # def crime_map(year, crime):
274
+ # df_filtered = df[(df['year'] == year) & (df['crm_cd_desc'] == crime)].sample(n=300, random_state=1)
275
+ # gdf_points = gpd.GeoDataFrame(
276
+ # df_filtered,
277
+ # geometry=gpd.points_from_xy(df_filtered['lon'], df_filtered['lat']),
278
+ # crs="EPSG:4326"
279
+ # )
280
 
281
+ # fig, ax = plt.subplots(figsize=(10, 10))
282
+ # gdf_counties.plot(ax=ax, color='lightgray', edgecolor='white')
283
+ # gdf_points.plot(ax=ax, color='red', markersize=10, alpha=0.6)
284
+ # ax.set_title(f"{crime} - {year}")
285
+ # ax.set_xlabel("Longitude")
286
+ # ax.set_ylabel("Latitude")
287
+ # plt.grid(True)
288
+ # st.pyplot(fig)
289
+
290
+ # # Call the function with selected values
291
+ # crime_map(year_dropdown, crime_dropdown)
292
+
293
+ # Add crime points
294
+ # Method comes from: https://folium.streamlit.app/.
295
+ for _, row in df_filtered.iterrows():
296
+ folium.CircleMarker(
297
+ location=[row['lat'], row['lon']],
298
+ radius=3,
299
+ color='red',
300
+ fill=True,
301
+ fill_opacity=0.6,
302
+ popup=row['crm_cd_desc']
303
+ ).add_to(new_map)
304
+
305
+ # Display the new map.
306
+ st_folium(new_map, width=700, height=500)
307
 
308
  st.markdown("""
309
  This visualization shows the distribution of different years versus crime types in geospatial space by overlaying crime event points on a map of county boundaries. We chose a static map combined with GeoPandas and Matplotlib in order to clearly present county boundaries relative to crime events in space, suitable for reporting or analyzing output. The background of the map is coded with the shape of the county boundaries in gray, and the foreground is coded with red dots to indicate specific crime events, with the latitude and longitude position of each point coding its geographic coordinates.