Tuesday, January 18, 2022

Geopandas Vs Folium - Generate Web map from data

 The code snippet below will demonstrate how to create an interactive choropleth web map using Geopandas and Folium libraries.


The latest version of Geopandas has the explore() method which can create a leafletjs map as seen above. 


import geopandas as gpd

# Read shp...
gdf = gpd.read_file(r"NGA_adm1.shp")

# Create web map obj...
mymap = gdf.explore(column='geographic')

# Save to file...
mymap.save('map.html')







import folium
import geopandas as gpd

zones = {'NEZ':1, 'SEZ':2, 'SSZ':3, 'SWZ':4, 'NCZ':5, 'NWZ':6}

# Read shp...
gdf = gpd.read_file(r"NGA_adm1.shp")

gdf.reset_index(level=0, inplace=True)
gdf['Weight'] = gdf['geographic'].map(zones)
gdf['index'] = gdf['index'].apply( lambda x: str(x) )

# Create folium map obj...
mymap = folium.Map(location=[8.67, 7.22], zoom_start=6)

folium.Choropleth(
    geo_data=geo_json_str, 
    data=gdf,
    name = 'Choropleth Map',
    columns = ['index','Weight', 'state_name'],
    key_on = 'feature.id',
    fill_color = 'YlGnBu', # RdYlGn
    legend_name = 'Name of Legend...',
    smooth_factor=  0
    
    ).add_to(mymap)

mymap










No comments:

Post a Comment