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

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

问题描述

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

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天全站免登陆