python folium解析错误oppening json

问题描述 投票:1回答:1

我正在观看udemy的python mega课程我按照指示编写所有应用程序在应用程序2中使用Python和Folium创建Web地图,我面临着这样的错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-0a5927ab8141> in <module>()
     26 
     27 fgp.add_child(folium.GeoJson(data=open('world.json', 'r', 
        encoding='utf-8-sig'),
---> 28 style_function=lambda x: {'fillColor':'green' if x['properties']
        ['POP2005'] < 10000000
     29 else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 
        else 'red'}))
     30 

C:\Users\poya\Anaconda3\lib\site-packages\folium\features.py in 
__init__(self, data, style_function, name, overlay, control, smooth_factor, 
highlight_function)
    493                 raise ValueError(msg)
    494         else:
--> 495             raise ValueError('Unhandled object {!r}.'.format(data))
    496 
    497         if style_function is None:

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

这个代码:

import folium
import pandas

data = pandas.read_csv("Volcanoes.txt")
lat = list(data["LAT"])
lon = list(data["LON"])
elev = list(data["ELEV"])

def color_producer(elevation):
    if elevation < 1000:
        return 'green'
    elif 1000 <= elevation < 3000:
        return 'orange'
    else:
        return 'red'
 map = folium.Map(location=[38.58, -99.09], zoom_start=6, tiles="Mapbox 
 Bright")

 fgv = folium.FeatureGroup(name="Volcanoes")

for lt, ln, el in zip(lat, lon, elev):
    fgv.add_child(folium.CircleMarker(location=[lt, ln], radius = 6, 
popup=str(el)+" m",
    fill_color=color_producer(el), color = 'grey', fill_opacity=0.7))

fgp = folium.FeatureGroup(name="Population")

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

map.add_child(fgv)
map.add_child(fgp)
map.add_child(folium.LayerControl())

map.save("Map1.html")
python json valueerror folium
1个回答
0
投票

尝试使用以下更改:

fgp.add_child(folium.GeoJson(data=open('World_Population.json').read(),
style_function = lambda x: {'fillColor':'green' if x['properties']['POP2005']< 10000000 else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 else'red'}))
© www.soinside.com 2019 - 2024. All rights reserved.