Folium Choropleth map marker is not working

2k views Asked by At

I'm trying to follow the blog post from Domino lab, Creating interactive crime maps with Folium. And I found that the code base is too old to run the Folium's Choropleth map marker. Although older version on Domino platform seems working (2015), the latest Ipython notebook doesn't work. So I'm guessing Folium changed something on markers? I tried to find the update but I can't find it. Are anyone familiar with this library? If so please give me advices.

My code below:

from IPython.display import HTML

def display(m, height=500):
    """Takes a folium instance and embed HTML."""
    m._build_map()
    srcdoc = m.HTML.replace('"', '"')
    embed = HTML('<iframe srcdoc="{0}" '
                 'style="width: 100%; height: {1}px; '
                 'border: none"></iframe>'.format(srcdoc, height))
    return embed

import folium  
import pandas as pd

SF_COORDINATES = (37.76, -122.45)  
crimedata = pd.read_csv('data/SFPD_Incidents_-_Current_Year__2015_.csv')

#for speed purposes
MAX_RECORDS = 1000

#create empty map zoomed in on San Francisco
map = folium.Map(location=SF_COORDINATES, zoom_start=12)

#add a marker for every record in the filtered data, use a clustered view
for each in crimedata[0:MAX_RECORDS].iterrows():  
    map.simple_marker(
        location = [each[1]['Y'],each[1]['X']], 
        clustered_marker = True)

display(map)  

#definition of the boundaries in the map
district_geo = r'data/sfpddistricts.json'

#calculating total number of incidents per district
crimedata2 = pd.DataFrame(crimedata['PdDistrict'].value_counts().astype(float))  
crimedata2.to_json('data/crimeagg.json')  
crimedata2 = crimedata2.reset_index()  
crimedata2.columns = ['District', 'Number']

#creation of the choropleth
map1 = folium.Map(location=SF_COORDINATES, zoom_start=12)  
map1.geo_json(geo_path = district_geo,  
              data_out = 'data/crimeagg.json', 
              data = crimedata2,
              columns = ['District', 'Number'],
              key_on = 'feature.properties.DISTRICT',
              fill_color = 'YlOrRd', 
              fill_opacity = 0.7, 
              line_opacity = 0.2,
              legend_name = 'Number of incidents per district')

display(map1)  
2

There are 2 answers

0
ndailey On

Not sure if you mean markers (popups) or the choropleth method itself isn't working?

The map1.geo_json() method is deprecated (see here).

Instead, try map1.choropleth(geo_path = district_geo,
data_out = 'data/crimeagg.json', data = crimedata2, columns = ['District', 'Number'], key_on = 'feature.properties.DISTRICT', fill_color = 'YlOrRd', fill_opacity = 0.7, line_opacity = 0.2, legend_name = 'Number of incidents per district')

The map.choropleth method worked for me, but don't know if they fixed the popup issue for choropleth maps. Hope this helps!

0
Colin Anthony On

The mapObject.choropleth method is being depricated.

folium.GeoJson is the suggested method as per this github issue: https://github.com/python-visualization/folium/issues/589

A comment in that issue links to this example, which shows how to build the choropleth: http://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/GeoJSON_and_choropleth.ipynb?flush_cache=true

TLDR

  • replace geo_json with GeoJson

  • and for the args like fill_color, use fillColor: <hex_color> in the style_function dictionary kwarg.