I'm doing the Python Mega Course Bundle on Udemy and I'm at the part where I must make a WebMap using folium. It was all going normally until I had to add a polygon layer through GeoJson and that's when the issues started. Okay so first off here is the code:
import folium
import pandas
map = folium.Map(location=(42.521422, 27.461541),zoom_start= 5,tiles="Mapbox Bright") #initializes a map at location by co-ordinates
data = pandas.read_csv("dataset.txt") #reads the CSV file with pandas
coords = zip(list(data["LAT"]),list(data["LON"]), list(data["NAME"]), list(data["ELEV"]))
def colourPicker(elev): #decided colours of points based on elevations
if elev < 1000:
return "green"
elif 1000 <= elev < 2000:
return "orange"
elif 2000 <= elev < 3000:
return "red"
else:
return "darkred"
group = folium.FeatureGroup(name="My Map")
#group.add_child(folium.Marker(location=[42.521422, 27.461541], popup="Home", icon=folium.Icon(color="green")))
#map.add_child(group)
for x, y, z, e in coords:
#group.add_child(folium.Marker(location = [x,y], popup = folium.Popup(z + " " + str(e), parse_html = True), icon = folium.Icon(color = colourPicker(e))))
group.add_child(folium.CircleMarker(location = [x,y], popup = folium.Popup(z + " " + str(e), parse_html = True),
fill = True, fill_opacity = 0.8, radius = 6, color = 'gray', fill_color = colourPicker(e))) #creates the little tabs on the map showing the volcanoes, their heights and colours based on height
group.add_child(folium.GeoJson(data=(open('world.json', 'r', encoding='utf-8-sig').read()))) # <----- problematic line. It's supposed to draw polygons on the map around each country
map.add_child(group)
map.save("Map.html") #saves the map object into a html file
Okay so when i run the program, my Cmder gets stuck on this:
for 46.36 seconds give or take. Once it finishes, as you can see from the attempts above, it doesn't give me any errors. UPDATE: The errors in the previous Cmder runs are from putting the .read() function is the wrong place. That was fixed before I even posted this. The problem is ONCE the code actually gets executed, it takes a long time for it to actually run and the resulting Map.html file is broken as shown below.
WHen I try to open the WebMap in my browser to see if those 46 seconds were worth it, I get a blank white screen, my fan goes nuts and just look at those CPU temps.
Any ideas on how I can fix this? Or at least what could be causing the issue?
The entire dir, including the json file which is apparently breaking things, is at the following dropbox:
https://www.dropbox.com/sh/2wwe08j2q3qo6ua/AABYuAx9F5I4Q3t3b0GYHkona?dl=0
Thanks in advance for your help
The issue was the add_child function was inside a loop where it shouldn't have been.
Working full code:
import folium
import pandas
#Functions:
#colours of points based on elevations
def colourPicker(elev):
if elev < 1000:
return "green"
elif 1000 <= elev < 2000:
return "orange"
elif 2000 <= elev < 3000:
return "red"
else:
return "darkred"
#initializes a map at location by co-ordinates
mapInit = folium.Map(location=(42.521422, 27.461541),zoom_start= 5,tiles="Mapbox Bright")
#reads the CSV file with pandas
data = pandas.read_csv("dataset.txt")
#Zip file with columns from dataset
coords = zip(list(data["LAT"]),
list(data["LON"]),
list(data["NAME"]),
list(data["ELEV"]))
#Volcano Markers Feature Group for the map
volcanoGroup = folium.FeatureGroup(name="Volcano Markers")
#Volcano Markers Feature Group for the map
populationGroup = folium.FeatureGroup(name="Population Overlay")
#Adding of circle markers based on the zip file
for lat, lon, name, elev in coords:
volcanoGroup.add_child(folium.CircleMarker(location = [lat,lon],
popup = folium.Popup(name + " " + str(elev), parse_html = True),
fill = True,
fill_opacity = 0.8,
radius = 6,
color = 'gray',
fill_color = colourPicker(elev)))
#Adding of the polygon overlay and setting colours based on population as given in the dataset
overlay = open('world.json', 'r', encoding='utf-8-sig').read()
overlayStyle = lambda x: {'fillColor':'green' if x['properties']['POP2005'] < 10000000
else 'yellow' if 10000000 <= x['properties']['POP2005'] < 50000000
else 'orange' if 50000000 <= x['properties']['POP2005'] < 100000000
else 'red'}
populationGroup.add_child(folium.GeoJson(data = overlay,
style_function = overlayStyle))
#Adding the Feature Groups and Layer Control to the Map
mapInit.add_child(volcanoGroup)
mapInit.add_child(populationGroup)
mapInit.add_child(folium.LayerControl())
#saves the map object into a html file
mapInit.save("Map.html")
print("Execution Success.")