Using folium in jupyter notebooks, I am trying to draw a choropleth which displays info when I hover over the geographical boundary. I am unable to get the tool tip functionality to work
I tried plotting the map alone with no child layer of hover -> works just fine I tried adding hover but not tooltip -> works just fine when I add too tip I get the error " TypeError: 'NoneType' object is not subscriptable" I have tried converting colA into a string datatype but it does not make a difference
m = folium.Map(location=[-36.8485, 174.7633], zoom_start=10, tiles='CartoDB positron')
# Adjust the opacity of the base map tiles
m.get_root().html.add_child(folium.Element('<style>.leaflet-tile-pane{opacity: 0.6 !important;}</style>'))
merged_df['Statistical area 2 description'] = merged_df['Statistical area 2 description'].astype(str)
merged_df.crs = 'EPSG:2193'
style_function = lambda x: {'fillColor': '#ffffff',
'color':'#000000',
'fillOpacity': 0.1,
'weight': 0.1}
highlight_function = lambda x: {'fillColor': '#000000',
'color':'#000000',
'fillOpacity': 0.50,
'weight': 0.1}
hover = folium.features.GeoJson(
merged_df,
style_function=style_function,
control=False,
highlight_function=highlight_function,
tooltip=folium.features.GeoJsonTooltip(
fields=['Statistical area 2 description'],
aliases=['SA2 description'],
style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;"),
localize=True
)
)
m.add_child(hover)
m.keep_in_front(hover)
folium.Choropleth(
geo_data=merged_df,
name='choropleth',
data =merged_df,
columns = ['MESHBLOCK_2018', 'mean_merged_HbA1C'],
key_on="feature.properties.MESHBLOCK_2018",
nan_fill_color='grey',
nan_fill_opacity=0.4,
fill_color = 'YlGnBu', #YlOrBr,'OrRd', #colormap, #'OrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Average HbA1C",
).add_to(m)
folium.LayerControl().add_to(m)
m
I get the following error:
1094 def warn_for_geometry_collections(self): 1095 """Checks for GeoJson GeometryCollection features to warn user about incompatibility.""" 1096 geom_collections = [ 1097 feature.get("properties") if feature.get("properties") is not None else key 1098 for key, feature in enumerate(self._parent.data["features"]) 1099 if feature["geometry"]["type"] == "GeometryCollection" 1100 ] 1101 if any(geom_collections): 1102 warnings.warn( 1103 "{} is not configured to render for GeoJson GeometryCollection geometries. " 1104 "Please consider reworking these features: {} to MultiPolygon for full functionality.\n" (...) 1108 UserWarning, 1109 )
TypeError: 'NoneType' object is not subscriptable
merged_df is a dataframe with a geometry column showing multipolygon boundaries.
Thank you for your help