Search code examples
pythonpython-3.xgeojsoncolormapfolium

Colormap issue in folium map


I am looking to create a heatmap of the USA based on data of COVID 19 cases. For that, I am using folium and geopandas. Here is the link for the data

and the code I used is given below.

colormap = cm.linear.YlGnBu_09.to_step(data=merged_data_cases['cases'], 
           method='quant', quantiles=[0,0.1,0.75,0.9,0.98,1]

usa_map = folium.Map(location=[48, -102], zoom_start=3, tiles=None)
folium.TileLayer('CartoDB positron',name="Light Map",control=False).add_to(usa_map)
colormap.caption = 'Confirmed Cases'


style_function = lambda x: {"weight":0.5, 
                        'color':'black',
                        'fillColor':colormap(x['properties']['cases']), 
                        'fillOpacity':0.75}
highlight_function = lambda x: {'fillColor': '#000000', 
                           'color':'#000000', 
                            'fillOpacity': 0.50, 
                            'weight': 0.1}
interactive = folium.features.GeoJson(
                merged_data_cases,
                style_function=style_function, 
                control=False,
                highlight_function=highlight_function, 
        tooltip=folium.features.GeoJsonTooltip(
                 fields=['Name','cases'],
                 aliases=['State: ','Confirmed Cases: '],
                 style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;"),
    sticky = True
   )
)
   colormap.add_to(usa_map)
   usa_map.add_child(interactive)
   usa_map

While I run the code, I get the following error.

AssertionError: The field Name is not available in the data. Choose from: ('NAME', 'STATE', 'cases').

I have gone through so many articles, blogs and documentation but still couldn't figure out my error. Can anybody point out what I am doing wrong here? I believe the error is due to style_function parameters.


Solution

  • It seems that you should write name in capital letters as NAME in fields=['Name','cases'] as the available fields in the error message are stated as NAME, STATE, cases