8

I have two choropleth layers in which I would like to add GeoJsonTooltip to but I keep receiving error TypeError: __init__() missing 1 required positional argument: 'text'

My current code is as follows.

import folium
import pandas as pd
import json

df_theft = pd.read_csv('PA_Theft.csv')
df_assualt = pd.read_csv('PA_Assualt.csv')

theft_json = json.load(open('theft_geojson.json'))
assualt_json = json.load(open('assualt_geojson.json'))

m = folium.Map(location=[41.20, -77.50], tiles="cartodbdark_matter", zoom_start=8.3)

theft = folium.Choropleth(
    geo_data=theft_json,
    data=df_theft,               
    columns=['county_name', 'rate'],
    key_on='feature.properties.county_name',
    fill_color='OrRd',
    fill_opacity=0.9,
    nan_fill_color='#ffffff',
    nan_fill_opacity=0.9,
    legend_name='Incident rate per 100,000 people',
    highlight=True,
    name='Theft'
).add_to(m)

folium.GeoJson(
    theft_json,
    tooltip=folium.features.Tooltip(fields=['feature.properties.county_name'])
).add_to(theft)

assualt = folium.Choropleth(
    geo_data=assualt_json,
    data=df_assualt,               
    columns=['county_name', 'rate'],
    key_on='feature.properties.county_name',
    fill_color='OrRd',
    fill_opacity=0.9,
    nan_fill_color='#ffffff',
    nan_fill_opacity=0.9,
    legend_name='Incident rate per 100,000 people',
    highlight=True,
    name='Assualt'
).add_to(m)

folium.GeoJson(
    assualt_json,
    tooltip=folium.features.Tooltip(fields=['feature.properties.county_name'])
).add_to(assualt)


folium.LayerControl().add_to(m) 
m.save('Crime_Map.html')

print('Map created.')

The end result I'm looking for is that when the user hovers over each county in PA the tooltip popup is activated and the following information from the geoJSON is displayed.

Example geojson

  "properties": {
        "county_name": "ADAMS",
        "incident": "Theft",
        "arrests": 24,
        "incident_count": 51,
        "incident_total": 75,
        "population": 102336,
        "rate": 73.2879924953096
      }
prime90
  • 889
  • 2
  • 14
  • 26
  • 2
    Two things: * You need to use the `GeoJsonTooltip` class to use the geojson fields. The regular `Tooltip` class only works with simple text. That's the error you're getting. * You can pass the `GeoJsonTooltip` to the `GeoJson` object that is created by `Choropleth` under the hood: `Choropleth(...).geojson.add_child(GeoJsonTooltip(...))`. Or in your case: `GeoJsonTooltip(....).add_to(theft.geojson)` – Conengmo Mar 10 '19 at 16:33
  • @Conengmo I think I understand but I added `.geojson.add_child(GeoJsonTooltip(fields=['feature']['properties']['county_name'])).add_to(m)` after the Choropleth class and ended up receiving this error `NameError: name 'GeoJsonTooltip' is not defined` Does this mean the GeoJson object was not created under the hood? – prime90 Mar 10 '19 at 18:34
  • @Conengmo I was able to get it to work with your second option. Thank you. – prime90 Mar 10 '19 at 18:50
  • Good to hear! I'll add it as an answer then. – Conengmo Mar 11 '19 at 06:44

1 Answers1

11

Two things:

  • You need to use the GeoJsonTooltip class to use the geojson fields. The regular Tooltip class only works with simple text. That's the error you're getting.
  • You can pass the GeoJsonTooltip to the GeoJson object that is created by Choropleth under the hood: GeoJsonTooltip(....).add_to(theft.geojson)
Conengmo
  • 783
  • 7
  • 13