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

前端 未结 2 1300
一整个雨季
一整个雨季 2021-01-14 09:26

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

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 09:57

    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)
    

提交回复
热议问题