[GIS] Understanding ValueError from folium

foliumpythonpython 3valueerror

I was trying the following line of codes in python (that I learnt from a tutorial),

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")
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",
    fill=True, fill_color=color_producer(el), color = 'grey', fill_opacity=0.7))

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

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

I receive an error saying

Traceback (most recent call last):
  File "map1.py", line 23, in <module>
    fg.add_child(folium.GeoJson(data=(open('world.json', 'r', encoding='
utf-8-sig'))))
  File "C:\Users\TOSHIBA\AppData\Local\Programs\Python\Python36-32\lib\s
ite-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'>.

I typed exactly the code what I learnt from tutorial course, but seems the code not working for me. Please help me identify the problem ! I was trying it on Atom 1.21.0 or is there any changes made in folium ?

Best Answer

Python needs to be able to find the file world.json. Either that or there is something python isn't liking about that file. It should be located in the same directory as map1.py.

Looking at the same issue on Stackoverflow, the solution was to change line 23 to:

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