-1

I am writing this code:

import pandas
import folium
map1 = folium.Map(zoom_start = 6, tiles = "Stamen Terrain")
vol = pandas.read_csv("Volcanoes.txt")
lat = list(vol["LAT"])
lon = list(vol["LON"])
name = list(vol["NAME"])
loc = list(vol["LOCATION"])
elev = list(vol["ELEV"])
html = """
Hello, My name is %s, i live in %s with an elevation of %s meters high
"""
def color_code(elevation):
    if elevation < 1000:
        return "green"
    elif 1000 <= elevation <= 3000:
        return "orange"
    else:
        return "red"
fg = folium.FeatureGroup(name="My Map")
for lt, ln, n, l, e in zip(lat, lon, name, loc, elev):
    iframe = folium.IFrame(html=html % (n, l, str(e)), width=200, height=110)
    fg.add_child(folium.CircleMarker(location=(lt, ln), radius=6, popup=folium.Popup(iframe, parse_html=True), fill_color = color_code(e), color = None, fill_opacity = 1))
fg.add_child(folium.GeoJson(data=open('world.json','r').read()))
map1.add_child(fg)
map1.save("Map1.html")

now i want to add the polygons using that JSON file, and it is not working.

"Side note, when that error is raised: FileNotFoundError: [Errno 2] No such file or directory, it is followed by the content of the file"

tried to remove the .read() method and it raised another error: ValueError: Cannot render objects with any missing geometries: <_io.TextIOWrapper name='world.json' mode='r' encoding='cp1252'> I've been here for...god knows how long and i made sure that the JSON file is in the same directory so what is going on here?

Full Exception trace back:

FileNotFoundError                         Traceback (most recent call last)
Cell In[4], line 24
     21     iframe = folium.IFrame(html=html % (n, l, str(e)), width=200, height=110)
     22     fg.add_child(folium.CircleMarker(location=(lt, ln), radius=6, popup=folium.Popup(iframe, parse_html=True), fill_color = color_code(e), color = None, fill_opacity = 1))
---> 24 fg.add_child(folium.GeoJson(data=data_json.read()))
     25 map1.add_child(fg)
     26 map1.save("Map1.html")

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\folium\features.py:643, in GeoJson.__init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup, zoom_on_click, marker)
    638         raise TypeError(
    639             "Only Marker, Circle, and CircleMarker are supported as GeoJson marker types."
    640         )
    641 self.marker = marker
--> 643 self.data = self.process_data(data)
    645 if self.style or self.highlight:
    646     self.convert_to_feature_collection()

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\folium\features.py:680, in GeoJson.process_data(self, data)
    678         if not self.embed:
    679             self.embed_link = data
--> 680         with open(data) as f:
    681             return json.loads(f.read())
    682 elif hasattr(data, "__geo_interface__"):

FileNotFoundError: [Errno 2] No such file or directory: 

followed by the content of the actual file.

  • Which specific line in your code is resulting in the `FileNotFound` error? Can you post the complete exception traceback? – larsks Feb 05 '23 at 12:32
  • added the complete exception traceback – Rookie-kinda Feb 05 '23 at 12:42
  • That traceback doesn't seem to include any lines from your code (all of the referenced lines are contained in the `folium` package). Has it been truncated? Also, it looks like maybe you're running this in Jupyter or something similar; is that accurate? – larsks Feb 05 '23 at 12:44
  • 2
    The error seems to show exactly what the issue is: Folium is expecting `data` to be a path to use `open()` on, yet you're passing the contents of the json instead. Change the `data` value to the path. – B Remmelzwaal Feb 05 '23 at 12:54
  • i had to use this on jupyter since the error included the content of the file and the file is HUGE, not in size but it's really really long, the normal python shell couldn't print out the entire thing. – Rookie-kinda Feb 05 '23 at 12:58
  • ok then, how do i change the data value to path, because when i removed the read method it just errored (i mentioned the error), and i don't really know what to do now. – Rookie-kinda Feb 05 '23 at 12:58
  • The path is just `world.json`. – B Remmelzwaal Feb 05 '23 at 15:13
  • yes ik, still not working tho – Rookie-kinda Feb 05 '23 at 17:23

1 Answers1

0

Reading folium python geoJson API it suggests that data parameter is a Pandas dataframe. PLease use pandas read_json method to read json file and input into data.

Thalaiva
  • 88
  • 7
  • i tried to do that, it gave me this error: ValueError: Expected object or value – Rookie-kinda Feb 05 '23 at 12:59
  • I tried your code, but used read_json instead of open, and I didn't get any error message. When you tried pandas.read_json you got a different error, which means it should work. Can you share a couple of lines from volcanoes.txt and world.json please? – Thalaiva Feb 05 '23 at 18:39
  • sure, but i should add, i tried a different file than world.json and it worked just fine. – Rookie-kinda Feb 06 '23 at 07:06
  • okay. did it work fine with reading it using pandas dataframe? It seems then the problem is with the world.json file. – Thalaiva Feb 06 '23 at 09:56