Skip to content

Commit 964385d

Browse files
committed
Deprecate old lat/lon tick formatters
This is a re-creation of #1066, but with a completely new implementation using proper module-level deprecation.
1 parent fe23120 commit 964385d

2 files changed

Lines changed: 67 additions & 48 deletions

File tree

‎lib/cartopy/mpl/gridliner.py‎

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,6 @@
6060
)
6161

6262

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-
10663
class Gridliner(matplotlib.artist.Artist):
10764
def __init__(self, axes, crs, draw_labels=False, xlocator=None,
10865
ylocator=None, collection_kwargs=None,
@@ -1301,3 +1258,65 @@ def check_overlapping(self, label):
13011258
if overlapping:
13021259
self.set_visible(False)
13031260
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+
# ######################################################################################

‎lib/cartopy/tests/mpl/test_gridliner.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
from shapely import geos_version
1515

1616
import cartopy.crs as ccrs
17-
from cartopy.mpl import _MPL_311
17+
from cartopy.mpl import _MPL_311, gridliner
1818
from cartopy.mpl.geoaxes import GeoAxes
1919
from cartopy.mpl.gridliner import (
20-
LATITUDE_FORMATTER,
21-
LONGITUDE_FORMATTER,
2220
Gridliner,
2321
classic_formatter,
2422
classic_locator,
@@ -174,8 +172,10 @@ def test_grid_labels():
174172
gl.right_labels = True
175173
gl.xlines = False
176174
gl.xlocator = mticker.FixedLocator([-180, -45, 45, 180])
177-
gl.xformatter = LONGITUDE_FORMATTER
178-
gl.yformatter = LATITUDE_FORMATTER
175+
with pytest.warns(DeprecationWarning):
176+
gl.xformatter = gridliner.LONGITUDE_FORMATTER
177+
with pytest.warns(DeprecationWarning):
178+
gl.yformatter = gridliner.LATITUDE_FORMATTER
179179
gl.xlabel_style = {'color': 'red'}
180180
gl.xpadding = 10
181181
gl.ypadding = 15

0 commit comments

Comments
 (0)