Skip to content

GeoJSON popup not working when GeoJSON in FeatureGroup #904

Closed

Description

@mfredk

Based on this example:
Highlight_Function.ipynb

Please add a code sample or a nbviewer link, copy-pastable if possible

import os
import folium
import pandas as pd

print(folium.__version__)

df = pd.read_csv('highlight_flight_trajectories.csv')

m = folium.Map(
    location=[40, 10],
    zoom_start=4,
    control_scale=True,
    prefer_canvas=True
)


def style_function(feature):
    return {
        'fillColor': '#ffaf00',
        'color': 'blue',
        'weight': 1.5,
        'dashArray': '5, 5'
    }


def highlight_function(feature):
    return {
        'fillColor': '#ffaf00',
        'color': 'green',
        'weight': 3,
        'dashArray': '5, 5'
    }

# Add Layer
layer = folium.FeatureGroup(name='layer')

for index, row in df.iterrows():
    c = folium.GeoJson(
        row['geojson'],
        name=('{}{}'.format(row['dep'], row['dest'])),
        overlay=True,
        style_function=style_function,
        highlight_function=highlight_function
    )
    folium.Popup('{}\n{}'.format(row['dep'], row['dest'])).add_to(c)
    c.add_to(layer)

layer.add_to(m)
folium.LayerControl().add_to(m)
m.save('maptest.html')

Problem description

Popup of a GeoJSON is not working, if the GeoJSON is contained in a FeatureGroup.

Expected Output

When clicking on a GeoJSON the popup should be displayed

Output of folium.__version__

0+unknown (pulled from GIT today and installed via setup.py)

Activity

  1. added
    bug
    on Jul 10, 2018
  2. Conengmo commented on Jul 10, 2018

    @Conengmo
    Member

    You're right, I'm seeing the problem. If GeoJson is added to a FeatureGroup, a Popup added to the GeoJson object doesn't work. I'm not sure why this is, in the rendered Javascript everything looks as expected.

    I'm not sure if this isn't expected behavior for Leaflet though. In Leaflet a GeoJSON object extends FeatureGroup, so maybe they're not supposed to be added to a FeatureGroup.

    Do you have any ideas on why this problem is occuring?

    Maybe you can try to collect your data in a single geojson description and make a single GeoJson object. That way you don't have a popup per feature though. That functionality still needs to be added to folium. If you want you can add this, PR's are welcome. @jtbaker is working on something similar for tooltips in #883.

    @jtbaker you mentioned looking into popups for GeoJson objects, can you chime in on this?

  3. mfredk commented on Jul 12, 2018

    @mfredk
    Author

    Hi, thanks for having a look and for your comment.
    I'm sorry, I am more the end-user type of guy, so that I unfortunately have no suggestion, why it does not work as intended. My current work-around is to add all the GeoJSONs to the main map. But as there are quite a few GeoJSONs I would have preferred to group them in a layer for easy toggling with the layer-control.

  4. davidolmo commented on Feb 12, 2019

    @davidolmo

    I think this issue was solved recently by a new version, or at least it seems like I could add custom popups to a Feature Group

    I'm using the version '0.7.0+49.g45a82c6' which is the one by git cloning master branch (it contains the Search feature added by @jtbaker which hasn't been released yet). i don't know if this version has any other difference other than the Search Plugin

    The difference with your code is that I'm iterating directly over the GeoJson instead a dataframe

    My code:

    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)
    

    When clicking on a GeoJson polygon it shows a custom PopUp (in this case, tonbaf 2006)
    As you can see I'm combining a tooltip and the GeoJson popup (which is the point of this issue)

    image

  5. Conengmo commented on Nov 30, 2022

    @Conengmo
    Member

    I can confirm this is no longer an issue, on the main branch at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions