【问题标题】:Making choropleth map from dataframe using folium使用 folium 从数据框制作等值线图
【发布时间】:2020-06-25 19:02:41
【问题描述】:

我正在尝试使用 pandas 制作等值线图来读取 csv 数据。我在网上学习了一个教程,但没有让我的代码工作。

教程链接:https://towardsdatascience.com/choropleth-maps-with-folium-1a5b8bcdd392。这是我要开始工作的代码:

# CHOLOPLETH MAP
import json
kunnat_geo = r'kunnat.geojson'
coords = pd.read_csv('taulu2.csv')

map_cholo = folium.Map(location=[65,26], zoom_start=4, tiles='stamenwatercolor')
map_cholo.choropleth(
    geo_data=kunnat_geo,
    data=coords,
    columns=['ALUE', 'MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018'],
    key_on='features.properties.Kunta',
    fill_color='YlGnBu', 
    fill_opacity=1, 
    line_opacity=1,
    legend_name='MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018',
    smooth_factor=0)

map_cholo

但是它给了我一个属性错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-74-026c8c7bfd66> in <module>
     14     line_opacity=1,
     15     legend_name='MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018',
---> 16     smooth_factor=0)
     17 
     18 map_cholo

D:\Anaconda3\lib\site-packages\folium\folium.py in choropleth(self, *args, **kwargs)
    416         )
    417         from folium.features import Choropleth
--> 418         self.add_child(Choropleth(*args, **kwargs))
    419 
    420     def keep_in_front(self, *args):

D:\Anaconda3\lib\site-packages\folium\features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
   1249                 style_function=style_function,
   1250                 smooth_factor=smooth_factor,
-> 1251                 highlight_function=highlight_function if highlight else None)
   1252 
   1253         self.add_child(self.geojson)

D:\Anaconda3\lib\site-packages\folium\features.py in __init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup)
    456             self.convert_to_feature_collection()
    457             if self.style:
--> 458                 self._validate_function(style_function, 'style_function')
    459                 self.style_function = style_function
    460                 self.style_map = {}

D:\Anaconda3\lib\site-packages\folium\features.py in _validate_function(self, func, name)
    521         """
    522         test_feature = self.data['features'][0]
--> 523         if not callable(func) or not isinstance(func(test_feature), dict):
    524             raise ValueError('{} should be a function that accepts items from '
    525                              'data[\'features\'] and returns a dictionary.'

D:\Anaconda3\lib\site-packages\folium\features.py in style_function(x)
   1223 
   1224         def style_function(x):
-> 1225             color, opacity = color_scale_fun(x)
   1226             return {
   1227                 'weight': line_weight,

D:\Anaconda3\lib\site-packages\folium\features.py in color_scale_fun(x)
   1204 
   1205             def color_scale_fun(x):
-> 1206                 key_of_x = get_by_key(x, key_on)
   1207                 if key_of_x is None:
   1208                     raise ValueError("key_on `{!r}` not found in GeoJSON.".format(key_on))

D:\Anaconda3\lib\site-packages\folium\features.py in get_by_key(obj, key)
   1201                 return (obj.get(key, None) if len(key.split('.')) <= 1 else
   1202                         get_by_key(obj.get(key.split('.')[0], None),
-> 1203                                    '.'.join(key.split('.')[1:])))
   1204 
   1205             def color_scale_fun(x):

D:\Anaconda3\lib\site-packages\folium\features.py in get_by_key(obj, key)
   1200             def get_by_key(obj, key):
   1201                 return (obj.get(key, None) if len(key.split('.')) <= 1 else
-> 1202                         get_by_key(obj.get(key.split('.')[0], None),
   1203                                    '.'.join(key.split('.')[1:])))
   1204 

AttributeError: 'NoneType' object has no attribute 'get'

这是我使用的geojson文件:https://raw.githubusercontent.com/varmais/maakunnat/master/kunnat.geojson

还有数据:https://pastebin.com/SdEXDM89

【问题讨论】:

  • 使用key_on='feature.properties.Kunta'。但是,geojson文件似乎有问题。
  • 如您所见,我已经在使用“feature.properties.Kunta”。我不认为 geojson 文件有什么问题,因为它可以自己工作。我只是在将它实施到等值线地图时遇到了麻烦。
  • 不。您正在使用key_on='features.properties.Kunta'。我已经用你的 geojson 文件中的前三个 id 创建了一个玩具 geojson 文件,我可以毫无问题地获取地图。
  • 我使用了 key_on='feature.properties.Kunta ,但它给了我 ValueError: key_on 'properties.Kunta'` 在 GeoJSON 中找不到。 `
  • 我告诉过你,geojson 有问题。

标签: python pandas geojson folium choropleth


【解决方案1】:

您使用的 geojson 包含多个缺陷。只是一个例子,看看

{"id":49,"type":"Feature","geometry":{"coordinates":[[[[25.134789950981,65.0613390595066],[24.9509796014637,65.1411214235741],[24.1149334366251,65.1739425977789],[24.1374948169974,65.2416484128856],[24.6071512739822,65.211492673253],[25.2249108920834,65.2366422852267],[25.319454962569,65.2626286706296],[25.5524286749097,65.2529182323504],[25.6623616596278,65.260748036272],[25.7723806903971,65.2919894627541],[25.9132907485861,65.2931966157892],[26.0266782370841,65.2703735338041],[25.993210256336,65.1914324603082],[25.736352838218,65.2201254797071],[25.5637124399186,65.1919505955019],[25.635197316715,65.1390540421563],[25.6086182968484,65.0816350751541],[25.3775958184099,65.1070832092577],[25.2892903033333,65.0817828240486],[25.134789950981,65.0613390595066]]]],"type":"MultiPolygon"},"properties":{"nationalCode":84,"Country":"FI","name_fi":"Haukipudas","name_se":"Haukipudas"}}

它的属性中没有Kunta。这会使您的 geojson 格式错误。我建议你替换那个geojson,或者作为最后的手段编辑它。

【讨论】:

  • 你在哪里找到那个geojson文件?看起来和我的不一样。 raw.githubusercontent.com/varmais/maakunnat/master
  • 您在原始问题中提供了该文件:这是我使用的 geojson 文件:raw.githubusercontent.com/varmais/maakunnat/master/…
  • 哦,是的,我的错,似乎某些“Kunta”属性有一些额外的变量“name_fi”和“”name_se”,因为一些芬兰城市也有瑞典名称。我将所有“name_fi”替换为“Kunta”,但它什么也没做。我还打印了没有数据的 GeoJson 文件,它似乎工作得很好。
  • 好吧,正如我告诉你的那样,该文件存在多个缺陷。没有任何更新,我认为问题已得到解答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
相关资源
最近更新 更多