Shaik Mazahar Ali
Shaik Mazahar Ali

Reputation: 21

Python compile time error when using GeoJson

I used the below code and it gives an error. Here's my code:

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

Then I get the following error messages:

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

Upvotes: 2

Views: 109

Answers (1)

Ken Y-N
Ken Y-N

Reputation: 15018

Looking at the docs, you shouldn't use the data= to open the file. Furthermore, some formatting and splitting things up would help:

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)

Upvotes: 2

Related Questions