folium icon indicating copy to clipboard operation
folium copied to clipboard

Strange behaviour using multiple tilelayers and control layer

Open jmaralc opened this issue 7 years ago • 7 comments

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.

jmaralc avatar Aug 16 '19 08:08 jmaralc

Hi @jmaralc, quick remark, I haven't tested your code, but it shouldn't be necessary to call render manually, if you don't do that, does that make a difference? Or why do you call render?

Conengmo avatar Aug 16 '19 09:08 Conengmo

You are right, it is not needed. I just did it because I think the source of the error is when the render is call several times. What I was doing was previsualizing the map in the jupyter cell and then exporting to html; and then it was when I was not able to see the tile layer because all were disabled. None the less one does not need to save as html, just the second time one calls render one get this behavior

jmaralc avatar Aug 16 '19 09:08 jmaralc

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.

Conengmo avatar Sep 21 '19 07:09 Conengmo

I also meet this problem, after I inspected the output html file, I try to make a hotfix:

  1. open that html file and find the row which includes the base map name you want to show at start. (e.g. "Open Street Map" in my case)
  2. then extract the hash id after that name
  3. find the row includes "name+id".remove(), then add "//" in front of it.
  4. save to another html (index_hotfix.html in my case)

Here is the code I used.

tile_layer_name_id = ''
with open('index.html') as f, open('index_hotfix.html', 'w') as f_out:
    for line in f:
        if not tile_layer_name_id:
            if '"Open Street Map" : tile_layer' in line:
                tile_layer_name_id = line.split(' : ')[-1].split(',')[0]
        else:
            if tile_layer_name_id + '.remove();' in line:
                line = '//' + line
        f_out.write(line)

scku208 avatar Nov 11 '20 02:11 scku208

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.

Hanneska avatar Jan 24 '21 21:01 Hanneska

Bumping to see the status of this since it seems to be an older issued that's still not resolved.

@Hanneska work around does fix the issue, but, as they said, it's not exactly 100% ideal. This avoids the tile layer being unchecked on initial render, but it results in no tile layer being selected inside of the layer control even though it's being shown on the map.

brandonwsims avatar Jul 23 '22 13:07 brandonwsims

+1, just lost a bunch of time before finding this.

colasri avatar Sep 15 '22 12:09 colasri