Using Ordnance Survey Images As Backgrounds In Cartopy

metadata
  • keywords:
    • software
    • ordnancesurvey
    • python
    • cartopy
    • matplotlib
    • PIL
  • published:
  • updated:
  • Atom Feed

Previously I have posted about Replacing Cartopy’s Background Image and Adding Background Images Of Elevation (to Cartopy). These two previous efforts were relatively easy because the sets of background images that I was adding were all global, i.e., they were equirectangular images of the whole of the planet. Recently I wanted to have background images in my plots using the following free Ordnance Survey datasets:

This presented a problem because, firstly, the images do not have global extent and, secondly, the images do not use the equirectangular projection. Therefore, they need to be added to a plot using the ax.imshow() method in MatPlotLib with a custom extent and transform, rather than by using the (more user friendly) ax.background_img() method in Cartopy. I decided to write some Python scripts to convert the images into PNG images with sidecar JSON files of their extents (for easy usage later).

The following articles are worth a read too:

Firstly, I wrote the following three Python scripts to save the images contained within the three ZIP files as PNG images and to generate the sidecar JSON files.

To view this source code snippet without JavaScript you can either download “background-OS-images-miniscale.py” directly or view “background-OS-images-miniscale.py” on GitHub Gist.
To view this source code snippet without JavaScript you can either download “background-OS-images-overview.py” directly or view “background-OS-images-overview.py” on GitHub Gist.
To view this source code snippet without JavaScript you can either download “background-OS-images-raster.py” directly or view “background-OS-images-raster.py” on GitHub Gist.

These are the three sidecar JSON files that were generated. The JSON files are very simple: they just give the name of the colour PNG image, the name of the greyscale PNG image and the extent of the PNG images on the Ordnance Survey National Grid.

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

Secondly, as a demonstration of how to use these generated PNG images and JSON files, I wrote the following three Python scripts to make an example plot for each free Ordnance Survey dataset.

To view this source code snippet without JavaScript you can either download “example-background-OS-image-miniscale.py” directly or view “example-background-OS-image-miniscale.py” on GitHub Gist.
To view this source code snippet without JavaScript you can either download “example-background-OS-image-overview.py” directly or view “example-background-OS-image-overview.py” on GitHub Gist.
To view this source code snippet without JavaScript you can either download “example-background-OS-image-raster.py” directly or view “example-background-OS-image-raster.py” on GitHub Gist.

Below are the three example plots (showing radii around Derby Train Station) using the three free Ordnance Survey datasets.

Download:
  1. 512x447 (167.5 KiB)
  2. 1,024x893 (451.2 KiB)
  3. 1,730x1,509 (853.3 KiB)
Download:
  1. 512x447 (95.7 KiB)
  2. 1,024x893 (197.8 KiB)
  3. 1,730x1,509 (284.3 KiB)
Download:
  1. 512x447 (252.3 KiB)
  2. 1,024x893 (830.3 KiB)
  3. 1,730x1,509 (1.7 MiB)

I believe that the above examples (using the ax.imshow() method) are not too onerous for the user and I hope that this little project means that I will be using more relevant background images in some of my plots in future.