Skip to content

Commit

Permalink
FIX: Handle non-earth bodies within Projections
Browse files Browse the repository at this point in the history
There were several places that initialized PlateCarree with
no globe. We want to use the same globe as the Projection we
are transforming between if the user doesn't pass one in explicitly.
  • Loading branch information
greglucas committed Nov 9, 2023
1 parent 0d58449 commit 3c8f7fa
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/cartopy/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ def __init__(self, central_longitude=-96.0, central_latitude=39.0,
lons[1:-1] = np.linspace(central_longitude - 180 + 0.001,
central_longitude + 180 - 0.001, n)

points = self.transform_points(PlateCarree(), lons, lats)
points = self.transform_points(PlateCarree(globe=globe), lons, lats)

self._boundary = sgeom.LinearRing(points)
mins = np.min(points, axis=0)
Expand Down
2 changes: 1 addition & 1 deletion lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ def gridlines(self, crs=None, draw_labels=False,
the Y axis.
"""
if crs is None:
crs = ccrs.PlateCarree()
crs = ccrs.PlateCarree(globe=self.projection.globe)
from cartopy.mpl.gridliner import Gridliner
gl = Gridliner(
self, crs=crs, draw_labels=draw_labels, xlocator=xlocs,
Expand Down
3 changes: 3 additions & 0 deletions lib/cartopy/mpl/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def __call__(self, value, pos=None):
ccrs.Mercator)):
raise TypeError("This formatter cannot be used with "
"non-rectangular projections.")
if source.globe != self._target_projection.globe:
# The transforms need to use the same globe
self._target_projection = ccrs.PlateCarree(globe=source.globe)
projected_value = self._apply_transform(value,
self._target_projection,
source)
Expand Down
8 changes: 8 additions & 0 deletions lib/cartopy/tests/crs/test_lambert_conformal.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def test_specific_lambert():
check_proj_params('lcc', crs, other_args)


def test_lambert_moon():
moon = ccrs.Globe(ellipse=None, semimajor_axis=1737400, semiminor_axis=1737400)
crs = ccrs.LambertConformal(globe=moon)
other_args = {'a=1737400', 'b=1737400', 'lat_0=39.0', 'lat_1=33', 'lat_2=45',
'lon_0=-96.0', 'x_0=0.0', 'y_0=0.0'}
check_proj_params('lcc', crs, other_args)


class Test_LambertConformal_standard_parallels:
def test_single_value(self):
crs = ccrs.LambertConformal(standard_parallels=[1.])
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion lib/cartopy/tests/mpl/test_gridliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

@pytest.mark.natural_earth
# Robinson projection is slightly better in Proj 6+.
@pytest.mark.mpl_image_compare(filename='gridliner1.png', tolerance=0.7)
@pytest.mark.mpl_image_compare(filename='gridliner1.png', tolerance=0.73)
def test_gridliner():
ny, nx = 2, 4

Expand Down Expand Up @@ -584,3 +584,13 @@ def test_gridliner_labels_zoom():
# After zoom, we may not be using all the available labels.
assert len(gl._all_labels) == 24
assert gl._labels == gl._all_labels[:20]


def test_gridliner_with_globe():
fig = plt.figure()
proj = ccrs.PlateCarree(globe=ccrs.Globe(semimajor_axis=12345))
ax = fig.add_subplot(1, 1, 1, projection=proj)
gl = ax.gridlines()
fig.draw_without_rendering()

assert gl in ax.artists

0 comments on commit 3c8f7fa

Please sign in to comment.