【问题标题】:Text as tooltip, popup or labels in folium choropleth GeoJSON polygons在 folium choropleth GeoJSON 多边形中作为工具提示、弹出窗口或标签的文本
【发布时间】:2022-11-15 00:22:22
【问题描述】:

Folium 允许创建带有工具提示或弹出文本的标记。我想对我的 GeoJSON 多边形做同样的事情。

我的 GeoJSON 有一个名为 "name" 的属性(feature.properties.name -> 假设它是美国每个州的名称)。除了每个州的失业率之外,我还希望能够在我的 choropleth 地图中将其显示为标签。我在pandas dataframe"State" 列中也有相同的信息。

这可能吗?我会很高兴有一个解决方案允许它成为一个弹出窗口、工具提示或写在顶部的简单文本标签。

import pandas as pd


url = (
    "https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"
)
state_geo = f"{url}/us-states.json"
state_unemployment = f"{url}/US_Unemployment_Oct2012.csv"
state_data = pd.read_csv(state_unemployment)

m = folium.Map(location=[48, -102], zoom_start=3)

folium.Choropleth(
    geo_data=state_geo,
    name="choropleth",
    data=state_data,
    columns=["State", "Unemployment"],
    key_on="feature.id",
    fill_color="YlGn",
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name="Unemployment Rate (%)",
).add_to(m)

folium.LayerControl().add_to(m)

m

【问题讨论】:

    标签: python python-3.x leaflet folium choropleth


    【解决方案1】:

    在过去,我不得不使用 folium 的 GeoJsonTooltip() 和其他一些步骤来完成这项工作。我很想知道是否有人有更好的方法

    1. 捕获Choropleth函数的返回值
    2. 为 Chorpleth 的底层 geojson obj 添加一个值(例如失业)
    3. 使用步骤 2 中的值创建 GeoJsonTooltip
    4. 将该工具提示添加到 choropleth 的 geojson
      url = (
            "https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"
        )
        state_geo = f"{url}/us-states.json"
        state_unemployment = f"{url}/US_Unemployment_Oct2012.csv"
        state_data = pd.read_csv(state_unemployment)
        
        m = folium.Map(location=[48, -102], zoom_start=3)
        
        # capturing the return of folium.Choropleth()
        cp = folium.Choropleth(
            geo_data=state_geo,
            name="choropleth",
            data=state_data,
            columns=["State", "Unemployment"],
            key_on="feature.id",
            fill_color="YlGn",
            fill_opacity=0.7,
            line_opacity=0.2,
            legend_name="Unemployment Rate (%)",
        ).add_to(m)
        
        # creating a state indexed version of the dataframe so we can lookup values
        state_data_indexed = state_data.set_index('State')
        
        # looping thru the geojson object and adding a new property(unemployment)
        # and assigning a value from our dataframe
        for s in cp.geojson.data['features']:
            s['properties']['unemployment'] = state_data_indexed.loc[s['id'], 'Unemployment']
        
        # and finally adding a tooltip/hover to the choropleth's geojson
        folium.GeoJsonTooltip(['name', 'unemployment']).add_to(cp.geojson)
        
        folium.LayerControl().add_to(m)
        
        m
      

    【讨论】:

    【解决方案2】:

    Bob 的解决方案效果很好。两个注意事项:

    1. 为此,您需要在数据表(此处:groupValues)中为 folium.Choropleth 定义中指定的 geo_data= 表(此处:counties)中的所有条目提供值。
      cp = folium.Choropleth(
              geo_data = counties,
              name = 'choropleth',
              data = groupValues, 
      etc.
      

      如果 geo_data 表中的条目缺少数据表中的对应值,则循环将抛出 KeyError。为了解决这个问题,我创建了一个循环,使用 try . . . except 将正确的值添加到 geojson 表中。

      1. 我能够将数字数据 (float64) 添加到 cp.geojson 表中,folium 允许我使用 GeoJsonTooltip 和 LayerControl 添加它,但是当我尝试显示地图时,我得到一个“float64 数据无法序列化“ 错误。我通过在将其添加到 cp.geojson 表之前将其转换为字符串来修复此问题。

      工作循环如下所示:

      for row in cp.geojson.data['features']:
          try:
              row['properties']['PctAllPop'] = str(groupValues.loc[row['properties']['FIPS'],'PctAllPop'])
          except KeyError:
              row['properties']['PctAllPop'] = 'No adherents'
          
      folium.GeoJsonTooltip(['NAME','PctAllPop'],aliases=['County:','Pct total pop:']).add_to(cp.geojson)
      

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2016-11-05
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多