Skip to content

Commit

Permalink
Stop guessing whether Paths are polygons on new MPL.
Browse files Browse the repository at this point in the history
The main things that suffered from this are font glyphs, fixed in 2.0.1,
and contours, which were fixed with Matplotlib's new contouring code.
Legacy contouring was remove in 2.2.0.

Also, our own geos_to_path, which was just fixed.
  • Loading branch information
QuLogic committed Nov 8, 2018
1 parent c232860 commit 39a9748
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/cartopy/mpl/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from __future__ import (absolute_import, division, print_function)

import numpy as np
import matplotlib
from matplotlib.path import Path
import shapely.geometry as sgeom

Expand Down Expand Up @@ -163,15 +164,16 @@ def path_to_geos(path, force_ccw=False):
if len(path_verts) == 0:
continue

# XXX A path can be given which does not end with close poly, in that
# situation, we have to guess?
verts_same_as_first = np.all(path_verts[0, :] == path_verts[1:, :],
axis=1)
if all(verts_same_as_first):
geom = sgeom.Point(path_verts[0, :])
elif path_verts.shape[0] > 4 and path_codes[-1] == Path.CLOSEPOLY:
geom = sgeom.Polygon(path_verts[:-1, :])
elif path_verts.shape[0] > 3 and verts_same_as_first[-1]:
elif (matplotlib.__version__ < '2.2.0' and
# XXX A path can be given which does not end with close poly,
# in that situation, we have to guess?
path_verts.shape[0] > 3 and verts_same_as_first[-1]):
geom = sgeom.Polygon(path_verts)
else:
geom = sgeom.LineString(path_verts)
Expand Down
15 changes: 13 additions & 2 deletions lib/cartopy/tests/mpl/test_patch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2015 - 2017, Met Office
# (C) British Crown Copyright 2015 - 2018, Met Office
#
# This file is part of cartopy.
#
Expand All @@ -17,7 +17,9 @@

from __future__ import (absolute_import, division, print_function)

import matplotlib
from matplotlib.path import Path
import pytest
import shapely.geometry as sgeom

import cartopy.mpl.patch as cpatch
Expand All @@ -33,12 +35,21 @@ def test_empty_polyon(self):
assert [type(geom) for geom in geoms] == [sgeom.Point, sgeom.Point]
assert len(geoms) == 2

@pytest.skipif(matplotlib.__version__ < '2.2.0',
reason='Paths may not be closed with old Matplotlib.')
def test_non_polygon_loop(self):
p = Path([[0, 10], [170, 20], [-170, 30], [0, 10]],
codes=[1, 2, 2, 2])
geoms = cpatch.path_to_geos(p)
assert [type(geom) for geom in geoms] == [sgeom.MultiLineString]
assert len(geoms) == 1

def test_polygon_with_interior_and_singularity(self):
# A geometry with two interiors, one a single point.
p = Path([[0, -90], [200, -40], [200, 40], [0, 40], [0, -90],
[126, 26], [126, 26], [126, 26], [126, 26], [126, 26],
[114, 5], [103, 8], [126, 12], [126, 0], [114, 5]],
codes=[1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2])
codes=[1, 2, 2, 2, 79, 1, 2, 2, 2, 79, 1, 2, 2, 2, 79])
geoms = cpatch.path_to_geos(p)
assert [type(geom) for geom in geoms] == [sgeom.Polygon, sgeom.Point]
assert len(geoms[0].interiors) == 1

0 comments on commit 39a9748

Please sign in to comment.