Search code examples
pythonjsongeospatialgeopandasfolium

How to generate Folium map using GeoDataFrame?


I have created a geoDataFrame using, and would like to create a Folium Map, plotting the population eat for each country. Do I have to create the Json file, or I can directly use the geoDataFrame file?

import folium
import fiona
import geopandas as gpd

world = fiona.open(gpd.datasets.get_path('naturalearth_lowres'))
world = gpd.GeoDataFrame.from_features([feature for feature in world])
world = world[(world.pop_est > 0) & (world.name != "Antarctica")]

I used folium.map and geojson function, but it failed to create correct JSON files. Thanks for the help!


Solution

  • In recent releases of folium, you don't need to convert the GeoDataFrame to geojson, but you can pass it directly. Connecting the population column to color the polygons is still somewhat tricky to get correct:

    m = folium.Map()
    m.choropleth(world, data=world, key_on='feature.properties.name',
                 columns=['name', 'pop_est'], fill_color='YlOrBr')
    m