|
5 | 5 |
|
6 | 6 | from matplotlib.path import Path |
7 | 7 | import numpy as np |
| 8 | +from numpy.testing import assert_array_equal |
8 | 9 | import pytest |
9 | 10 | import shapely.geometry as sgeom |
10 | 11 |
|
@@ -102,3 +103,32 @@ def test_non_polygon_path_closing(self, path): |
102 | 103 | closed_path = cpath._ensure_path_closed(path) |
103 | 104 | assert isinstance(closed_path, Path) |
104 | 105 | assert closed_path.vertices.size == 0 |
| 106 | + |
| 107 | + |
| 108 | +class Test_shapely_to_path: |
| 109 | + def test_polygon_with_multiple_interiors(self): |
| 110 | + exterior = sgeom.box(0, 0, 12, 12).exterior.coords |
| 111 | + interiors = [sgeom.box(1, 1, 2, 2, ccw=False).exterior.coords, |
| 112 | + sgeom.box(4, 4, 5, 6, ccw=False).exterior.coords, |
| 113 | + sgeom.box(8, 8, 9, 10, ccw=False).exterior.coords] |
| 114 | + poly = sgeom.Polygon(exterior, interiors) |
| 115 | + |
| 116 | + path = cpath.shapely_to_path(poly) |
| 117 | + |
| 118 | + rings = [poly.exterior, *poly.interiors] |
| 119 | + expected_vertices = np.concatenate([np.asarray(ring.coords) |
| 120 | + for ring in rings]) |
| 121 | + assert_array_equal(path.vertices, expected_vertices) |
| 122 | + |
| 123 | + # Each ring is 5 vertices long (a closed box), so the codes for |
| 124 | + # each ring should be MOVETO, LINETO, LINETO, LINETO, CLOSEPOLY. |
| 125 | + expected_codes = np.tile( |
| 126 | + [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY], |
| 127 | + len(rings)) |
| 128 | + assert_array_equal(path.codes, expected_codes) |
| 129 | + assert path.codes.dtype == Path.code_type |
| 130 | + |
| 131 | + # The path should round-trip back to an equivalent geometry. |
| 132 | + result = cpath.path_to_shapely(path) |
| 133 | + assert isinstance(result, sgeom.Polygon) |
| 134 | + assert result.equals(poly) |
0 commit comments