Python 代码执行时间很长,结果被破坏

Python code taking a long time to execute and result is broken

我正在 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

好的,所以当我 运行 程序时,我的 Cmder 卡在了这个上:

46.36 秒给予或接受。一旦完成,正如您从上面的尝试中看到的那样,它不会给我任何错误。 更新:之前 Cmder 运行 中的错误是由于将 .read() 函数放在了错误的位置。这在我发布之前就已修复。问题是一旦代码真正被执行,它需要很长时间才能真正 运行 并且生成的 Map.html 文件被破坏,如下所示。

当我尝试在浏览器中打开 WebMap 以查看这 46 秒是否值得时,我得到一个空白的白屏,我的风扇发疯了,只看那些 CPU 温度。

关于如何解决这个问题的任何想法?或者至少是什么导致了这个问题?

整个目录,包括 json 文件,这显然是破坏性的东西,在以下保管箱中:

https://www.dropbox.com/sh/2wwe08j2q3qo6ua/AABYuAx9F5I4Q3t3b0GYHkona?dl=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.")