|
60 | 60 | ) |
61 | 61 |
|
62 | 62 |
|
63 | | -def _lon_hemisphere(longitude): |
64 | | - """Return the hemisphere (E, W or '' for 0) for the given longitude.""" |
65 | | - # Wrap the longitude to the range -180 to 180, keeping positive 180s |
66 | | - lon_wrapped = ((longitude + 180) % 360) - 180 |
67 | | - longitude = 180 if (longitude > 0 and lon_wrapped == -180) else lon_wrapped |
68 | | - if longitude > 0: |
69 | | - hemisphere = 'E' |
70 | | - elif longitude < 0: |
71 | | - hemisphere = 'W' |
72 | | - else: |
73 | | - hemisphere = '' |
74 | | - return hemisphere |
75 | | - |
76 | | - |
77 | | -def _lat_hemisphere(latitude): |
78 | | - """Return the hemisphere (N, S or '' for 0) for the given latitude.""" |
79 | | - if latitude > 0: |
80 | | - hemisphere = 'N' |
81 | | - elif latitude < 0: |
82 | | - hemisphere = 'S' |
83 | | - else: |
84 | | - hemisphere = '' |
85 | | - return hemisphere |
86 | | - |
87 | | - |
88 | | -def _east_west_formatted(longitude, num_format='g'): |
89 | | - hemisphere = _lon_hemisphere(longitude) |
90 | | - return f'{abs(longitude):{num_format}}\N{Degree Sign}{hemisphere}' |
91 | | - |
92 | | - |
93 | | -def _north_south_formatted(latitude, num_format='g'): |
94 | | - hemisphere = _lat_hemisphere(latitude) |
95 | | - return f'{abs(latitude):{num_format}}\N{Degree Sign}{hemisphere}' |
96 | | - |
97 | | - |
98 | | -#: A formatter which turns longitude values into nice longitudes such as 110W |
99 | | -LONGITUDE_FORMATTER = mticker.FuncFormatter(lambda v, pos: |
100 | | - _east_west_formatted(v)) |
101 | | -#: A formatter which turns longitude values into nice longitudes such as 45S |
102 | | -LATITUDE_FORMATTER = mticker.FuncFormatter(lambda v, pos: |
103 | | - _north_south_formatted(v)) |
104 | | - |
105 | | - |
106 | 63 | class Gridliner(matplotlib.artist.Artist): |
107 | 64 | def __init__(self, axes, crs, draw_labels=False, xlocator=None, |
108 | 65 | ylocator=None, collection_kwargs=None, |
@@ -1301,3 +1258,65 @@ def check_overlapping(self, label): |
1301 | 1258 | if overlapping: |
1302 | 1259 | self.set_visible(False) |
1303 | 1260 | return overlapping |
| 1261 | + |
| 1262 | + |
| 1263 | +# ###################################################################################### |
| 1264 | +# Deprecated formatters and their helpers. |
| 1265 | + |
| 1266 | +def _lon_hemisphere(longitude): |
| 1267 | + """Return the hemisphere (E, W or '' for 0) for the given longitude.""" |
| 1268 | + # Wrap the longitude to the range -180 to 180, keeping positive 180s |
| 1269 | + lon_wrapped = ((longitude + 180) % 360) - 180 |
| 1270 | + longitude = 180 if (longitude > 0 and lon_wrapped == -180) else lon_wrapped |
| 1271 | + if longitude > 0: |
| 1272 | + hemisphere = 'E' |
| 1273 | + elif longitude < 0: |
| 1274 | + hemisphere = 'W' |
| 1275 | + else: |
| 1276 | + hemisphere = '' |
| 1277 | + return hemisphere |
| 1278 | + |
| 1279 | + |
| 1280 | +def _lat_hemisphere(latitude): |
| 1281 | + """Return the hemisphere (N, S or '' for 0) for the given latitude.""" |
| 1282 | + if latitude > 0: |
| 1283 | + hemisphere = 'N' |
| 1284 | + elif latitude < 0: |
| 1285 | + hemisphere = 'S' |
| 1286 | + else: |
| 1287 | + hemisphere = '' |
| 1288 | + return hemisphere |
| 1289 | + |
| 1290 | + |
| 1291 | +def _east_west_formatted(longitude, num_format='g'): |
| 1292 | + hemisphere = _lon_hemisphere(longitude) |
| 1293 | + return f'{abs(longitude):{num_format}}\N{Degree Sign}{hemisphere}' |
| 1294 | + |
| 1295 | + |
| 1296 | +def _north_south_formatted(latitude, num_format='g'): |
| 1297 | + hemisphere = _lat_hemisphere(latitude) |
| 1298 | + return f'{abs(latitude):{num_format}}\N{Degree Sign}{hemisphere}' |
| 1299 | + |
| 1300 | + |
| 1301 | +def __getattr__(name): |
| 1302 | + match name: |
| 1303 | + case 'LONGITUDE_FORMATTER': |
| 1304 | + warnings.warn( |
| 1305 | + "The LONGITUDE_FORMATTER module-level attribute was deprecated in " |
| 1306 | + "Cartopy 0.26. Use LongitudeFormatter instead.", |
| 1307 | + DeprecationWarning, |
| 1308 | + stacklevel=2) |
| 1309 | + # A formatter which turns longitude values into nice longitudes such as 110W |
| 1310 | + return mticker.FuncFormatter(lambda v, pos: _east_west_formatted(v)) |
| 1311 | + case 'LATITUDE_FORMATTER': |
| 1312 | + warnings.warn( |
| 1313 | + "The LATITUDE_FORMATTER module-level attribute was deprecated in " |
| 1314 | + "Cartopy 0.26. Use LatitudeFormatter instead.", |
| 1315 | + DeprecationWarning, |
| 1316 | + stacklevel=2) |
| 1317 | + # A formatter which turns longitude values into nice longitudes such as 45S |
| 1318 | + return mticker.FuncFormatter(lambda v, pos: _north_south_formatted(v)) |
| 1319 | + raise AttributeError(f"module {Gridliner.__module__!r} has no attribute {name!r}") |
| 1320 | + |
| 1321 | +# Remove everything between these comments when removing the deprecation. |
| 1322 | +# ###################################################################################### |
0 commit comments