import geopandas
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
"Crime in Devon by Type")
st.title(
#########################################################
# Read the geopandas file and create a matplotlib figure
#########################################################
= geopandas.read_file("https://files.catbox.moe/4o0go1.gpkg")
lsoa_2011_crime_figures_df
= plt.subplots()
fig, ax
lsoa_2011_crime_figures_df.plot(="sw_5forces_street_by_lsoa_Other crime",
column=True,
legend=ax
ax
)
st.pyplot(fig)
= 'other_crime_devon.png'
filename
plt.savefig(filename)
with open(filename, "rb") as img:
= st.download_button(
btn ="Download Map",
label=img,
data=filename,
file_name="image/png"
mime )
20 Downloading Static Maps
20.1 Maps
While there are various other libraries available for displaying maps in Python and Streamlit, we are going to focus on the two we used in module 3 of the HSMA course
- matplotlib (which creates static maps)
- folium (which creates a leaflet.js interactive map)
For more of a reminder on how to work with geographic data and create maps, you can refer to the HSMA geographic modelling and visualisation book.
20.2 Downloading Static Maps Made With Matplotlib
Let’s just start by plotting a map.
20.2.0.1 A map with subplots
Static maps with subplots are much the same - you are just interested in saving the fig
object to a temporary .png
image file before then serving that temporary file to the user.
import geopandas
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
"Crime in Devon by Type")
st.title(
#########################################################
# Read the geopandas file and create a matplotlib figure
#########################################################
= geopandas.read_file("https://files.catbox.moe/4o0go1.gpkg")
lsoa_2011_crime_figures_df
= 290000,295000
xmin, xmax = 91000,95000
ymin, ymax = lsoa_2011_crime_figures_df.cx[xmin:xmax, ymin:ymax]
lsoa_2011_crime_figures_df_exeter
= lsoa_2011_crime_figures_df_exeter.rename(columns=
lsoa_2011_crime_figures_df_exeter
{'sw_5forces_street_by_lsoa_Anti-social behaviour': 'Anti-social behaviour',
'sw_5forces_street_by_lsoa_Bicycle theft': 'Bicycle theft',
'sw_5forces_street_by_lsoa_Burglary': 'Burglary',
'sw_5forces_street_by_lsoa_Criminal damage and arson': 'Criminal damage and arson',
'sw_5forces_street_by_lsoa_Drugs': 'Drugs',
'sw_5forces_street_by_lsoa_Total number crimes': 'Total number crimes'
}
)
= ['Anti-social behaviour', 'Bicycle theft', 'Burglary', 'Criminal damage and arson',
cols 'Drugs', 'Total number crimes']
= plt.subplots(2, 3, figsize=(20, 15))
fig, axs
for i, ax in enumerate(fig.axes):
="RdYlGn_r", legend=True, ax=ax)
lsoa_2011_crime_figures_df_exeter.plot(cols[i], cmap
'off')
ax.axis(
ax.title.set_text(cols[i])
#########################################################
# Display the figure
#########################################################
st.pyplot(fig)
#########################################################
# Create a download button
#########################################################
= 'crime_devon.png'
filename
plt.savefig(filename)
with open(filename, "rb") as img:
= st.download_button(
btn ="Download Map",
label=img,
data=filename,
file_name="image/png"
mime )
20.2.0.2 Multiple Separate Maps
What if we want to create several different maps instead of using the subplot feature?
We could do this and then provide a download button for each.
import geopandas
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
"Crime in Devon by Type")
st.title(
#########################################################
# Read the geopandas file and create a matplotlib figure
#########################################################
= geopandas.read_file("https://files.catbox.moe/4o0go1.gpkg")
lsoa_2011_crime_figures_df
for col in ['sw_5forces_street_by_lsoa_Anti-social behaviour',
'sw_5forces_street_by_lsoa_Bicycle theft',
'sw_5forces_street_by_lsoa_Burglary']:
= lsoa_2011_crime_figures_df.plot(column=col, legend=True)
fig
st.pyplot(fig)
= f'{col}.png'
filename
plt.savefig(filename)
with open(filename, "rb") as img:
= st.download_button(
btn ="Download Map",
label=img,
data=filename,
file_name="image/png"
mime )