Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 847
Abhinav Jain Avatar asked Sep 09 '25 12:09

Abhinav Jain


2 Answers

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)
like image 133
David Olmo Pérez Avatar answered Sep 11 '25 00:09

David Olmo Pérez


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"""
        <iframe id="popupIFrame"
            title="{name}"
            width="600"
            height="500"
            align="center"
            src="{source}">
        </iframe>
        """
    # 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)
like image 43
and-viceversa Avatar answered Sep 11 '25 00:09

and-viceversa