Show different pop-ups for different polygons in a GeoJSON [Folium] [Python] [Map]

假装没事ソ 提交于 2020-01-03 02:59:05

问题


I am using folium to visualise zones in an city.

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.

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)

回答1:


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)


来源:https://stackoverflow.com/questions/54595931/show-different-pop-ups-for-different-polygons-in-a-geojson-folium-python-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!