Search code examples
pythonpandasgeopandasfoliumchoropleth

Unable to draw choropleth data on folium map with Zip Code laid out


My Data is just a simple csv with a Zip Code column with an Uninsured Column to represent the percentage of uninsured population in that zip code. Also the geojson file for the zip codes is attached here

zip_uninsured.csv

Zipcodes_Arc.geojson

My Code is as follows:

import folium
import pandas as pd
import numpy as np
import os
import geopandas as gpd

geo_data_file=os.path.join('Zipcodes_Arc.geojson')

#load GeoJSON
with open(geo_data_file, 'r') as jsonFile:
    geo_df=gpd.read_file(jsonFile)
tmp=geo_df



df=pd.read_csv(os.path.join('zip_uninsured.csv'))



#remove ZIP codes not in geo data

tmp.info()
print(tmp.CODE)
geoJSON_zips=list(tmp.CODE.unique())
df_zips=list(df.zipcode)



missing_zips = np.setdiff1d(geoJSON_zips,df_zips)

tmp = tmp.rename(columns = {"CODE":"zipcode"})


my_map = folium.Map(location=[39.95228, -75.16245], zoom_start=9,
                   detect_retina=True, control_scale=False)

folium.Choropleth(
        geo_data=geo_df,
        name='choropleth',
        data=df,
        columns=[ 'Uninsured','zipcode'],
        key_on='zipcode',
        fill_color='OrRd',
        fill_opacity=0.2,
        line_opacity=0.8,
        line_color='Blue',
        legend_name='Uninsured'
    ).add_to(my_map)

The zip code outlines are correctly drawn on folium map in blue outline, but the choropleth values are not (colors not filled in the zip outlines.

Am especially new to geospatial side of python and Would be obliged for any help in resolving this issue.

(This issue doesn't seem to be the same as folium blank choropleth map on jupyter, possible JSON format issue)


Solution

  • The reason you could not draw the graph may be that the geometry of geopandas is a line segment. I got the zip code geojson file from here to deal with this. This file is in polygon format. Also, if you reference it as it is in goepandas, the key referenced in key_on will be 'feature.propeties.zipcode'. One important point is that the zip code to be associated with geojson is a string, so the user data must also be a string.

    import folium
    import pandas as pd
    import numpy as np
    import os
    import geopandas as gpd
    
    #load GeoJSON
    geo_data_file=os.path.join('./data/Zipcodes_Poly.geojson')
    
    with open(geo_data_file, 'r') as jsonFile:
        geo_df=gpd.read_file(jsonFile)
    
    geo_df = geo_df.rename(columns = {"CODE":"zipcode"})
    
    df = pd.read_csv(os.path.join('./data/zip_uninsured.csv'), index_col=0)
    df['zipcode'] = df['zipcode'].astype(str)
    
    my_map = folium.Map(location=[39.95228, -75.16245], zoom_start=11,
                       detect_retina=True, control_scale=False)
    
    folium.Choropleth(
            geo_data=geo_df,
            name='choropleth',
            data=df,
            columns=['zipcode','Uninsured'],
            key_on='feature.properties.zipcode',
            fill_color='OrRd',
            fill_opacity=0.5,
            line_opacity=0.8,
            line_color='Blue',
            legend_name='Uninsured'
        ).add_to(my_map)
    
    my_map
    

    enter image description here