Adding Vector Map Underlays In Cartopy

metadata

Recently I decided that I wanted map backgrounds which worked quickly over a variety of scales; I did not want to have to load a 21,600 px × 10,800 px image just to get green and blue backgrounds for the south coast of England. To this end I wrote a Python function called pyguymer3.geo.add_map_underlay() which plots a variety of vector datasets from Natural Earth as well as the GeoJSON files from my previous blog post about vectorising the GLOBE dataset. It has the option too of plotting cultural datasets, such as railways and roads.

Below are three example images created by this function, showing a global extent, a local extent and an animation.

Download:
  1. 512 px × 288 px (0.1 Mpx; 172.7 KiB)
  2. 1,024 px × 576 px (0.6 Mpx; 574.6 KiB)
  3. 2,048 px × 1,152 px (2.4 Mpx; 1.8 MiB)
  4. 3,840 px × 2,160 px (8.3 Mpx; 3.3 MiB)
Download:
  1. 512 px × 512 px (0.3 Mpx; 365.3 KiB)
  2. 1,024 px × 1,024 px (1.0 Mpx; 1.0 MiB)
  3. 2,048 px × 2,048 px (4.2 Mpx; 2.7 MiB)
  4. 2,160 px × 2,160 px (4.7 Mpx; 1.8 MiB)
Download:
  1. 512 px × 512 px (0.3 Mpx; 2.0 MiB)
  2. 1,024 px × 1,024 px (1.0 Mpx; 2.8 MiB)
  3. 2,048 px × 2,048 px (4.2 Mpx; 6.3 MiB)
  4. 2,160 px × 2,160 px (4.7 Mpx; 7.3 MiB)

The three Python scripts used to generate the above images are included below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#!/usr/bin/env python3

# Use the proper idiom in the main module ...
# NOTE: See https://docs.python.org/3.12/library/multiprocessing.html#the-spawn-and-forkserver-start-methods
if __name__ == "__main__":
    # Import standard modules ...
    import os

    # Import special modules ...
    try:
        import cartopy
        cartopy.config.update(
            {
                "cache_dir" : os.path.expanduser("~/.local/share/cartopy_cache"),
            }
        )
    except:
        raise Exception("\"cartopy\" is not installed; run \"pip install --user Cartopy\"") from None
    try:
        import matplotlib
        matplotlib.rcParams.update(
            {
                       "backend" : "Agg",                                       # NOTE: See https://matplotlib.org/stable/gallery/user_interfaces/canvasagg.html
                    "figure.dpi" : 300,
                "figure.figsize" : (9.6, 7.2),                                  # NOTE: See https://github.com/Guymer/misc/blob/main/README.md#matplotlib-figure-sizes
                     "font.size" : 8,
            }
        )
        import matplotlib.pyplot
    except:
        raise Exception("\"matplotlib\" is not installed; run \"pip install --user matplotlib\"") from None

    # Import my modules ...
    try:
        import pyguymer3
        import pyguymer3.geo
        import pyguymer3.image
    except:
        raise Exception("\"pyguymer3\" is not installed; you need to have the Python module from https://github.com/Guymer/PyGuymer3 located somewhere in your $PYTHONPATH") from None

    # **************************************************************************

    # Create figure ...
    fg = matplotlib.pyplot.figure(figsize = (12.8, 7.2))

    # Create axis ...
    ax = pyguymer3.geo.add_axis(
        fg,
        add_coastlines = False,
    )

    # Configure axis ...
    pyguymer3.geo.add_NE_map_underlay(ax, cultural = False, linewidth = 0.0, resolution = "10m")

    # Configure figure ...
    fg.tight_layout()

    # Save figure ...
    fg.savefig("addMapUnderlay_global.png")
    matplotlib.pyplot.close(fg)

    # Optimize PNG ...
    pyguymer3.image.optimize_image("addMapUnderlay_global.png", strip = True)

              
You may also download “addMapUnderlay_global.py” directly or view “addMapUnderlay_global.py” on GitHub Gist (you may need to manually checkout the “main” branch).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#!/usr/bin/env python3

# Use the proper idiom in the main module ...
# NOTE: See https://docs.python.org/3.12/library/multiprocessing.html#the-spawn-and-forkserver-start-methods
if __name__ == "__main__":
    # Import special modules ...
    try:
        import matplotlib
        matplotlib.rcParams.update(
            {
                       "backend" : "Agg",                                       # NOTE: See https://matplotlib.org/stable/gallery/user_interfaces/canvasagg.html
                    "figure.dpi" : 300,
                "figure.figsize" : (9.6, 7.2),                                  # NOTE: See https://github.com/Guymer/misc/blob/main/README.md#matplotlib-figure-sizes
                     "font.size" : 8,
            }
        )
        import matplotlib.pyplot
    except:
        raise Exception("\"matplotlib\" is not installed; run \"pip install --user matplotlib\"") from None

    # Import my modules ...
    try:
        import pyguymer3
        import pyguymer3.geo
        import pyguymer3.image
    except:
        raise Exception("\"pyguymer3\" is not installed; you need to have the Python module from https://github.com/Guymer/PyGuymer3 located somewhere in your $PYTHONPATH") from None

    # **************************************************************************

    # Create figure ...
    fg = matplotlib.pyplot.figure(figsize = (7.2, 7.2))

    # Create axis ...
    ax = pyguymer3.geo.add_axis(
        fg,
        add_coastlines = False,
                  dist = 1000.0e3,
                   lat = +40.0,
                   lon =   0.0,
    )

    # Configure axis ...
    pyguymer3.geo.add_NE_map_underlay(ax, resolution = "10m")

    # Configure figure ...
    fg.tight_layout()

    # Save figure ...
    fg.savefig("addMapUnderlay_local.png")
    matplotlib.pyplot.close(fg)

    # Optimize PNG ...
    pyguymer3.image.optimize_image("addMapUnderlay_local.png", strip = True)

              
You may also download “addMapUnderlay_local.py” directly or view “addMapUnderlay_local.py” on GitHub Gist (you may need to manually checkout the “main” branch).
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

#!/usr/bin/env python3

# Use the proper idiom in the main module ...
# NOTE: See https://docs.python.org/3.12/library/multiprocessing.html#the-spawn-and-forkserver-start-methods
if __name__ == "__main__":
    # Import standard modules ...
    import os
    import shutil

    # Import special modules ...
    try:
        import cartopy
        cartopy.config.update(
            {
                "cache_dir" : os.path.expanduser("~/.local/share/cartopy_cache"),
            }
        )
    except:
        raise Exception("\"cartopy\" is not installed; run \"pip install --user Cartopy\"") from None
    try:
        import matplotlib
        matplotlib.rcParams.update(
            {
                       "backend" : "Agg",                                       # NOTE: See https://matplotlib.org/stable/gallery/user_interfaces/canvasagg.html
                    "figure.dpi" : 300,
                "figure.figsize" : (9.6, 7.2),                                  # NOTE: See https://github.com/Guymer/misc/blob/main/README.md#matplotlib-figure-sizes
                     "font.size" : 8,
            }
        )
        import matplotlib.pyplot
    except:
        raise Exception("\"matplotlib\" is not installed; run \"pip install --user matplotlib\"") from None

    # Import my modules ...
    try:
        import pyguymer3
        import pyguymer3.geo
        import pyguymer3.image
        import pyguymer3.media
    except:
        raise Exception("\"pyguymer3\" is not installed; you need to have the Python module from https://github.com/Guymer/PyGuymer3 located somewhere in your $PYTHONPATH") from None

    # **************************************************************************

    # Make sure that the output folder exists ...
    if not os.path.exists("png"):
        os.mkdir("png")

    # Initialize list ...
    frames = []

    # Loop over longitudes ...
    for ilon in range(-180, 180):
        # Deduce longitude ...
        lon = float(ilon)                                                       # [°]

        # Deduce filename, append it to list and skip this longitude if it
        # already exists ...
        frame = f"png/addMapUnderlay_animation_lon={lon:+08.3f}.png"
        frames.append(frame)
        if os.path.exists(frame):
            continue

        print(f"Making \"{frame}\" ...")

        # Create figure ...
        fg = matplotlib.pyplot.figure(figsize = (7.2, 7.2))

        # Create axis ...
        ax = pyguymer3.geo.add_axis(
            fg,
            add_coastlines = False,
                       lat = 51.5,
                       lon = lon,
        )

        # Configure axis ...
        pyguymer3.geo.add_NE_map_underlay(ax, cultural = False, linewidth = 0.0, resolution = "50m")

        # Configure figure ...
        fg.tight_layout()

        # Save figure ...
        fg.savefig(frame)
        matplotlib.pyplot.close(fg)

        # Optimize PNG ...
        pyguymer3.image.optimize_image(frame, strip = True)

    # **************************************************************************

    print("Making \"addMapUnderlay_animation.mp4\" ...")

    # Save 25fps MP4 ...
    vname = pyguymer3.media.images2mp4(
        frames,
    )
    shutil.move(vname, "addMapUnderlay_animation.mp4")

    # **************************************************************************

    print("Making \"addMapUnderlay_animation.webp\" ...")

    # Save 25fps WEBP ...
    pyguymer3.media.images2webp(
        frames,
        "addMapUnderlay_animation.webp",
    )

    # **************************************************************************

    # Set maximum sizes ...
    # NOTE: By inspection, the PNG frames are 2,160 px tall/wide.
    maxSizes = [512, 1024, 2048]                                                # [px]

    # Loop over maximum sizes ...
    for maxSize in maxSizes:
        print(f"Making \"addMapUnderlay_animation{maxSize:04d}px.mp4\" ...")

        # Save 25fps MP4 ...
        vname = pyguymer3.media.images2mp4(
            frames,
             screenWidth = maxSize,
            screenHeight = maxSize,
        )
        shutil.move(vname, f"addMapUnderlay_animation{maxSize:04d}px.mp4")

        # **********************************************************************

        print(f"Making \"addMapUnderlay_animation{maxSize:04d}px.webp\" ...")

        # Save 25fps WEBP ...
        pyguymer3.media.images2webp(
            frames,
            f"addMapUnderlay_animation{maxSize:04d}px.webp",
             screenWidth = maxSize,
            screenHeight = maxSize,
        )

              
You may also download “addMapUnderlay_animation.py” directly or view “addMapUnderlay_animation.py” on GitHub Gist (you may need to manually checkout the “main” branch).

Enjoy!