question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange behaviour using multiple tilelayers and control layer

See original GitHub issue

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

# Executed within a Jupyter notebook cell
import folium
mymap = folium.Map(location=(39.472997, -6.370755), zoom_start=10,
                  tiles='OpenStreetMap', attr='Javi')

another_tile = folium.TileLayer('cartodbdark_matter', attr='Javi')
another_tile.layer_name = "darker"
another_tile.add_to(mymap)

folium.map.LayerControl().add_to(mymap)

# If one leaves the comments 
# If one uncomments the next line, the map will not have any tile layer active by default
# mymap.render()

mymap

# If one does a save to html. I will save it with no default active tile layer
# mypath = '~'
# mymap.save(mypath)

Problem description

Hi, I found this issue while I was working in my maps in a Jupyter notebook and then saving the result as html.

The behavior I think is wrong (or weird) is that after executing the render function of the map, when I have multiple tile layers, and I try to show them again in in html or within a jupyter cell (I think both make usage of the render), all the layers are unchecked in the control layer, so I have to click on one of them.

So I share two screenshots: https://drive.google.com/open?id=1qbtwMI2ZlwzcYlk0LEqvMvaB_MtUzFHa https://drive.google.com/open?id=1V-T079qvUOeeol9IuZ4u9chAwWcSLqWr

Expected Output

So, the expected behavior is like the first time that render is called i.e. that one layer is checked by default in the control layer.

Output of folium.__version__

Working with: Python 3.6.7

appdirs==1.4.3 attrs==19.1.0 backcall==0.1.0 bleach==3.1.0 boto==2.49.0 boto3==1.9.205 botocore==1.12.205 branca==0.3.1 cached-property==1.5.1 cachetools==3.1.0 certifi==2019.3.9 chardet==3.0.4 Click==7.0 click-plugins==1.1.1 cligj==0.5.0 colour==0.1.5 cycler==0.10.0 decorator==4.4.0 defusedxml==0.5.0 dill==0.2.9 docutils==0.14 entrypoints==0.3 Fiona==1.8.6 folium==0.10.0 geopandas==0.4.1 google-auth==1.6.3 google-auth-oauthlib==0.3.0 googleads==18.1.0 googlemaps==3.0.2 gspread==3.1.0 gspread-dataframe==3.0.2 holoviews==1.12.1 httplib2==0.13.0 idna==2.8 ipykernel==5.1.0 ipython==7.4.0 ipython-genutils==0.2.0 ipywidgets==7.4.2 isodate==0.6.0 jedi==0.13.3 Jinja2==2.10.1 jmespath==0.9.4 joblib==0.13.2 jsonschema==3.0.1 jupyter==1.0.0 jupyter-client==5.2.4 jupyter-console==6.0.0 jupyter-core==4.4.0 jupyterlab==0.35.4 jupyterlab-server==0.2.0 kiwisolver==1.0.1 lxml==4.3.3 MarkupSafe==1.1.1 matplotlib==3.0.3 mistune==0.8.4 multiprocess==0.70.7 munch==2.3.2 mysql==0.0.2 mysqlclient==1.4.2.post1 nbconvert==5.4.1 nbformat==4.4.0 notebook==5.7.8 numpy==1.16.2 oauth2client==4.1.3 oauthlib==3.0.1 pandas==0.24.2 pandocfilters==1.4.2 param==1.9.0 parso==0.4.0 patsy==0.5.1 pexpect==4.7.0 pickleshare==0.7.5 polyline==1.3.2 prometheus-client==0.6.0 prompt-toolkit==2.0.9 psycopg2-binary==2.8.2 ptyprocess==0.6.0 pyasn1==0.4.5 pyasn1-modules==0.2.4 pycodestyle==2.5.0 Pygments==2.3.1 PyMySQL==0.9.3 pyparsing==2.4.0 pyproj==2.1.3 pyrsistent==0.14.11 python-dateutil==2.8.0 python-Levenshtein==0.12.0 pytz==2019.1 pyviz-comms==0.7.2 PyYAML==5.1 pyzmq==18.0.1 qtconsole==4.4.3 requests==2.21.0 requests-oauthlib==1.2.0 requests-toolbelt==0.9.1 rsa==4.0 Rtree==0.8.3 s3transfer==0.2.1 scikit-learn==0.21.2 scipy==1.2.1 seaborn==0.9.0 Send2Trash==1.5.0 Shapely==1.6.4.post2 six==1.12.0 SQLAlchemy==1.3.3 statsmodels==0.9.0 suds-jurko==0.6 terminado==0.8.2 testpath==0.4.2 tornado==6.0.2 tqdm==4.31.1 traitlets==4.3.2 uk-postcode-utils==1.0 urllib3==1.24.1 wcwidth==0.1.7 webencodings==0.5.1 widgetsnbextension==3.4.2 xlrd==1.2.0 xlwt==1.3.0 xmltodict==0.12.0 zeep==3.3.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

1reaction
Hanneskacommented, Jan 24, 2021

I have found a workaround that is not 100% perfect, but still ok. Eventhough, no base layer is selected by default (no visible tick in the LayerControl), you can still see a TileLayer after adding the LayerControl:

  1. Create a new TileLayer.
  2. Make it an exact duplicate (same parameters) of the one you want to be displayed after rendering.
  3. Set the parameter control=False.

This means that this TileLayer is not unchecked by the render method of LayerControl, see:

https://github.com/python-visualization/folium/blob/60ae79cd1f4f16fc48e206947c7cef870ea28b8e/folium/map.py#L156

A disadvantage is that no layer is selected in the LayerControl. Therefore, the user might not know at the outset which layer she/he is actually looking at.

1reaction
Conengmocommented, Sep 21, 2019

It seems like this is indeed a bug. When we render the layer control object we try to make sure we only check the first base layer. We do that by unchecking the others. Because the object has already been rendered it unchecks all base layers I suspect.

https://github.com/python-visualization/folium/blob/60ae79cd1f4f16fc48e206947c7cef870ea28b8e/folium/map.py#L161

We have to look into this and see how we can solve this. The render function of each class should be idempotent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange tile layer behavior while zooming on Safari
On Safari, the colors in the tile layer change when the map is zoomed ... Create layer control thing L.control.layers(baseMaps, overlayMaps) ...
Read more >
Manage hosted tile layers—ArcGIS Online Help | Documentation
When you publish a hosted tile layer from a hosted feature layer or hosted feature layer view, the tile layer inherits the visible...
Read more >
Solved: Add Tiles Layer with multi layers - Esri Community
I would like to create a Tile layer with several raster layers. Using ArcGIS Pro, I open my 2 rasters and I create...
Read more >
Using Leaflet Layer Controller - GIS Stack Exchange
This works by first initializing the map without a tile layer, then creating the background layers and adding only one of them to...
Read more >
Tiled Documentation - Read the Docs
The primary tool for editing tile layers is a stamp brush that ... But first, let's have a quick look at the various...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found