I'm planning on using folium to plot a few maps but noticed that everytime I replot something folium goes back to the internet and fetch the map data. Is there a way to configure it to download all map data and use it offline?
Not really. The whole goal of folium is to leverage the base map using a Web Tile Service (WTS). (And in some cases even more than just the base map is downloaded.)
Maybe someone could implemente a cache system to save a certain area/zoom level, like phones do, to work offline. A PRs with such functionality would be welcomed :wink:
BTW, if you your goal is to produce static figures, take a look at cartopy.
Thanks @ocefpaf. I'm doing an evaluation of the mapping options ATM for Python and being interactive is one of my requisites. I'm looking at cartopy but so far there are several problems installing it.
I'm looking at cartopy but so far there are several problems installing it.
Do you use conda? If so try:
conda install -c ioos cartopy
Nope, using standard pip, and it seems that the problem is something with the underlying libraries. It installs fine. The system is Ubuntu 14.04.
Is this a feature that's coming or has it been dropped?
Is this a feature that's coming or has it been dropped?
Folium is a leaflet wrapper. Leaflet render map tiles from a server. Unless you set a local server for your tiles there is no way to display slippy maps without an internet connection and a map server.
There is a way to "hack" folium to work offline. I used what's described below and was able to get folium.Map() working and rendering a geojson of a country without being online.
How to do it:
_default_css and _default_jss links (lines 225-257) to hit something stored on you local network. The lines are in the element.py file. You'll need to run a few steps225-257 of element.pyelement.py to point to your local copy of these files.Warning If you make changes to element.py, make sure to save a copy of element.py somewhere else because if you update folium, your changes get overwritten.
Hello, would there be any way to use it offline?
@celebrimborOOO , I gave some instructions which worked for me above.
@linwoodc3 can you tell me where can I find element.py file?
I can't find it in below folder!
"C:\ProgramData\Anaconda2\Lib\site-packagesfolium"
@morteza3000 , I haven't tested this fix with the new files, but here is what you can try. You would now change folium.py:
folium.py. The lines you'll need to change are: https://github.com/python-visualization/folium/blob/f8a12a64db15a5f4b44058f52c436a741fdfdd13/folium/folium.py#L28-L52
You can find the location of your file by doing this:
pip install folium
import folium
import os
# see if the folium.py file is there
print(os.listdir("/".join((folium.__file__).split('/')[:-1])))
# get the path; copy this or navigate to location
print("/".join((folium.__file__).split('/')[:-1]))
Copy the output of the last print statement to navigate to that file. Now just change the _default_js and/or _default_css. And by change, I mean you need to enter new values for the CDN or website where the css or js files are on your local/internal network. If you don't have access to the css or js files whereever you're doing this, it probably won't work.
@linwoodc3 Thanks a lot, it works for me. is there any way to have base map (tiles) offline too?
For those like me who found this much later...
Here's a quick Folium extension that supports offline raster tile schemes in .mbtiles and standard {z}{x}{y} format. Offline means you rolled your own raster tiles.
I've been using TileMill to roll my own tiles.
@linwoodc3 's method works, but it's generally not advised to hack on your dependency code.
Instead this just overrides raster_layers.TileLayer._template, folium._default_js, and folium._default_css to point locally instead of the Leaflet CDNs.
Simple Usage:
Grab folium_offline.py
Change variables in main() to suit your needs
Add Folium code to make maps
Hi all!
Has anyone tried to do this in Dash? The html generated by @and-viceversa script works, but when I put it in an html.IFrame it doesn't show the map, just the marker that I put on it.
I'm having this issue too, but without IFrames. For some reason {z}{x}{y} works just fine offline, while .mbtiles only works offline in Edge.
@and-viceversa hey man, I'm using your script with generated tiles using tilemill but im getting
Leaflet.TileLayer.MBTiles.js:22 Fetch API cannot load file:///storage/git/folium_offline/data/tiles/your_tiles.mbtiles. URL scheme must be "http" or "https" for CORS request.
When I load index.html and its completely grey.
Any idea on this?
EDIT:
It's okay i figured it out, used tilesets in directory instead and it worked