在 GeoJSON [Folium] [Python] [Map] 中为不同的多边形显示不同的弹出窗口 [英] Show different pop-ups for different polygons in a GeoJSON [Folium] [Python] [Map]

查看:34
本文介绍了在 GeoJSON [Folium] [Python] [Map] 中为不同的多边形显示不同的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 folium 来可视化城市中的区域.

I am using folium to visualise zones in an city.

我的 GeoJSON 是一个具有多个多边形作为特征的 FeatureCollection.我希望能够为文件中的不同多边形添加不同的弹出窗口.这个想法是在 GEOJSON 文件中显示不同多边形的名称.

My GeoJSON is a FeatureCollection with multiple polygons as features. I want to be able to add different popups for different polygons in the file. The idea is to show names of the different polygons in the GEOJSON file.

我能够向完整的 geoJSON 添加一个弹出窗口.但是,我希望能够为不同的多边形添加不同的弹出窗口(本质上是特征的名称).

I was able to add a popup to the complete geoJSON. However, I want to be able to add different popup for different polygons (essentially the name of the feature).

folium.GeoJson(gurgaon_subzone,name='geojson').add_child(folium.Popup("Gurgaon")).add_to(m)

推荐答案

有一个解决方法.您需要遍历每个 geoJson 功能并为每个功能创建一个新的 geojson.然后,为每个 geoJson 功能添加一个弹出窗口.然后将所有特征组合在一个层中.在我的代码中,完整的 geoJson 是 data_geojson_dict

There is a work-around for this. You need to iterate over the each geoJson feature and create a new geojson for each one. Then, add a popup for each geoJson feature. Then combine all features in a layer. In my code, the full geoJson is data_geojson_dict

layer_geom = folium.FeatureGroup(name='layer',control=False)

for i in range(len(data_geojson_dict["features"])):
    temp_geojson = {"features":[data_geojson_dict["features"][i]],"type":"FeatureCollection"}
    temp_geojson_layer = folium.GeoJson(temp_geojson,
                   highlight_function=lambda x: {'weight':3, 'color':'black'},
                    control=False,
                    style_function=lambda feature: {
                   'color': 'black',
                   'weight': 1},
                    tooltip=folium.features.GeoJsonTooltip(fields=list_tooltip_vars,
                                        aliases=[x.capitalize()+":" for x in list_tooltip_vars], 
                                          labels=True, 
                                          sticky=False))
    folium.Popup(temp_geojson["features"][0]["properties"]["productor"]).add_to(temp_geojson_layer)
    temp_geojson_layer.add_to(layer_geom)

layer_geom.add_to(m)
folium.LayerControl(autoZIndex=False, collapsed=True).add_to(m)

这篇关于在 GeoJSON [Folium] [Python] [Map] 中为不同的多边形显示不同的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆