您需要遍历文件。下面提到的文件是一个简单的文件,它有纬度、经度和海拔三列。
如果您以这种格式创建一个简单的文本文件,此代码将循环通过一个文件并添加它们。它获取具有 lat、long、elevation 的列,并在弹出窗口中创建一个动态弹出窗口。
data = pandas.read_csv("Volcanoes.txt")
lat = list(data["LAT"])
lon = list(data["LON"])
elev = list(data["ELEV"])
# make a color code for elevation
def color_producer(elevation):
if elevation < 1000:
return 'green'
elif 1000 <= elevation < 3000:
return 'orange'
else:
return 'red'
# set the base map
map = folium.Map(location=[47.0, -122.5], zoom_start=12)
# add an additional tile layer
map.add_tile_layer("Stamen Toner")
fgv = folium.FeatureGroup(name="Volcanoes")
# loop through and plot everything
for lt, ln, el in zip(lat, lon, elev):
fgv.add_child(folium.CircleMarker(location=[lt, ln], radius = 6, popup=str(el)+" m",
fill_color=color_producer(el), fill=True, color = 'grey', fill_opacity=0.7))
fgp = folium.FeatureGroup(name="Population")
# add a map of shading by population
fgp.add_child(folium.GeoJson(data=open('world.json', 'r', encoding='utf-8-sig').read(),
style_function=lambda x: {'fillColor':'green' if x['properties']['POP2005'] < 10000000
else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 else 'red'}))
# add the layers
map.add_child(fgv)
map.add_child(fgp)
map.add_child(folium.LayerControl())