GeoJson usage in folium

2019-08-20 23:19发布

问题:

I have used GeoJson method in FeatureGroup of folium to add polygon layers to my map. I am getting some error. I have checked the syntax of this method.Everything is correct but still i am getting the error mentioned below in the image.

回答1:

I stumbled upon the same issue.

From the documentation at https://python-visualization.github.io/folium/modules.html#folium.features.GeoJson you can see various examples of opening the geojson file.

>>> # Providing file that shall be embedded.
>>> GeoJson(open('foo.json'))
>>> # Providing filename that shall not be embedded.
>>> GeoJson('foo.json')
>>> # Providing dict.
>>> GeoJson(json.load(open('foo.json')))
>>> # Providing string.
>>> GeoJson(open('foo.json').read())

What worked for me was,

>>> GeoJson(open('foo.json').read())


回答2:

The data parameter in GeoJson() needs string to process, you sending it as a file object, in order to convert it to string, just add read() method at the end of open() to convert it to string.

Like this

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

That's all, no over will pop up then.