Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add GeoJsonTooltip to folium.Choropleth class in folium?

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
      }
like image 239
prime90 Avatar asked Mar 10 '19 14:03

prime90


People also ask

How do you make a Choropleth map in Folium?

To create a choropleth map using folium, we need to first initiate a base map by using folium. Map() and then add layers to it. We can pass the starting coordinates to the map by using the location parameter. The starting coordinates we choose here (40,-96) approximately represent the center of the U.S. map.

What is Geo_data parameter in Folium Choropleth map?

geo_data (string/object) — URL, file path, or data (json, dict, geopandas, etc) to your GeoJSON geometries. data (Pandas DataFrame or Series, default None) — Data to bind to the GeoJSON. columns (dict or tuple, default None) — If the data is a Pandas DataFrame, the columns of data to be bound.

What is Key_on in Folium?

geometries” (Folium documentation). No matter how we load the file we must convert the geometry data to function properly with this method. The key_on parameter of this method binds the data for each specific location (GeoJSON data) with the data for that location (i.e. population).

What other colors are available in the Folium library?

You can use: ['red', 'blue', 'green', 'purple', 'orange', 'darkred', 'lightred', 'beige', 'darkblue', 'darkgreen', 'cadetblue', 'darkpurple', 'white', 'pink', 'lightblue', 'lightgreen', 'gray', 'black', 'lightgray'] icon_color (str, default 'white') – The color of the drawing on the marker.


1 Answers

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)
like image 141
Conengmo Avatar answered Oct 15 '22 11:10

Conengmo