Yunus Serhat Bıçakçı commited on
Commit
46906f0
·
1 Parent(s): ba74ea7
Files changed (1) hide show
  1. pages/4_Test.py +46 -4
pages/4_Test.py CHANGED
@@ -1,10 +1,25 @@
1
  import streamlit as st
2
  import leafmap.foliumap as leafmap
3
- import leafmap.colormaps as cm
4
  import json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  st.set_page_config(layout="wide")
7
 
 
8
  st.sidebar.info(
9
  '''
10
  - Web App URL: <https://interactive-crime-map.hf.space/>
@@ -19,6 +34,7 @@ st.sidebar.info(
19
  '''
20
  )
21
 
 
22
  st.title("Comparision of Hate Tweets, Hate Crime Rates and Total Crime Rates")
23
  st.markdown(
24
  '''
@@ -28,9 +44,11 @@ st.markdown(
28
  '''
29
  )
30
 
 
31
  uploaded_file = st.file_uploader("Upload a GeoJSON file", type=["geojson"])
32
  uploaded_geojson = None
33
 
 
34
  map_1 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/boroughs_count_df_2022_dec.geojson"
35
  map_2 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps_hate_2022_dec_count.geojson"
36
  map_3 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps2022dec_count.geojson"
@@ -46,17 +64,40 @@ if uploaded_file:
46
  except json.JSONDecodeError:
47
  st.error("Failed to decode the uploaded file. Please ensure it's a valid GeoJSON format.")
48
 
 
49
  map_choices = ["Original Map 1", "Original Map 2", "Original Map 3"]
50
  if uploaded_geojson:
51
  map_choices.append("Uploaded GeoJSON")
52
 
53
  selected_map_1 = st.selectbox("Select data for Map 1", map_choices)
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  selected_map_2 = st.selectbox("Select data for Map 2", map_choices)
55
 
56
- available_columns = ['geo.name', 'count', 'geometry']
57
- selected_column_1 = st.selectbox("Select column for Map 1 visualization", available_columns, index=1)
58
- selected_column_2 = st.selectbox("Select column for Map 2 visualization", available_columns, index=1)
 
 
 
 
 
 
 
 
59
 
 
60
  row1_col1, row1_col2 = st.columns([1, 1])
61
 
62
  with row1_col1:
@@ -81,6 +122,7 @@ with row1_col2:
81
  else:
82
  m2.add_data(map_3, column=selected_column_2)
83
 
 
84
  longitude = -0.1
85
  latitude = 51.50
86
  zoomlevel = st.number_input("Zoom", 0, 20, 10)
 
1
  import streamlit as st
2
  import leafmap.foliumap as leafmap
 
3
  import json
4
+ import requests
5
+
6
+ # Functions to dynamically fetch columns
7
+ def get_columns_from_geojson(geojson_data):
8
+ """Retrieve column names from a GeoJSON data."""
9
+ if "features" in geojson_data:
10
+ properties = geojson_data["features"][0]["properties"]
11
+ return list(properties.keys())
12
+ return []
13
+
14
+ def get_columns_from_url(url):
15
+ """Retrieve column names from a GeoJSON URL."""
16
+ response = requests.get(url)
17
+ geojson_data = response.json()
18
+ return get_columns_from_geojson(geojson_data)
19
 
20
  st.set_page_config(layout="wide")
21
 
22
+ # Sidebar Information
23
  st.sidebar.info(
24
  '''
25
  - Web App URL: <https://interactive-crime-map.hf.space/>
 
34
  '''
35
  )
36
 
37
+ # Title and Description
38
  st.title("Comparision of Hate Tweets, Hate Crime Rates and Total Crime Rates")
39
  st.markdown(
40
  '''
 
44
  '''
45
  )
46
 
47
+ # File Uploader
48
  uploaded_file = st.file_uploader("Upload a GeoJSON file", type=["geojson"])
49
  uploaded_geojson = None
50
 
51
+ # Map URLs
52
  map_1 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/boroughs_count_df_2022_dec.geojson"
53
  map_2 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps_hate_2022_dec_count.geojson"
54
  map_3 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps2022dec_count.geojson"
 
64
  except json.JSONDecodeError:
65
  st.error("Failed to decode the uploaded file. Please ensure it's a valid GeoJSON format.")
66
 
67
+ # Map Selection
68
  map_choices = ["Original Map 1", "Original Map 2", "Original Map 3"]
69
  if uploaded_geojson:
70
  map_choices.append("Uploaded GeoJSON")
71
 
72
  selected_map_1 = st.selectbox("Select data for Map 1", map_choices)
73
+
74
+ # Determine the columns based on the selected dataset for Map 1
75
+ if selected_map_1 == "Uploaded GeoJSON" and uploaded_geojson:
76
+ available_columns_1 = get_columns_from_geojson(uploaded_geojson)
77
+ elif selected_map_1 == "Original Map 1":
78
+ available_columns_1 = get_columns_from_url(map_1)
79
+ elif selected_map_1 == "Original Map 2":
80
+ available_columns_1 = get_columns_from_url(map_2)
81
+ elif selected_map_1 == "Original Map 3":
82
+ available_columns_1 = get_columns_from_url(map_3)
83
+
84
+ selected_column_1 = st.selectbox("Select column for Map 1 visualization", available_columns_1)
85
+
86
  selected_map_2 = st.selectbox("Select data for Map 2", map_choices)
87
 
88
+ # Determine the columns based on the selected dataset for Map 2
89
+ if selected_map_2 == "Uploaded GeoJSON" and uploaded_geojson:
90
+ available_columns_2 = get_columns_from_geojson(uploaded_geojson)
91
+ elif selected_map_2 == "Original Map 1":
92
+ available_columns_2 = get_columns_from_url(map_1)
93
+ elif selected_map_2 == "Original Map 2":
94
+ available_columns_2 = get_columns_from_url(map_2)
95
+ elif selected_map_2 == "Original Map 3":
96
+ available_columns_2 = get_columns_from_url(map_3)
97
+
98
+ selected_column_2 = st.selectbox("Select column for Map 2 visualization", available_columns_2)
99
 
100
+ # Display Maps
101
  row1_col1, row1_col2 = st.columns([1, 1])
102
 
103
  with row1_col1:
 
122
  else:
123
  m2.add_data(map_3, column=selected_column_2)
124
 
125
+ # Zoom
126
  longitude = -0.1
127
  latitude = 51.50
128
  zoomlevel = st.number_input("Zoom", 0, 20, 10)