Skip to main content
 首页 » 编程设计

python之将弹出窗口添加到 folium 中的 geojson 层

2024年10月17日7artech

我有以下使用此 geojson file 的代码作为输入。

import folium 
markers_geojson='volcanoes_json.geojson'   
map=folium.Map(location=[0,0],zoom_start=6,tiles='Mapbox bright') 
map.add_child(folium.GeoJson(data=open(markers_geojson),name='Volcano').add_child(folium.Popup("A plain pop up string")))     
map.save(outfile='test5.html') 

以上代码生成带有标记的传单 map 。问题是它当前在弹出消息中显示一个静态字符串(即“一个普通的弹出字符串”)。我不知道如何显示 geojson 属性(例如 STATUS 属性)的值。

有人知道如何实现这个吗?

请您参考如下方法:

您需要遍历文件。下面提到的文件是一个简单的文件,有纬度、经度和高程三列。

如果您以此格式创建一个简单的文本文件,此代码将循环遍历文件并添加它们。它获取具有纬度、经度、高程的列,并在弹出窗口中创建动态弹出窗口。

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())