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

前端 未结 2 1331
一整个雨季
一整个雨季 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条回答
  •  猫巷女王i
    2021-01-14 10:05

    To build on @David Olmo Pérez's answer, this seemed more intuitive to me.

    # Create feature group to add to folium.Map object
    layer = folium.FeatureGroup(name='your layer name', show=False)
    
    # load GEOJSON, but don't add it to anything
    temp_geojson = folium.GeoJson('path/to/your/file.geojson')
    
    # iterate over GEOJSON, style individual features, and add them to FeatureGroup
    for feature in temp_geojson.data['features']:
        # GEOJSON layer consisting of a single feature
        temp_layer = folium.GeoJson(feature,
                                    style_function={
                                        'color': '#000000',
                                        'opacity': 0.7,
                                    })
        # lambda to add HTML
        foo = lambda name, source: f"""
            
            """
        # create Popup and add it to our lone feature
        # this example embeds a .png
        folium.Popup(
            html=foo('name of your IFrame',
                     f'path/to/embed/file_{feature["properties"]["some_attribute"]}.png')
        ).add_to(temp_layer)
    
        # consolidate individual features back into the main layer
        temp_layer.add_to(layer)
    
    # add main layer to folium.Map object
    layer.add_to(m)
    

提交回复
热议问题