我正在做Udemy上的Python Mega Course Bundle,而我必须使用folium制作WebMap。一切正常,直到我不得不通过GeoJson添加多边形图层,然后问题才开始出现。好的,首先是下面的代码:
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
持续46.36秒。一旦完成,就可以从上面的尝试中看到,它不会给我任何错误。 更新:以前的Cmder运行中的错误是由于将.read()函数放在错误的位置而引起的。在我什至发布此消息之前,该问题已解决。问题在于,一旦代码真正被执行,它就会花很长时间才能真正运行,并且生成的Map.html文件会损坏,如下所示。
当我尝试在浏览器中打开WebMap来查看这46秒是否值得时,出现空白的白色屏幕,我的风扇发疯,只看那些CPU的温度。
关于如何解决此问题的任何想法?或者至少是什么原因导致了该问题?
整个目录(包括显然是在破坏内容的json文件)位于以下保管箱:
https://www.dropbox.com/sh/2wwe08j2q3qo6ua/AABYuAx9F5I4Q3t3b0GYHkona?dl=0
预先感谢您的帮助
答案 0 :(得分:0)
问题在于add_child函数位于本不应该存在的循环中。
完整的代码:
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.")