Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Gridliner should handle offset central longitudes #2489

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/cartopy/mpl/gridliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import matplotlib.ticker as mticker
import matplotlib.transforms as mtrans
import numpy as np
import shapely
import shapely.geometry as sgeom

import cartopy
Expand Down Expand Up @@ -815,17 +816,16 @@ def update_artist(artist, renderer):
if isinstance(intersection, sgeom.LineString):
intersection = [intersection]
elif len(intersection.geoms) > 4:
# Gridline and map boundary are parallel and they
# intersect themselves too much it results in a
# multiline string that must be converted to a single
# linestring. This is an empirical workaround for a
# problem that can probably be solved in a cleaner way.
xy = np.append(
intersection.geoms[0].coords,
intersection.geoms[-1].coords,
axis=0,
)
intersection = [sgeom.LineString(xy)]
# If lines are parallel, there will be many intersections
# merge them to get only one for the calculations below
merged_line = shapely.line_merge(intersection)
if isinstance(merged_line, sgeom.MultiLineString):
# our merge still produced a multilinestring, so
# manually concatenate the original coordinates
xy = np.concatenate(
[inter.coords for inter in intersection.geoms], axis=0)
merged_line = shapely.LineString(xy)
intersection = [merged_line]
else:
intersection = intersection.geoms
tails = []
Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the "Known bug" text in the upper right axes has been removed because the right labels show up now.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions lib/cartopy/tests/mpl/test_gridliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,10 @@ def test_grid_labels():
ax.coastlines(resolution="110m")
ax.gridlines(draw_labels=True)

# Check that adding labels to Mercator gridlines gives an error.
# (Currently can only label PlateCarree gridlines.)
ax = fig.add_subplot(
3, 2, 2, projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines(resolution="110m")

ax.set_title('Known bug')
gl = ax.gridlines(crs=crs_pc, draw_labels=True)
gl.top_labels = False
gl.left_labels = False
Expand Down
Loading