Which are the main metrics for the project by using Geopandas, Folium , Matplotlib¶
Here more info about Acquamat, a crowdsourced map of drinking water spots scattered throughout all cities around Europe,based in Naples, with the aim of encouraging the use of a public water, reducing the purchase of water plastic bottles.
- Here the imports to script
In [1]:
import geopandas
import folium
import matplotlib.pyplot as plt
- Function To show the values on the bar
In [2]:
def showValue(ax):
for p in ax.patches:
b = p.get_bbox()
ax.annotate(int(b.y1), (((b.x0 + b.x1)/2)-0.1 , b.y1+0.1 ))
- Data repository
In [3]:
quartieri_na = geopandas.read_file("https://maps.nicoladeinnocentis.it/quartieri-na-fontanelle/src/fontanelle_x_quartiere.json")
fontanelle = geopandas.read_file("https://raw.githubusercontent.com/deinic/fontanelle/main/fontanelle.json")
municipalita = geopandas.read_file('https://raw.githubusercontent.com/deinic/fontanelle/main/municipalita.geojson')
- SPATIAL JOIN data
In [4]:
fontanelle_in_municipalita = fontanelle.sjoin(municipalita,how="inner",predicate="intersects")
pop_area_quartieri=quartieri_na.sjoin(municipalita,how="inner",predicate="intersects")
- Pie Chart in % of total number of Drinking spot water per district
In [5]:
numero_totale_fontanelle_in_municipalita= fontanelle_in_municipalita.dissolve(by='municip' , aggfunc='count')
numero_totale_fontanelle_in_municipalita.plot(kind="pie",y='node',legend=False,autopct='%1.1f%%',title='Numero Totale di Fontanelle per Municipalità in %')
ntfm=numero_totale_fontanelle_in_municipalita.plot(kind="bar",y='node',legend=False,title='Numero Totale di Fontanelle per Municipalità')
showValue(ntfm)
- Number of drinking water spot per district IN SERVICE
In [6]:
#On the map
municipalita_ft_tot = municipalita.merge(numero_totale_fontanelle_in_municipalita['node'], on='municip')
municipalita_ft_tot_proj=municipalita_ft_tot.to_crs(epsg=32633)
municipalita_ft_tot_proj.plot(column="node",legend="True",scheme="equalinterval",cmap="Blues",edgecolor='black').set_axis_off()
for i in range(len(municipalita_ft_tot_proj)):
plt.text(municipalita_ft_tot_proj.geometry.centroid.x[i]-780,municipalita_ft_tot_proj.geometry.centroid.y[i]-500,"{}\n{}".format(municipalita_ft_tot_proj.municip[i],municipalita_ft_tot_proj.node[i]),size=6,multialignment='center')
#Numero di Fontanelle FUNZIONANTI per Municipalità Plot
fontanelle_funzionanti_in_municipalita=fontanelle_in_municipalita.loc[fontanelle_in_municipalita['status']=='1']
numero_fontanelle_funzionanti_in_municipalita = fontanelle_funzionanti_in_municipalita.dissolve(by='municip' , aggfunc='count',dropna=False)
nffm = numero_fontanelle_funzionanti_in_municipalita.plot(kind="bar",y='node',color='green',ylabel='Numero fontanelle Funzionanti',legend=False)
showValue(nffm)
- Number of drinking water spot per district OUT OF SERVICE
In [7]:
fontanelle_non_funzionanti_in_municipalita=fontanelle_in_municipalita.loc[fontanelle_in_municipalita['status']=='2']
numero_fontanelle_non_funzionanti_in_municipalita = fontanelle_non_funzionanti_in_municipalita.dissolve(by='municip' , aggfunc='count')
nfnfm = numero_fontanelle_non_funzionanti_in_municipalita.plot(kind="bar",y='node',color='red',ylabel='Numero fontanelle Non Funzionanti',legend=False)
showValue(nfnfm)
- Plot Number of inhabitants in the district per 1 drinking water spot
In [8]:
pop_area_quartieri_dissolve =pop_area_quartieri.dissolve(by='municip' , aggfunc='sum')
pop_area_quartieri_dissolve['pop_res'] = pop_area_quartieri_dissolve['popolazione_residente']/numero_fontanelle_funzionanti_in_municipalita['node']
fs = pop_area_quartieri_dissolve.plot(kind="bar", y='pop_res',xlabel='Municipalità' , ylabel = 'Abitanti', color='orange',title='Numero residenti nella municipalità per 1 fontanella funzionante',legend=False)
for p in fs.patches:
b = p.get_bbox()
fs.annotate(int(b.y1), (((b.x0 + b.x1)/2)-0.15 , b.y1+6000 ))
#Show plots
plt.show()
Choropleth Map by using Folium¶
We’re going to verify the total number of drinking water spots for each district in Naples by using choropleth map of Folium python library. I also add a popup layer in order to visualize data information.
In [9]:
m = folium.Map(location=[40.830891,14.257421], tiles="Stamen Terrain", zoom_start=12)
folium.Choropleth(
geo_data=municipalita_ft_tot,
name="Fontanelle",
data=municipalita_ft_tot,
columns=["municip","node"],
fill_color="YlGnBu",
key_on='feature.properties.municip',
fill_opacity=1,
line_opacity=0.2,
highlight=True,
show=False,
overlay=True,
legend_name="Drinking Water Spots",
nan_fill_color = "White"
).add_to(m)
popup_layer = folium.features.GeoJson(
data = municipalita_ft_tot,
style_function=lambda x:{'fillColor': '#ffffff',
'color':'#000000',
'fillOpacity': 0.1,
'weight': 0.1},
tooltip=folium.features.GeoJsonTooltip(
fields=["municip","node"],
aliases=["District","Drinking Water Spot Number"],
style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;")
)
)
m.add_child(popup_layer)
m.keep_in_front(popup_layer)
m
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook