Skip to content

maintain the layer order (z level) when using the check boxes in layer control dropdown #767

Closed

Description

@Alcampopiano

Thank you for this amazing tool. Being able to create these types of visualizations in python is a game changer for me at work. Quick question:

I am able to create layers of polygons on the map using the GeoJson method. However, when I toggle the checkbox from "unchecked" to "checked" in the dropdown menu, the recently "checked" layer is placed on top of all other layers. So if there is a layer with popup properties (e.g., a set of markers), they can become covered up by the topmost polygon, and are then unclickable.

Is there a way to maintain the layer ordering so that the layers are always placed in the order that they were added to the map during creation? I'm sure this is possible, but I haven't been able to figure it out. Any tip would be appreciated.

Sincerely,
Allan

Activity

  1. Alcampopiano commented on Nov 13, 2017

    @Alcampopiano
    Author

    Here is a simple example and screenshot. This issue is that I cannot seem to get the geoJson layer to stay on the bottom so that the markers/heat stays on top:

    m = folium.Map(location=[43.45,-79.75], tiles='OpenStreetMap', zoom_start=12)
    
    # geoJson layer
    folium.GeoJson(
        geo_data,
        control=False,
        style_function=lambda feature: {
            'fillColor': colormap(some_dict[int(feature['id'])]),
        }
    ).add_to(m)
    
    # Another layer
    HeatMap(lats_lons, blur=1, gradient= {0.4: 'red', 0.65: 'red', 1: 'red'}, name='my_heatmap').add_to(m)
    
    folium.LayerControl().add_to(m)
    m
    

    heatmap_underneath_geojson

  2. Alcampopiano commented on Nov 27, 2017

    @Alcampopiano
    Author

    Hi guys,
    Any hints about keeping the heat map above the choropleth?

  3. Conengmo commented on Nov 28, 2017

    @Conengmo
    Member

    I think it has to do with Leaflet, not Folium. I tested combining a heatmap with a geojson polygon layer. The order in which you add them to the map does not matter. autoZIndex also does not seem to do anything. I wonder in what cases that option does work.

    If you look in the html, both are in the overlay pane. The heatmap is a canvas and gets a z-index of 100, while the geojson layer is a svg and gets a z-index of 200. This seems fixed in the leaflet css file. So to fix this you would need to look at leaflet, or maybe the leaflet.heat plugin that Folium uses for the heatmaps.

    A quick dirty fix is to append the following to your html file: <style>.leaflet-heatmap-layer{z-index:300 !important}</style> Any z-index higher than 200 does the trick. You can add it to a custom template as described in #86.

  4. ocefpaf commented on Nov 28, 2017

    @ocefpaf
    Member

    @Conengmo you are correct. I'll keep this open in case we can come up with an easier zorder for all objects in folium, without relying on leaflet. Not sure what would take to implement that in a sane way though 😄

  5. Alcampopiano commented on Nov 28, 2017

    @Alcampopiano
    Author

    Fantastic guys. Thanks very much for the info.

  6. added
    feature request
    on Apr 21, 2018
  7. Conengmo commented on Oct 12, 2018

    @Conengmo
    Member

    We have a keep_in_front method in Map that can be used to define which object should stay in front and in what order. See #840. I think that solves this issue. Feel free to add other info, we can reopen if needed.

  8. bfmcneill commented on Nov 29, 2018

    @bfmcneill

    keep_in_front is not working for me

    Am I implementing it wrong?

    ```python
    shp1 = folium.GeoJson(pipeline1, name='pipeline1', show=False).add_to(hmap)
    shp2 = folium.GeoJson(pipeline1, name='pipeline2', show=False).add_to(hmap)
    
    hmap_wide = HeatMap(rows,
                        min_opacity=0.2,
                        max_val=500,
                        radius=10, 
                        blur=15, 
                        max_zoom=1,
                        name='Inventory > 100')
    
    
    
    
    hmap.keep_in_front(shp1, shp2, hmap.add_child(hmap_wide))
    
    
    folium.LayerControl().add_to(hmap)

    with CSS hack

    with_hack

    without CSS hack

    with_out_hack

  9. Conengmo commented on Nov 30, 2018

    @Conengmo
    Member

    No, you've did it as intended. But I guess it doesn't work with heatmap. As described earlier in this thread the heatmap layer is on a different z-index, so apperently keep_in_front doesn't help there. When I made that function I made it with regular vector and raster layers.

  10. JavierParada commented on Dec 5, 2020

    @JavierParada

    How can we solve this same issue with HeatMapWithTime instead of HeatMap?

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

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions