Background Images Of Elevation

  • keywords:
    • software
    • python
    • cartopy
    • matplotlib
    • numpy
    • requests
    • globe
  • published:
  • updated:
  • Atom Feed

This post is a follow up to my previous post about Replacing Cartopy’s Background Image. As discussed in the previous post, Cartopy’s default background image can be replaced with whatever you want: you just need to create a folder of PNG images with a JSON database file that describes them. To use this folder within your scripts you just need to set the environment variable CARTOPY_USER_BACKGROUNDS either by explicitly setting it in the shell before you call Python or by overriding it within Python by running something like os.environ["CARTOPY_USER_BACKGROUNDS"] = "/path/to/background/images/folder".

Recently I wanted to create some maps using elevation data and I came across the Global Land One-km Base Elevation (GLOBE) project, which is a 30-arc-second (1-km) gridded, quality-controlled global Digital Elevation Model (DEM). Their dataset is provided as a ZIP file containing a bunch of binary signed 16-bit integer tiles. To create images of the world I needed to load up all the tiles into RAM simultaneously (and then optionally clip them) to save them as a single PNG image.

The script below downloads the ZIP file (if it is missing) and then loops over pairs of elevations. For each elevation pair, such as “0m and 3000m”, it creates an array of the entire world and populates it with the elevation data from the tiles in the ZIP file. This array is then clipped to be between 0m and 3000m before being saved as a PNG image. Finally, the script creates smaller PNG images to allow quicker drawing before it creates the JSON database file that describes the whole folder.

To view this source code snippet without JavaScript you can either download “” directly or view “” on GitHub Gist.

Be careful using the script as it takes a long time and uses a lot of RAM to generate the initial PNG images before they are downsized. The GLOBE dataset in the ZIP file is 43200x21600 (which equates to a 889.9 mega-pixel image!). As a raw 2D 16-bit array that is 1.73 GiB of RAM; as a raw 3-channel image that is 2.61 GiB of RAM. On my Core i7 NAS it took around 1 hour 35 minutes to generate each initial PNG image - given the RAM usage (at least 4.34 GiB) I decided to not parallelise this script using multiprocessing. The JSON database file that is created is below.

To view this source code snippet without JavaScript you can either download “background-images-of-elevation-images.json” directly or view “background-images-of-elevation-images.json” on GitHub Gist.

The PNG file that it creates for the elevation pair “0m and 3000m”, is shown below.

  1. 512x256 (92.6 KiB)
  2. 1,024x512 (295.5 KiB)
  3. 2,048x1,024 (973.1 KiB)
  4. 4,096x2,048 (3.2 MiB)
  5. 8,192x4,096 (10.8 MiB)
  6. 43,200x21,600 (74.4 MiB)

The resulting folder of PNG images and JSON database file is entirely compatible with my function pyguymer3.add_map_background() - enjoy!