如何在 geojson 弹出窗口中显示信息 - Python

Den*_*Tay 3 python json geojson folium

我在 folium 弹出窗口中显示信息时遇到问题,其中信息是从 JSON 文件检索的。目前,我的代码仅从 JSON 文件中检索最后一条信息并将其插入到所有弹出窗口中,因此我的所有弹出窗口仅显示一条信息。我似乎无法弄清楚每个节点在弹出窗口中都有自己独特的信息。任何帮助表示赞赏。

# reading JSON file
with open('exportBuilding.geojson') as access_json:
    read_content = json.load(access_json)

feature_access = read_content['features']

# Creating Folium Map
m = folium.Map(location=[1.400150, 103.910172], titles="Punggol", zoom_start=17)
nodeData = os.path.join('exportBuilding.geojson')
geo_json = folium.GeoJson(nodeData)

# retrieve all names and store in popup
for feature_data in feature_access:
    property_data = feature_data['properties']
    geo_json.add_child(folium.Popup(property_data['name'))

geo_json.add_to(m)
Run Code Online (Sandbox Code Playgroud)

Den*_*Tay 7

对于和我遇到同样问题的人,最新的 folium 中有一个名为“GeoJsonPopup”的函数,它将从 JSON 文件中检索您指定的所有信息并将其显示在 Popup 中,解决了所有节点都会有其自身信息的问题。拥有独特的个人信息。

不是创建一个 for 循环来循环整个 JSON,

# reading JSON file
with open('exportBuilding.geojson') as access_json:
    read_content = json.load(access_json)

feature_access = read_content['features']

# Creating Folium Map
m = folium.Map(location=[1.400150, 103.910172], titles="Punggol", zoom_start=17)
nodeData = os.path.join('exportBuilding.geojson')

# This is retrieve all information, in this case is name from my JSON file 
# and display it into my popup, such that all nodes 
# will have its own unique information.
geo_json = folium.GeoJson(nodeData, popup=folium.GeoJsonPopup(fields=['name']))

geo_json.add_to(m)
Run Code Online (Sandbox Code Playgroud)