Coverage for pyguymer3/geo/add_map_background.py: 5%

21 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def add_map_background( 

5 ax, 

6 /, 

7 *, 

8 debug = __debug__, 

9 extent = None, 

10 name = "natural-earth-1", 

11 resolution = "medium0512px", 

12): 

13 """Add an image of a map as a background to a Cartopy axis. 

14 

15 Parameters 

16 ---------- 

17 axis : cartopy.mpl.geoaxes.GeoAxesSubplot 

18 the axis to add the background image to 

19 debug : bool, optional 

20 print debug statements 

21 extent : list of floats 

22 for high-resolution images, save time by specifying the extent that is 

23 to be added 

24 name : str, optional 

25 the name of the image in the database 

26 resolution : str, optional 

27 the resolution of the image in the database 

28 

29 Notes 

30 ----- 

31 If the specified image cannot be found then a default map will be used 

32 instead. See the `Cartopy documentation of background_img() 

33 <https://scitools.org.uk/cartopy/docs/latest/matplotlib/geoaxes.html#cartopy.mpl.geoaxes.GeoAxes.background_img>`_ 

34 and the `Cartopy documentation of stock_img() 

35 <https://scitools.org.uk/cartopy/docs/latest/matplotlib/geoaxes.html#cartopy.mpl.geoaxes.GeoAxes.stock_img>`_ . 

36 

37 Copyright 2017 Thomas Guymer [1]_ 

38 

39 References 

40 ---------- 

41 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

42 """ 

43 

44 # Import standard modules ... 

45 import json 

46 import os 

47 

48 # ************************************************************************** 

49 

50 # Initialize trigger ... 

51 default = True 

52 

53 # Check if the environment variable has been defined ... 

54 if "CARTOPY_USER_BACKGROUNDS" in os.environ: 

55 # Determine JSON path and check it exists ... 

56 jpath = f'{os.environ["CARTOPY_USER_BACKGROUNDS"]}/images.json' 

57 if os.path.exists(jpath): 

58 # Load JSON and check keys exist ... 

59 with open(jpath, "rt", encoding = "utf-8") as fObj: 

60 info = json.load(fObj) 

61 if name in info: 

62 if resolution in info[name]: 

63 # Determine image path and check it exists ... 

64 ipath = f'{os.environ["CARTOPY_USER_BACKGROUNDS"]}/{info[name][resolution]}' 

65 if os.path.exists(ipath): 

66 default = False 

67 

68 # Draw background image ... 

69 if default: 

70 if debug: 

71 print("INFO: Drawing default background.") 

72 ax.stock_img() 

73 else: 

74 if debug: 

75 print("INFO: Drawing user-requested background.") 

76 ax.background_img(name = name, resolution = resolution, extent = extent)