map / pages /4_Test.py
Yunus Serhat Bıçakçı
update
ba74ea7
raw
history blame
3.78 kB
import streamlit as st
import leafmap.foliumap as leafmap
import leafmap.colormaps as cm
import json
st.set_page_config(layout="wide")
st.sidebar.info(
'''
- Web App URL: <https://interactive-crime-map.hf.space/>
- HuggingFace repository: <https://huggingface.co/spaces/interactive-crime/map/tree/main>
'''
)
st.sidebar.title("Contact")
st.sidebar.info(
'''
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)
'''
)
st.title("Comparision of Hate Tweets, Hate Crime Rates and Total Crime Rates")
st.markdown(
'''
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.
In the first map shows the representation of hate tweets according to Twitter data, while the second and third maps show the representation of rates of hate and all crimes according to MPS data.
'''
)
uploaded_file = st.file_uploader("Upload a GeoJSON file", type=["geojson"])
uploaded_geojson = None
map_1 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/boroughs_count_df_2022_dec.geojson"
map_2 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps_hate_2022_dec_count.geojson"
map_3 = "https://raw.githubusercontent.com/yunusserhat/data/main/data/mps2022dec_count.geojson"
if uploaded_file:
try:
uploaded_geojson = json.load(uploaded_file)
if "features" not in uploaded_geojson:
st.warning("The uploaded file does not seem to be a valid GeoJSON format.")
uploaded_geojson = None
else:
st.success("GeoJSON file uploaded successfully!")
except json.JSONDecodeError:
st.error("Failed to decode the uploaded file. Please ensure it's a valid GeoJSON format.")
map_choices = ["Original Map 1", "Original Map 2", "Original Map 3"]
if uploaded_geojson:
map_choices.append("Uploaded GeoJSON")
selected_map_1 = st.selectbox("Select data for Map 1", map_choices)
selected_map_2 = st.selectbox("Select data for Map 2", map_choices)
available_columns = ['geo.name', 'count', 'geometry']
selected_column_1 = st.selectbox("Select column for Map 1 visualization", available_columns, index=1)
selected_column_2 = st.selectbox("Select column for Map 2 visualization", available_columns, index=1)
row1_col1, row1_col2 = st.columns([1, 1])
with row1_col1:
m1 = leafmap.Map(center=[51.50, -0.1], zoom=10)
if selected_map_1 == "Uploaded GeoJSON":
m1.add_data(uploaded_geojson, column=selected_column_1)
elif selected_map_1 == "Original Map 1":
m1.add_data(map_1, column=selected_column_1)
elif selected_map_1 == "Original Map 2":
m1.add_data(map_2, column=selected_column_1)
else:
m1.add_data(map_3, column=selected_column_1)
with row1_col2:
m2 = leafmap.Map(center=[51.50, -0.1], zoom=10)
if selected_map_2 == "Uploaded GeoJSON":
m2.add_data(uploaded_geojson, column=selected_column_2)
elif selected_map_2 == "Original Map 1":
m2.add_data(map_1, column=selected_column_2)
elif selected_map_2 == "Original Map 2":
m2.add_data(map_2, column=selected_column_2)
else:
m2.add_data(map_3, column=selected_column_2)
longitude = -0.1
latitude = 51.50
zoomlevel = st.number_input("Zoom", 0, 20, 10)
row2_col1, row2_col2 = st.columns([1, 1])
with row2_col1:
m1.set_center(longitude, latitude, zoomlevel)
with row2_col2:
m2.set_center(longitude, latitude, zoomlevel)
row3_col1, row3_col2 = st.columns([1, 1])
with row3_col1:
m1.to_streamlit()
with row3_col2:
m2.to_streamlit()