Skip to content

Commit 8ae4e9e

Browse files
committed
test _determine_intersection
1 parent 53ed369 commit 8ae4e9e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

mplotutils/_cartopy_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def _determine_intersection(polygon, xy1, xy2):
479479
arr = np.atleast_2d(arr)
480480
elif isinstance(intersection, shapely.geometry.LineString):
481481
if intersection.is_empty:
482-
return np.array([])
482+
return np.array([[]])
483483
else:
484484
return np.array(intersection.coords)
485485
else:

mplotutils/tests/test_mapticklabels.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cartopy.crs as ccrs
22
import numpy as np
33
import pytest
4+
import shapely
45

56
import mplotutils as mpu
67

@@ -109,3 +110,46 @@ def test_xyticklabels_not_on_map():
109110

110111
# assert ax.texts[0].get_text() == "60°E"
111112
# assert ax.texts[-1].get_text() == "60°W"
113+
114+
115+
def test_determine_intersection():
116+
117+
box = shapely.box(0, 0, 1, 1)
118+
119+
# case 0 -> the expected two points top & bottom
120+
121+
a = shapely.Point((0.5, -0.5))
122+
b = shapely.Point((0.5, 1.5))
123+
124+
result = mpu._cartopy_utils._determine_intersection(box, a, b)
125+
expected = np.array([[0.5, 0.0], [0.5, 1.0]])
126+
127+
np.testing.assert_allclose(result, expected)
128+
129+
# case 1 -> only one intersection (not sure how this would happen)
130+
131+
b = shapely.Point((0.5, 0.5))
132+
133+
result = mpu._cartopy_utils._determine_intersection(box, a, b)
134+
expected = np.array([[0.5, 0.0]])
135+
np.testing.assert_allclose(result, expected)
136+
137+
# case 2 -> intersection along a polygon edge
138+
139+
b = shapely.Point((1, 1.5))
140+
a = shapely.Point((1, -0.5))
141+
142+
result = mpu._cartopy_utils._determine_intersection(box, a, b)
143+
expected = np.array([[1.0, 0.0], [1.0, 1.0]])
144+
145+
np.testing.assert_allclose(result, expected)
146+
147+
# case 3 -> no intersection
148+
149+
a = shapely.Point((1.5, -0.5))
150+
b = shapely.Point((1.5, 1.5))
151+
result = mpu._cartopy_utils._determine_intersection(box, a, b)
152+
153+
expected = np.array([[]])
154+
155+
np.testing.assert_allclose(result, expected)

0 commit comments

Comments
 (0)