Skip to main content
 首页 » 编程设计

使用 GeoJson 时 Python 编译时错误

2024年10月01日6zhoujg

我使用了下面的代码,但它给出了一个错误。这是我的代码:

fg_population.add_child(folium.GeoJson(data=open('world.json', 'r', encoding='utf-8-sig'), 
style_function=lambda x: {'fillColor':'green' if x['properties']['POP2005'] < 10000000 
else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 else 'red'})) 

然后我收到以下错误消息:

ValueError: Unhandled object <_io.TextIOWrapper name='world.json' mode='r' encoding='utf-8-sig'>

请您参考如下方法:

查看the docs ,您不应使用 data= 打开文件。此外,一些格式化和拆分会有所帮助:

the_world = open('world.json', 'r', encoding='utf-8-sig') 
the_style = lambda x: {'fillColor': 
                           'green' if x['properties']['POP2005'] < 10000000 
                      else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 
                      else 'red'} 
the_map = folium.GeoJson(the_world, style_function=the_style) 
fg_population.add_child(the_map)