繁体   English   中英

ValueError: 未处理的对象 <_io.TextIOWrapper name='world.json' mode='r' encoding='utf-8-sig'>

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

我正在尝试使用 GeoJson 多边形颜色特征更改国家/地区的颜色。

这是给我一个错误的代码。

import folium
import pandas

data = pandas.read_csv("Volcanoes_USA.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")

fg = folium.FeatureGroup(name="My Map")

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

fg.add_child(folium.GeoJson(data=open('world.json','r', encoding='utf-8-sig'),
style_function = lambda x: {'fillColour':'yellow'}))

最后两行导致以下错误消息:

> > Traceback (most recent call last):   File "map1.py", line 25, in <module>
>     style_function = lambda x: {'fillColour':'yellow'}))   File "/Users/g_wauns/anaconda3/lib/python3.6/site-packages/folium/features.py",
> line 495, in __init__
>     raise ValueError('Unhandled object {!r}.'.format(data)) ValueError: Unhandled object <_io.TextIOWrapper name='world.json'
> mode='r' encoding='utf-8-sig'>.

由于我是编码新手,我不知道这意味着什么。

[GitHub]: ( master ) folium/folium/features.py:343 (就像在这一点上一样),看起来folium.GeoJson构造函数只能处理(对于你的场景):

  1. 文件
  2. Python字典

因此,解决此问题的最简单方法是传递文件名:

fg.add_child(folium.GeoJson("world.json", style_function=lambda x: {"fillColour": "yellow"}))

当然,您可以自己将文件读入dict (使用[Python]: json — JSON 编码器和解码器模块),但这正是上述文件在下面约 20 行中所做的。

只需为数据添加read()

fg.add_child(folium.GeoJson(data=open('world.json', 'r', encoding='utf-8-sig').read(), style_function=lambda x: {'fillColor':'yellow'}))

这对我有用

我使用以下代码块中的代码解决了上述完全相同的问题

import folium
import pandas
import json

data = pandas.read_csv("Volcanoes_USA.csv")

我必须创建以下行作为 str 函数,以使我的代码能够正确读取 world.json 文件。 课程中的例子对我不起作用。 因此,我必须导入 json 库并首先使用编码读取文件,然后将其传递给代码中进一步描述的 GeoJson 方法

data_json = open("world.json", 'r', encoding='utf-8-sig').read()

lat = list(data["LAT"])
long = list(data["LON"])
elev = list(data["ELEV"])
name = list(data["NAME"])

def pin_color(elev):
    if elev <= 1500.0:
        return "green"
    elif elev >= 3000.0:
        return "red"
    else:
        return "orange"

map = folium.Map(location=[38.58,-99.09], zoom_start=6, control_scale=True,
tiles="Stamen Terrain")
map.add_child(folium.LatLngPopup())
map

fg = folium.FeatureGroup(name="my_map")

我无法使用课程中解释的行: fg.add_child(folium.GeoJson(data=open('world.json', 'r', encoding=utf-8-sig))) 所以我将上面代码中提到的 data_json 定义为一个 str,供 folium.GeoJson 在下面读取。

fg.add_child(folium.GeoJson(data=data_json,
style_function=lambda x: {'fillColor':'green' if x['properties'] 
['POP2005'] < 10000000
else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 else 
'red' }))

for i,j,z,v in zip(lat, long, elev, name):
    icn_pin=folium.Popup(str(v)+" Volcano"+": "+"Elevation:"+str(z)+"m "+" 
    "+"Lat:"+str(i)+" "+"Long:"+str(j),parse_html=True)
    fg.add_child(folium.CircleMarker(location=[i,j], popup=icn_pin, 
    radius=7, fill=True, color=pin_color(z), fill_color=pin_color(z), 
    fill_opacity=0.85))

map.add_child(fg)
map.save("world_pop_map.html")

正如 user9608587 发布的那样,它帮助我了解如何完成这项工作

我导入了 'io' 模块并使用了 io.open() 函数。

import io
data_json = io.open("world.json",'r',encoding='utf-8-sig').read()

最终代码,您只需复制粘贴即可,它会起作用。

import folium
import pandas,io

data = pandas.read_csv("Volcanoes_USA.txt")
data_json = io.open("world.json",'r',encoding='utf-8-sig').read()
lat = list(data.LAT)
lon = list(data.LON)
elev = list(data.ELEV)

def colour_producer(elev):
    if elev<1000:
        return "green"
    elif 1000<= elev <3000:
        return "orange"
    else:
        return "red"

Map = folium.Map(location=[38.58,-99.09],zoom_start=6,tiles = "Mapbox Bright")
fg = folium.FeatureGroup(name="My Map")

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

fg.add_child(folium.GeoJson(data=data_json,style_function=lambda x: {'fillColor':'blue' if x['properties']
['POP2005'] < 10000000
else 'green' if 10000000 <= x['properties']['POP2005'] < 20000000 else
'red' }))


Map.add_child(fg)
Map.save("Map1.html")

fg.add_child(folium.GeoJson(data = (open("world.json", "r", encoding=("utf-8-sig"))).read()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM