Yunus Serhat Bıçakçı commited on
Commit
4bab8a7
·
1 Parent(s): 6d3814a
Files changed (1) hide show
  1. pages/4_Test.py +137 -35
pages/4_Test.py CHANGED
@@ -1,39 +1,141 @@
1
- import folium
2
- import matplotlib.pyplot as plt
3
- import pandas as pd
4
-
5
- boroughs_count_2022_dec_df = "https://raw.githubusercontent.com/yunusserhat/data/main/data/boroughs_count_df_2022_dec.geojson"
6
- data_dict = boroughs_count_2022_dec_df.set_index("geo.name")["count"].to_dict()
7
-
8
- # Initialize the map centered around London
9
- m = folium.Map(location=[51.5074, -0.1278], zoom_start=10)
10
-
11
- boroughs_count_2022_dec_df = "https://raw.githubusercontent.com/yunusserhat/data/main/data/boroughs_count_df_2022_dec.geojson"
12
-
13
- # Choropleth map for Hate Tweets by Borough
14
- folium.Choropleth(
15
- geo_data=boroughs_count_2022_dec_df,
16
- data=data_dict,
17
- columns=["geo.name", "count"],
18
- key_on="feature.properties.geo.name",
19
- fill_color="YlOrRd",
20
- fill_opacity=0.7,
21
- line_opacity=0.2,
22
- legend_name="Number of Hate Tweets by Borough",
23
- name="Hate Tweets",
24
- highlight=True
25
- ).add_to(m)
26
-
27
- # Adding popups to each borough
28
- for idx, row in boroughs_count_2022_dec_df.iterrows():
29
- popup_content = f"<strong>{row['geo.name']}</strong><br>Number of Hate Tweets: {row['count']}"
30
- folium.Popup(popup_content).add_to(
31
- folium.GeoJson(row['geometry']).add_to(m)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  )
33
 
34
- # Adding a layer control
35
- folium.LayerControl().add_to(m)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Displaying the map
38
- m.to_streamlit()
39
 
 
 
 
1
+ import streamlit as st
2
+ import leafmap.foliumap as leafmap
3
+ import leafmap.colormaps as cm
4
+
5
+ st.set_page_config(layout="wide")
6
+
7
+ st.sidebar.info(
8
+ """
9
+ - Web App URL: <https://interactive-crime-map.hf.space/>
10
+ - HuggingFace repository: <https://huggingface.co/spaces/interactive-crime/map/tree/main>
11
+ """
12
+ )
13
+
14
+ import json
15
+
16
+ # Adding a widget to allow users to upload GeoJSON files
17
+ uploaded_file = st.file_uploader("Upload a GeoJSON file", type=["geojson"])
18
+
19
+ # Variable to hold the uploaded GeoJSON data
20
+ uploaded_geojson = None
21
+
22
+ if uploaded_file:
23
+ try:
24
+ # Read the uploaded file and parse as JSON (GeoJSON is a subset of JSON)
25
+ uploaded_geojson = json.load(uploaded_file)
26
+
27
+ # Basic validation (checking if it's a valid GeoJSON format)
28
+ if "features" not in uploaded_geojson:
29
+ st.warning("The uploaded file does not seem to be a valid GeoJSON format.")
30
+ uploaded_geojson = None
31
+ else:
32
+ st.success("GeoJSON file uploaded successfully!")
33
+
34
+ except json.JSONDecodeError:
35
+ st.error("Failed to decode the uploaded file. Please ensure it's a valid GeoJSON format.")
36
+
37
+ # Assuming there are two maps in the original app.
38
+ # Let's create a dropdown for each map to select which data to display.
39
+
40
+ map_choices = ["Original Map 1", "Original Map 2"]
41
+ if uploaded_geojson:
42
+ map_choices.append("Uploaded GeoJSON")
43
+
44
+ selected_map_1 = st.selectbox("Select data for Map 1", map_choices)
45
+ selected_map_2 = st.selectbox("Select data for Map 2", map_choices)
46
+
47
+ if selected_map_1 == "Uploaded GeoJSON":
48
+ # Display the uploaded GeoJSON data on Map 1
49
+ # You'll need to integrate this with the leafmap.foliumap methods you use
50
+ st.write("Display uploaded GeoJSON on Map 1")
51
+
52
+ elif selected_map_1 == "Original Map 1":
53
+ # Display the original Map 1
54
+ # This should be the code from your original app that displays the first map
55
+ st.write("Display original Map 1")
56
+
57
+ # ... Similarly for Map 2 and any other maps you have
58
+
59
+ # Rest of the application logic follows...
60
+
61
+
62
+ st.sidebar.title("Contact")
63
+ st.sidebar.info(
64
+ """
65
+ Yunus Serhat Bıçakçı at [yunusserhat.com](https://yunusserhat.com) | [GitHub](https://github.com/yunusserhat) | [Twitter](https://twitter.com/yunusserhat) | [LinkedIn](https://www.linkedin.com/in/yunusserhat)
66
+ """
67
+ )
68
+
69
+ st.title("Comparision of Hate Tweets, Hate Crime Rates and Total Crime Rates")
70
+ st.markdown(
71
+ """
72
+ These interactive maps illustrate a comparison of overall borough-level rates based on Twitter and London Metropolitan Police Service (MPS) data as of December 2022.
73
+
74
+ In the first map shows the representation of hate tweets according to Twitter data, while the second and third maps shows the representation of rates of hate and all crimes according to MPS data.
75
+ """
76
+ )
77
+
78
+ row1_col1, row1_col2, row1_col3 = st.columns([1, 1, 1])
79
+
80
+ # Twitter Hate Tweets Map
81
+ with row1_col1:
82
+ twitter = "https://raw.githubusercontent.com/yunusserhat/data/main/data/boroughs_count_df_2022_dec.geojson"
83
+ m1 = leafmap.Map(center=[51.50, -0.1], zoom=10)
84
+ m1.add_data(
85
+ twitter,
86
+ column="count",
87
+ scheme='Quantiles',
88
+ cmap='YlOrRd',
89
+ legend_title='Total Hate Tweet Number'
90
+ )
91
+
92
+ # MPS Hate Crimes Map
93
+ with row1_col2:
94
+ mps_hate = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps_hate_2022_dec_count.geojson"
95
+ m2 = leafmap.Map(center=[51.50, -0.1], zoom=10)
96
+ m2.add_data(
97
+ mps_hate,
98
+ column="Hate_Crime_Number",
99
+ scheme='Quantiles',
100
+ cmap='YlOrRd',
101
+ legend_title='Hate Crime Number'
102
  )
103
 
104
+ # MPS Total Crimes Map
105
+ with row1_col3:
106
+ mps_total = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps2022dec_count.geojson"
107
+ m3 = leafmap.Map(center=[51.50, -0.1], zoom=10)
108
+ m3.add_data(
109
+ mps_total,
110
+ column="Crime_Number",
111
+ scheme='Quantiles',
112
+ cmap='YlOrRd',
113
+ legend_title='Total Crime Number'
114
+ )
115
+
116
+ row2_col1, row2_col2, row2_col3 = st.columns([1, 1, 1])
117
+
118
+ # Setting the zoom and center for each map
119
+ longitude = -0.1
120
+ latitude = 51.50
121
+ zoomlevel = st.number_input("Zoom", 0, 20, 10)
122
+
123
+ with row2_col1:
124
+ m1.set_center(longitude, latitude, zoomlevel)
125
+
126
+ with row2_col2:
127
+ m2.set_center(longitude, latitude, zoomlevel)
128
+
129
+ with row2_col3:
130
+ m3.set_center(longitude, latitude, zoomlevel)
131
+
132
+ row3_col1, row3_col2, row3_col3 = st.columns([1, 1, 1])
133
+
134
+ with row3_col1:
135
+ m1.to_streamlit()
136
 
137
+ with row3_col2:
138
+ m2.to_streamlit()
139
 
140
+ with row3_col3:
141
+ m3.to_streamlit()