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 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.
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.
The PNG file that it creates for the elevation pair "0m and 3000m", is shown below.
The resulting folder of PNG images and JSON database file is entirely compatible with my function
pyguymer.add_map_background() - enjoy!