Search code examples
pythonleafletfolium

Folium map has no attribute geo_json


I'm trying to implement the second script in this tutorial. My code is pretty much the same as in the tutoral and pasted below.

I get the following error on map1.geo_json(...):

AttributeError: 'Map' object has no attribute 'geo_json'

I have checked that I have the latest version of folium. How do I use this geo_json feature?

import folium

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

# definition of the boundaries in the map
district_geo = '/Users/alexiseggermont/Dropbox/01. Personal/04. Models/23. Immoweb/BEL_adm_shp/BEL_adm4.geojson'

# calculating total naumber of incidents per district
merged2 = data_for_map.set_index('NAME_4')#pd.DataFrame(merged['PdDistrict'].value_counts().astype(float))
merged2.to_json('/Users/alexiseggermont/Dropbox/01. Personal/04. Models/23. Immoweb/BEL_adm_shp/merged2.json')
merged2 = merged2.reset_index()
merged2.columns = ['NAME_4', 'prix_par_mc']

# creation of the choropleth
map1 = folium.Map(location=SF_COORDINATES, zoom_start=12)
map1.geo_json(geo_path = district_geo, 
              data_out = '/Users/alexiseggermont/Dropbox/01. Personal/04. Models/23. Immoweb/BEL_adm_shp/merged2.json', 
              data = merged2,
              columns = ['NAME_4', 'prix_par_mc'],
              key_on = 'feature.properties.NAME_4',
              fill_color = 'YlOrRd', 
              fill_opacity = 0.7, 
              line_opacity = 0.2,
              legend_name = 'price per square meter')

display(map1)

Solution

  • if you get an AttributeError, i always recommend reading the docs in case the method has been moved.

    in your case, you're looking for folium.GeoJson.