Coverage for pyguymer3/geo/_add_lakes.py: 2%

42 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_lakes( 

5 ax, 

6 /, 

7 *, 

8 debug = __debug__, 

9 fov = None, 

10 onlyValid = False, 

11 repair = False, 

12 resolution = "10m", 

13): 

14 """Add lakes to a Cartopy axis. 

15 

16 Parameters 

17 ---------- 

18 axis : cartopy.mpl.geoaxes.GeoAxesSubplot 

19 the axis to add the lakes to 

20 debug : bool, optional 

21 print debug messages 

22 fov : None or shapely.geometry.polygon.Polygon, optional 

23 clip the plotted shapes to the provided field-of-view to work around 

24 occaisional MatPlotLib or Cartopy plotting errors when shapes much 

25 larger than the field-of-view are plotted 

26 onlyValid : bool, optional 

27 only add valid Polygons (checks for validity can take a while, if being 

28 being called often) 

29 repair : bool, optional 

30 attempt to repair invalid Polygons 

31 resolution : str, optional 

32 the resolution of the lakes 

33 

34 Notes 

35 ----- 

36 This function uses `CSS4 named colours 

37 <https://matplotlib.org/stable/gallery/color/named_colors.html>`_ . 

38 

39 Copyright 2017 Thomas Guymer [1]_ 

40 

41 References 

42 ---------- 

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

44 """ 

45 

46 # Import standard modules ... 

47 import os 

48 import urllib 

49 

50 # Import special modules ... 

51 try: 

52 import cartopy 

53 cartopy.config.update( 

54 { 

55 "cache_dir" : os.path.expanduser("~/.local/share/cartopy_cache"), 

56 } 

57 ) 

58 except: 

59 raise Exception("\"cartopy\" is not installed; run \"pip install --user Cartopy\"") from None 

60 try: 

61 import matplotlib 

62 matplotlib.rcParams.update( 

63 { 

64 "backend" : "Agg", # NOTE: See https://matplotlib.org/stable/gallery/user_interfaces/canvasagg.html 

65 "figure.dpi" : 300, 

66 "figure.figsize" : (9.6, 7.2), # NOTE: See https://github.com/Guymer/misc/blob/main/README.md#matplotlib-figure-sizes 

67 "font.size" : 8, 

68 } 

69 ) 

70 except: 

71 raise Exception("\"matplotlib\" is not installed; run \"pip install --user matplotlib\"") from None 

72 try: 

73 import shapely 

74 import shapely.geometry 

75 except: 

76 raise Exception("\"shapely\" is not installed; run \"pip install --user Shapely\"") from None 

77 

78 # Import sub-functions ... 

79 from .extract_polys import extract_polys 

80 

81 # ************************************************************************** 

82 

83 # Create suitable colour ... 

84 facecolor = matplotlib.colors.to_rgba(matplotlib.colors.CSS4_COLORS["lightblue"]) 

85 if debug: 

86 print(f"INFO: \"lakes\" is ({facecolor[0]:.6f},{facecolor[1]:.6f},{facecolor[2]:.6f},{facecolor[3]:.6f}).") 

87 

88 # Define names ... 

89 names = [ 

90 "lakes", 

91 "lakes_australia", 

92 "lakes_europe", 

93 "lakes_north_america", 

94 "lakes_pluvial", 

95 ] 

96 

97 # Loop over names ... 

98 for name in names: 

99 try: 

100 # Find file containing the shapes ... 

101 sfile = cartopy.io.shapereader.natural_earth( 

102 resolution = resolution, 

103 category = "physical", 

104 name = name, 

105 ) 

106 except urllib.error.HTTPError: 

107 continue 

108 if debug: 

109 print(f"INFO: \"{name}\" is \"{sfile}\".") 

110 

111 # Loop over records ... 

112 for record in cartopy.io.shapereader.Reader(sfile).records(): 

113 # Skip bad records ... 

114 if not hasattr(record, "geometry"): 

115 continue 

116 

117 # Create a list of Polygons to plot (taking in to account if the 

118 # user provided a field-of-view to clip them by) ... 

119 polys = [] 

120 for poly in extract_polys( 

121 record.geometry, 

122 onlyValid = onlyValid, 

123 repair = repair, 

124 ): 

125 if fov is None: 

126 polys.append(poly) 

127 continue 

128 if poly.disjoint(fov): 

129 continue 

130 polys.append(poly.intersection(fov)) 

131 

132 # Plot geometry ... 

133 ax.add_geometries( 

134 polys, 

135 cartopy.crs.PlateCarree(), 

136 edgecolor = "none", 

137 facecolor = facecolor, 

138 )