0

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 ?

0

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()))
  • I replaced the following code fg.addchild(folium.GeoJson(open("world.json",encoding = "utf-8-sig").read())) An error occurred as: ` File "map1.py", line 23, in <module> fg.addchild(folium.GeoJson(open("world.json",encoding = "utf-8-sig") .read()))` AttributeError: 'FeatureGroup' object has no attribute 'addchild' I am completely new to webgis and python, can you help! – Mehmud Oct 7 '17 at 21:22
  • 1
    Oops I might have inadvertently introduced that error. Try add_child instead of addchild. So it would be fg.add_child(folium.GeoJson(open("world.json",encoding = "utf-8-sig").read())) – Strat-O Oct 7 '17 at 21:37
  • You can also split that line up into two lines like this: my_world_json = open("world.json",encoding = "utf-8-sig").read() then on the next line do: fg.add_child(folium.GeoJson(my_world_json)) what that does is help you determine where the error is occuring. – Strat-O Oct 7 '17 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.