My problem is that suburbs are not displaying the correct color on the Folium map. For example, Dandenong and Frankston should be shaded with the darkest color as they have the
I got it all figured out. Missing values are coloured grey, and the legend is customized with intervals of my choice. Cleaning up the geojson, removing trailing white space, and making all suburb names UPPERCASE solved a lot of problems.
Files are here
Create Dictionary
import pandas as pd
import csv
csv_path='Data_tables_Criminal_Incidents_Visualisation_year_ending_June_2018.csv'
df=pd.read_csv(csv_path)
# sum the number of incidents recorded for each suburb
df=df.groupby(['Suburb/Town Name'])['Incidents Recorded'].agg(
# make the numbers numeric otherwise it just concatenates strings
lambda x: pd.to_numeric(x, errors='coerce').sum()
)
# create a dictionary, where keys are Suburb/Town Name and values are number of incidents
suburb_dict = df.to_dict()
Style Function
def style_function(feature):
suburb = suburb_dict.get(feature['properties']['Suburb_Name'])
return {
'fillColor': '#gray' if suburb is None else colormap(suburb),
'fillOpacity': 0.6,
#borders
'weight': 0.2,
}
Folium Map
import folium
world_map = folium.Map(
location=[-38.292102, 144.727880],
zoom_start=6,
tiles='openstreetmap'
)
folium.GeoJson(
data = 'vic_for_crime_2018.geojson',
style_function = style_function
).add_to(world_map)
Colormap
import branca
colormap = branca.colormap.linear.YlOrRd_09.scale(0, 8500)
colormap = colormap.to_step(index=[0, 1000, 3000, 5000, 8500])
colormap.caption = 'Incidents of Crime in Victoria (year ending June 2018)'
colormap.add_to(world_map)
world_map.save('vic_final.html')