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.