Using geoviews tile sources offline?

Okay, here’s how I got it to work.

Preparing environment

  1. Create a new cartopy environment (or use your existing):
conda create -n cartopy_env python=3.10
  1. Install all the relevant packages:
conda install -c conda-forge geoviews cartopy cartopy_offlinedata
  1. Verify the offline shapefiles exist:
from pathlib import Path
import cartopy

data_dir = cartopy.config["pre_existing_data_dir"]
list((Path(data_dir) / "shapefiles" / "natural_earth" / "cultural").glob("*"))

You should see a long list:

[PosixPath('/Users/ahuang/miniconda3/envs/cartopy_env/share/cartopy/shapefiles/natural_earth/cultural/ne_110m_admin_0_countries.shx'), PosixPath('/Users/ahuang/miniconda3/envs/cartopy_env/share/cartopy/shapefiles/natural_earth/cultural/ne_50m_admin_0_sovereignty.shx'), PosixPath('/Users/ahuang/miniconda3/envs/cartopy_env/share/cartopy/shapefiles/natural_earth/cultural/ne_110m_admin_0_countries_lakes.shx'), PosixPath('/Users/ahuang/miniconda3/envs/cartopy_env/share/cartopy/shapefiles/natural_earth/cultural/ne_10m_urban_areas.shx'), PosixPath('/Users/ahuang/miniconda3/envs/cartopy_env/share/cartopy/shapefiles/natural_earth/cultural/ne_10m_roads.cpg'), PosixPath('/Users/ahuang/miniconda3/envs/cartopy_env/share/cartopy/shapefiles/natural_earth/cultural/ne_10m_roads.shp')...

Using geoviews offline

Test using geoviews and switch off internet connection; if the machine is sealed from the internet and you’re planning to view the output on that machine, be sure to set resources=INLINE, or else opening the HTML file will result in a blank page.

import geoviews as gv
from bokeh.resources import INLINE

gv.extension("bokeh")

coastline = gv.feature.coastline()
borders = gv.feature.borders()
world = (coastline * borders).opts(global_extent=True)

gv.save(world, "world.html", resources=INLINE)

You should see this output:

Changing the default data directory

If you are unsatisfied with the default data directory that the data was downloaded to, it can be changed!

Here, I move it inside my user directory.

mkdir /Users/ahuang/.cartopy/
mv /Users/ahuang/miniconda3/envs/cartopy_env/share/cartopy /Users/ahuang/.cartopy/

If we try to re-run the geoviews code above, you should see something like this:

/Users/ahuang/miniconda3/envs/cartopy_env/lib/python3.10/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/110m_physical/ne_110m_coastline.zip
  warnings.warn(f'Downloading: {url}', DownloadWarning)
...
urllib.error.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>

To fix, update cartopy.config:

import cartopy
import geoviews as gv
from bokeh.resources import INLINE

cartopy.config["pre_existing_data_dir"] = "/Users/ahuang/.cartopy/cartopy"
print(cartopy.config)

gv.extension("bokeh")

coastline = gv.feature.coastline()
borders = gv.feature.borders()
world = (coastline * borders).opts(global_extent=True)

gv.save(world, "world.html", resources=INLINE)
2 Likes