Show different pop-ups for different polygons in a

2019-05-29 17:19发布

问题:

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)