Skip to content

Commit

Permalink
FIX: Gridliner should handle offset central longitudes
Browse files Browse the repository at this point in the history
This merges parallel multi linestring geometries from an intersection
into a single linestring when possible. Before this, the line would start
in the middle of the axes and then go to the boundaries. The previous
logic would only take the start/end geometry points and would therefore
put the labels in the middle of the axes at "0" instead of on the edges.
  • Loading branch information
greglucas committed Dec 4, 2024
1 parent 6265416 commit cfbd978
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
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
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

0 comments on commit cfbd978

Please sign in to comment.