Skip to content

Commit

Permalink
Merge pull request #1136 from QuLogic/old-mpl
Browse files Browse the repository at this point in the history
Deprecate clip_path.
  • Loading branch information
ajdawson committed Oct 2, 2018
2 parents b3508a1 + 266a948 commit ab99497
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 83 deletions.
6 changes: 6 additions & 0 deletions docs/source/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Features
zooming in a figure. :data:`cartopy.feature.NaturalEarthFeature.scale` is
now read-only. (:pull:`1102`, :pull:`983`)

Deprecations
------------
* :func:`cartopy.mpl.clip_path.clip_path` has been deprecated. It is a simple
wrapper for Matplotlib's path clipping, so use that instead. You can replace
``clip_path(subject, clip_bbox)`` by ``subject.clip_to_bbox(clip_bbox)``.

--------


Expand Down
92 changes: 12 additions & 80 deletions lib/cartopy/mpl/clip_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,71 +17,12 @@

from __future__ import (absolute_import, division, print_function)

import warnings

import matplotlib.path as mpath
import numpy as np


def clip_path_python(subject, clip, point_inside_clip_path):
"""
Clip the subject path with the given clip path using the
Sutherland-Hodgman polygon clipping algorithm.
Parameters
----------
subject
The subject path to be clipped. Must be a simple, single
polygon path with straight line segments only.
clip
The clip path to use. Must be a simple, single
polygon path with straight line segments only.
point_inside_clip_path
A point which can be found inside the clip path polygon.
"""
inside_pt = point_inside_clip_path

output_verts = subject.vertices

for i in xrange(clip.vertices.shape[0] - 1):
clip_edge = clip.vertices[i:i + 2, :]
input_verts = output_verts
output_verts = []
inside = np.cross(clip_edge[1, :] - clip_edge[0, :],
inside_pt - clip_edge[0, :])

try:
s = input_verts[-1]
except IndexError:
break

for e in input_verts:
e_clip_cross = np.cross(clip_edge[1, :] - clip_edge[0, :],
e - clip_edge[0, :])
s_clip_cross = np.cross(clip_edge[1, :] - clip_edge[0, :],
s - clip_edge[0, :])

if np.sign(e_clip_cross) == np.sign(inside):
if np.sign(s_clip_cross) != np.sign(inside):
p = intersection_point(clip_edge[0, :], clip_edge[1, :],
e, s)
output_verts.append(p)
output_verts.append(e)
elif np.sign(s_clip_cross) == np.sign(inside):
p = intersection_point(clip_edge[0, :], clip_edge[1, :],
e, s)
output_verts.append(p)
s = e

if output_verts == []:
path = mpath.Path([[0, 0]], codes=[mpath.Path.MOVETO])
else:
# If the subject polygon was closed, then the return should be too.
if np.all(subject.vertices[0, :] == subject.vertices[-1, :]):
output_verts.append(output_verts[0])
path = mpath.Path(np.array(output_verts))
return path


def intersection_point(p0, p1, p2, p3):
"""
Returns
Expand Down Expand Up @@ -113,25 +54,16 @@ def intersection_point(p0, p1, p2, p3):
# Provide a clip_path function which clips the given path to the given Bbox.
# There is inbuilt mpl functionality with v1.2.1 and beyond, but we provide
# a shim here for older mpl versions.
if hasattr(mpath.Path, 'clip_to_bbox'):
def clip_path(subject, clip_bbox):
"""
Clip the given path to the given bounding box.
"""
return subject.clip_to_bbox(clip_bbox)
else:
def clip_path(subject, clip_bbox):
"""
Clip the given path to the given bounding box.
"""
# A shim on clip_path_python to support Bbox path clipping.

bbox_patch = bbox_to_path(clip_bbox)
bbox_center = ((clip_bbox.x0 + clip_bbox.x1) / 2,
(clip_bbox.y0 + clip_bbox.y1) / 2)
return clip_path_python(subject, bbox_patch, bbox_center)
def clip_path(subject, clip_bbox):
"""
Clip the given path to the given bounding box.
"""
warnings.warn("This method has been deprecated. "
"You can replace ``clip_path(subject, clip_bbox)`` by "
"``subject.clip_to_bbox(clip_bbox)``. "
"See the \"What's new\" section for v0.17.")
return subject.clip_to_bbox(clip_bbox)


def lines_intersect(p0, p1, p2, p3):
Expand Down
5 changes: 2 additions & 3 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import cartopy.crs as ccrs
import cartopy.feature
import cartopy.img_transform
from cartopy.mpl.clip_path import clip_path
import cartopy.mpl.feature_artist as feature_artist
import cartopy.mpl.patch as cpatch
from cartopy.mpl.slippy_image_artist import SlippyImageArtist
Expand Down Expand Up @@ -353,8 +352,8 @@ def draw(self, renderer=None, inframe=False):
self.autoscale_view()

if self.outline_patch.reclip or self.background_patch.reclip:
clipped_path = clip_path(self.outline_patch.orig_path,
self.viewLim)
clipped_path = self.outline_patch.orig_path.clip_to_bbox(
self.viewLim)
self.outline_patch._path = clipped_path
self.background_patch._path = clipped_path

Expand Down

0 comments on commit ab99497

Please sign in to comment.