python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.86k stars 2.22k forks source link

Load locally generated tiles (e.g., gdal2tiles) in a Folium map #1754

Closed JosephKuchar closed 1 year ago

JosephKuchar commented 1 year ago

It's possible this is already implemented in Folium, and I just haven't been able to find anything in the documentation. If so, I apologise in advance!

Is it possible to load a local vector tile layer (for example, produced with something like gdal2tiles) on to a folium map? I'm trying to build a dashboard so that users can see some satellite imagery hosted on a local server. Most examples I come across assume the data is being queries through something like a wms, which is not currently something we have available.

Thanks!

Conengmo commented 1 year ago

Hi Joseph, I believe this is already possible!

Depending on your tiles some additional work may be necessary to get a right projection, but let's first check you can load your tiles.

JosephKuchar commented 1 year ago

Hi Conengmo,

Thanks for the help! So I tried using the TileLayer function, but it isn't working for me. I have the map loading, but without the raster layer, and without any errors showing. However, when I inspect the page and check the console in the dev tools, I see there are 404 errors associated with the tiles I'm trying to load. Essentially, the code is

m = Map(location=coords, zoom_start=12,)

TileLayer('PATH_TO_IMAGE/{z}/{x}/{y}.png', attr=attr, min_zoom=12, max_zoom=15).add_to(m)
m

But I see the path generating the 404 error is actually prepended with a localhost for some reason: Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8000/hub/PATH_TO_IMAGE/1/2/3.png (path above altered a bit, but that's the gist of it). This is all running in a Jupyter notebook on a Jupyter Hub on a remote machine, but I don't know why it's looking for the file at that location instead of the path I gave to it.

Thanks for your help!

Conengmo commented 1 year ago

maybe try to prepend a ./ or a file://, like for example 'file://PATH_TO_IMAGE/{z}/{x}/{y}.png'. Might be that that path needs to be absolute.

JosephKuchar commented 1 year ago

Unfortunately that hasn't worked. I've tried using os to get the absolute path and prepend it, I also copied the image folder to my current working directory so I could try just a ./, and have tried pre-pending with file://. Though it does do something. If I don't prepend the file://, then in the network tab I see those png's return a 404 error. If I do prepend file://, then there is never a response at all (so a request is sent, but no response code comes back at all).

JosephKuchar commented 1 year ago

So whatever the issue is, I don't think is Folium as much as it's Jupyter. I saved the folium map to html, and downloaded the html and tiles to my local machine where the tiles were properly generated. I'm still not sure what the fix is here, but it's probably nothing to do with Folium. Thanks!

Conengmo commented 1 year ago

Thanks for sharing your notes! I hope you find a solution. For now I'll close this issue. If you have additional information please do share it here for others who might encounter this problem.

JosephKuchar commented 1 year ago

So I did end up with a workaround solution, which was to create a simple python webserver with python -m http.server [PORTNUMBER] out of the folder where I generated my tiles, and then pass the url's rather than file paths to folium.