In [1]:
import json
from branca.colormap import linear
import folium
from folium import Map, Marker, GeoJson, LayerControl
import pandas as pd
import geopandas as gpd
%matplotlib inline
In [2]:
gj_path = "geojson/anc.geojson"
In [3]:
anc_df = gpd.read_file(gj_path)
In [4]:
anc_df.head()
Out[4]:
In [5]:
# Tried this to reduce the map complexity for Chrome, didn't work
# anc_df.geometry = anc_df.geometry.simplify(500, preserve_topology=False)
In [6]:
def embed_map(m):
from IPython.display import IFrame
m.save('inline_map.html')
return IFrame('inline_map.html', width='100%', height='750px')
In [7]:
election_df = pd.read_csv("../../cleaned_data/election_data_for_anc_map.csv")
election_df.head()
Out[7]:
In [8]:
election_df = election_df[election_df["ward"].notna()]
election_df["ward"] = election_df["ward"].map(round)
election_df["ANC_ID"] = election_df["ward"].map(str) + election_df["anc"]
election_df.drop(columns = ["ward", "anc"], inplace=True)
print(election_df.shape)
election_df.head()
Out[8]:
In [9]:
election_df_2018 = election_df[election_df.year == 2018]
print(election_df_2018.shape)
In [10]:
turnout = pd.read_csv("../../cleaned_data/anc_turnout.csv")
turnout = turnout[turnout.year == 2018]
turnout = turnout.rename(columns={"anc.full": "ANC_ID"})[["ANC_ID", "turnout"]]
print(turnout.shape)
turnout.head()
Out[10]:
In [11]:
joined_df = anc_df.merge(election_df_2018, how="outer", on="ANC_ID")
joined_df = joined_df.merge(turnout, how="outer", on="ANC_ID")
print(joined_df.shape)
joined_df.head()
Out[11]:
In [12]:
""" No longer necessary
for anc in gjdata['features']:
anc_id = anc['properties']['ANC_ID']
features = election_df.columns.tolist()
features.remove("ANC_ID")
for feature in features:
anc['properties'][feature] = joined_df.loc[joined_df['ANC_ID'] == anc_id, feature].item()
"""
Out[12]:
In [13]:
anc_map = Map(location = (38.8899, -77.0091),
zoom_start = 12,
tiles = 'Stamen Toner')
In [14]:
folium.Choropleth(
geo_data=gj_path,
data=joined_df,
columns=["ANC_ID", "votes"],
key_on='feature.properties.ANC_ID',
fill_color='GnBu',
fill_opacity=0.5,
line_weight=1,
highlight=True,
overlay=True,
name="average votes",
legend_name="average # votes for winning candidates",
).add_to(anc_map)
Out[14]:
In [15]:
folium.Choropleth(
geo_data=gj_path,
data=joined_df,
columns=["ANC_ID", "engagement"],
key_on='feature.properties.ANC_ID',
fill_color='PuRd',
fill_opacity=0.5,
line_weight=1,
highlight=True,
overlay=True,
name="engagement",
legend_name="percentage of ballots where ANC candidate was marked (complement of roll-off)",
).add_to(anc_map)
Out[15]:
In [16]:
folium.Choropleth(
geo_data=gj_path,
data=joined_df,
columns=["ANC_ID", "turnout"],
key_on='feature.properties.ANC_ID',
fill_color='YlOrBr',
fill_opacity=0.5,
line_weight=1,
highlight=True,
overlay=True,
name="turnout",
legend_name="estimated voter turnout",
).add_to(anc_map)
Out[16]:
In [17]:
LayerControl().add_to(anc_map)
Out[17]:
In [18]:
embed_map(anc_map)
Out[18]:
In [19]:
anc_map.save("anc_map.html")
In [ ]: