如何将GeoJsonTooltip添加到folium.Choropleth类中? [英] How do you add GeoJsonTooltip to folium.Choropleth class in folium?

查看:279
本文介绍了如何将GeoJsonTooltip添加到folium.Choropleth类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Choropleth层,我想在其中添加GeoJsonTooltip,但我一直收到错误TypeError: __init__() missing 1 required positional argument: 'text'

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'

我当前的代码如下.

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.')

我要寻找的最终结果是,当用户将鼠标悬停在PA的每个县上时,将弹出工具提示弹出窗口,并显示来自geoJSON的以下信息.

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.

geojson示例

  "properties": {
        "county_name": "ADAMS",
        "incident": "Theft",
        "arrests": 24,
        "incident_count": 51,
        "incident_total": 75,
        "population": 102336,
        "rate": 73.2879924953096
      }

推荐答案

两件事:

  • 您需要使用GeoJsonTooltip类来使用geojson字段.常规Tooltip类仅适用于简单文本.那就是你得到的错误.
  • 您可以将GeoJsonTooltip传递给Choropleth在幕后创建的GeoJson对象:GeoJsonTooltip(....).add_to(theft.geojson)
  • 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)

这篇关于如何将GeoJsonTooltip添加到folium.Choropleth类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆