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

41 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_reefs( 

5 ax, 

6 /, 

7 *, 

8 debug = __debug__, 

9 fov = None, 

10 linestyle = "solid", 

11 linewidth = 0.5, 

12 onlyValid = False, 

13 repair = False, 

14 resolution = "10m", 

15): 

16 """Add reefs to a Cartopy axis. 

17 

18 Parameters 

19 ---------- 

20 axis : cartopy.mpl.geoaxes.GeoAxesSubplot 

21 the axis to add the reefs to 

22 debug : bool, optional 

23 print debug messages 

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

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

26 occaisional MatPlotLib or Cartopy plotting errors when shapes much 

27 larger than the field-of-view are plotted 

28 linestyle : str, optional 

29 the style of the exterior of the reefs 

30 linewidth : float, optional 

31 the width of the exterior of the reefs 

32 onlyValid : bool, optional 

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

34 being called often) 

35 repair : bool, optional 

36 attempt to repair invalid Polygons 

37 resolution : str, optional 

38 the resolution of the reefs 

39 

40 Notes 

41 ----- 

42 This function uses `CSS4 named colours 

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

44 

45 Copyright 2017 Thomas Guymer [1]_ 

46 

47 References 

48 ---------- 

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

50 """ 

51 

52 # Import standard modules ... 

53 import os 

54 import urllib 

55 

56 # Import special modules ... 

57 try: 

58 import cartopy 

59 cartopy.config.update( 

60 { 

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

62 } 

63 ) 

64 except: 

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

66 try: 

67 import matplotlib 

68 matplotlib.rcParams.update( 

69 { 

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

71 "figure.dpi" : 300, 

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

73 "font.size" : 8, 

74 } 

75 ) 

76 except: 

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

78 try: 

79 import shapely 

80 import shapely.geometry 

81 except: 

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

83 

84 # Import sub-functions ... 

85 from .extract_polys import extract_polys 

86 

87 # ************************************************************************** 

88 

89 # Create suitable colour ... 

90 edgecolor = matplotlib.colors.to_rgba(matplotlib.colors.CSS4_COLORS["cornflowerblue"]) 

91 facecolor = matplotlib.colors.to_rgba(matplotlib.colors.CSS4_COLORS["aquamarine"]) 

92 if debug: 

93 print(f"INFO: \"reefs\" is ({edgecolor[0]:.6f},{edgecolor[1]:.6f},{edgecolor[2]:.6f},{edgecolor[3]:.6f}) and ({facecolor[0]:.6f},{facecolor[1]:.6f},{facecolor[2]:.6f},{facecolor[3]:.6f}).") 

94 

95 # Find file containing the shapes ... 

96 try: 

97 sfile = cartopy.io.shapereader.natural_earth( 

98 resolution = resolution, 

99 category = "physical", 

100 name = "reefs", 

101 ) 

102 except urllib.error.HTTPError: 

103 return 

104 if debug: 

105 print(f"INFO: \"reefs\" is \"{sfile}\".") 

106 

107 # Loop over records ... 

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

109 # Skip bad records ... 

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

111 continue 

112 

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

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

115 polys = [] 

116 for poly in extract_polys( 

117 record.geometry, 

118 onlyValid = onlyValid, 

119 repair = repair, 

120 ): 

121 if fov is None: 

122 polys.append(poly) 

123 continue 

124 if poly.disjoint(fov): 

125 continue 

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

127 

128 # Plot geometry ... 

129 ax.add_geometries( 

130 polys, 

131 cartopy.crs.PlateCarree(), 

132 edgecolor = edgecolor, 

133 facecolor = facecolor, 

134 linewidth = linewidth, 

135 linestyle = linestyle, 

136 )