Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeated CustomIcon wipes information #2039

Open
dkrasne opened this issue Dec 7, 2024 · 3 comments
Open

Repeated CustomIcon wipes information #2039

dkrasne opened this issue Dec 7, 2024 · 3 comments

Comments

@dkrasne
Copy link

dkrasne commented Dec 7, 2024

Describe the bug

When using FeatureGroupSubGroup and setting the marker to a CustomIcon, the layer isn't generated; it also wipes any customization from other markers added prior to the FeatureGroupSubGroup, and any added afterwards don't appear either. HOWEVER, the problem actually lies with CustomIcon, rather than FeatureGroupSubGroup, because there's also an issue if I simply try to use the same CustomIcon for multiple markers.

To Reproduce

import pandas as pd
import folium
import numpy as np
from folium.plugins import MarkerCluster
from folium.plugins import FeatureGroupSubGroup

map_df = pd.DataFrame(
    columns=["Collection Type", "Institution Name", "Collection Name", "address", "latitude", "longitude"],
    data=[["Academic Library", "Amherst College", "Robert Frost Library", "61 Quadrangle Dr, Amherst, MA 01002, USA", 42.371830, -72.516988],
          ["Other Library/Archive", "Wendy's Subway", np.nan, "379 Bushwick Ave, Brooklyn, NY 11206, USA", 40.703840, -73.937981]]
)

zoom_start=3
start_loc = [map_df.latitude.mean(), map_df.longitude.mean()]

m = folium.Map(location=start_loc, 
               zoom_start=zoom_start,
               control_scale=True)

icon_url = 'https://pratt.darcykrasne.com/cba_bookarts_map/{}'.format
icon_image = icon_url('cba_mapicon.png')
icon_shadow_image = icon_url('cba_mapicon_shadow.png')
cba_icon = folium.CustomIcon(icon_image=icon_image,
                            icon_size=(20,20),
                            icon_anchor=(10,20),
                            shadow_image=icon_shadow_image,
                            shadow_size=(30,7),
                            shadow_anchor=(4,7)
                            )

folium.Marker(location=start_loc,
              icon=cba_icon, popup=folium.Popup("#1")).add_to(m) # If I were to generate the map at this point, everything would be okay. After this, anything added is problematic.

folium.Marker(location=[start_loc[0]-20,start_loc[1]-20],
              icon=cba_icon, 
              popup=folium.Popup("#2")).add_to(m) # If I generate the map at this point, there's already a problem: the second marker doesn't appear, and the popup for the first is empty.

mcluster = MarkerCluster(name="Collections", control=False)
m.add_child(mcluster)

for collection_type in map_df['Collection Type'].unique():
    subgroup = FeatureGroupSubGroup(mcluster, f'{collection_type}')
    m.add_child(subgroup)
    for idx, row in map_df.query(f'`Collection Type` == "{collection_type}"').iterrows():
        collection_type = row["Collection Type"]
        coordinates = [row["latitude"], row["longitude"]]
        popup_text = []
        for item in [row['Collection Name'],row['Institution Name'],row['address']]:
            if item is not np.nan:
                popup_text.append(item)
        popup_html = ""
        for item in popup_text:
            popup_html += item+'<br>'
        marker = folium.Marker(location=coordinates,
                               icon=cba_icon,
                               popup=folium.Popup(popup_html, min_width=100, max_width=500))
        marker.add_to(subgroup)

folium.LayerControl(collapsed=False).add_to(m)

folium.Marker(location=[start_loc[0]-40,start_loc[1]-40],
              icon=cba_icon, popup=folium.Popup(popup_html="#3")).add_to(m) # Also doesn't appear; I just added this for further demonstration.

m

Expected behavior

The customized markers should all be present on the map, with their popup information intact.

Environment (please complete the following information):

  • Browser N/A - running in VS Code
  • Jupyter Notebook
  • Python version: sys.version_info(major=3, minor=11, micro=0, releaselevel='final', serial=0)
  • folium version: 0.15.0
  • branca version: 0.7.0

Additional context

I think I've described everything above; let me know if you need more information.

Possible solutions

Workaround: Generate a new CustomIcon for each marker instead of reusing the same one (I tested it, and it works if you do this within the loop).

@hansthen
Copy link
Collaborator

@Conengmo

This seems to be a duplicate of #1885 (and some other issues). The underlying cause is that Branca/Folium only has containment relations but not reference relations. A Branca object can only be part of one parent object. While this is true for HTML constructs, it does not match the Leaflet object model which can have reference relations (it being javascript).

I have a few ideas on how we can solve this issue and also on how to make this backwards compatible with existing Folium client code. It will be a tricky change though. Do you have ideas on what we could do with this issue? Perhaps we can brainstorm on this issue in the discussion page?

@Conengmo
Copy link
Member

Very well put @hansthen. I'd love for us to be able to work with references. I did look into it before, but never came to a satisfying approach. I'd love to hear your ideas on this, doing that on the discussion page seems like a great idea.

@hansthen
Copy link
Collaborator

@Conengmo I added a new discussion to the discussions page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants