From c29458fe172f96b3db7d33ac6de828ef2e971467 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sun, 23 Jan 2022 20:07:45 +0100 Subject: [PATCH] TST: fix array tests to use proper empty Polygon (#2274) --- geopandas/tests/test_array.py | 59 +++++++++++++++-------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/geopandas/tests/test_array.py b/geopandas/tests/test_array.py index 42023cd..2724b24 100644 --- a/geopandas/tests/test_array.py +++ b/geopandas/tests/test_array.py @@ -10,6 +10,7 @@ import shapely.affinity import shapely.geometry from shapely.geometry.base import CAP_STYLE, JOIN_STYLE import shapely.wkb +import shapely.wkt from shapely._buildcfg import geos_version import geopandas @@ -32,7 +33,7 @@ triangle_no_missing = [ shapely.geometry.Polygon([(random.random(), random.random()) for i in range(3)]) for _ in range(10) ] -triangles = triangle_no_missing + [shapely.geometry.Polygon(), None] +triangles = triangle_no_missing + [shapely.wkt.loads("POLYGON EMPTY"), None] T = from_shapely(triangles) points_no_missing = [ @@ -356,31 +357,8 @@ def test_predicates_vector_vector(attr, args): def test_unary_geo(attr): na_value = None - if attr == "boundary": - # pygeos returns None for empty geometries - if not compat.USE_PYGEOS: - # boundary raises for empty geometry - with pytest.raises(Exception): - T.boundary - - values = triangle_no_missing + [None] - A = from_shapely(values) - else: - values = triangles - A = T - - result = getattr(A, attr) - if attr == "exterior" and compat.USE_PYGEOS: - # TODO(pygeos) - # empty Polygon() has an exterior with shapely > 1.7, which gives - # empty LinearRing instead of None, - # but conversion to pygeos still results in empty GeometryCollection - expected = [ - getattr(t, attr) if t is not None and not t.is_empty else na_value - for t in values - ] - else: - expected = [getattr(t, attr) if t is not None else na_value for t in values] + result = getattr(T, attr) + expected = [getattr(t, attr) if t is not None else na_value for t in triangles] assert equal_geometries(result, expected) @@ -455,7 +433,14 @@ def test_binary_geo_scalar(attr): "has_z", # for is_ring we raise a warning about the value for Polygon changing pytest.param( - "is_ring", marks=pytest.mark.filterwarnings("ignore:is_ring:FutureWarning") + "is_ring", + marks=[ + pytest.mark.filterwarnings("ignore:is_ring:FutureWarning"), + pytest.mark.skipif( + not compat.SHAPELY_GE_17, + reason="is_ring on empty Polygon doesn't work in Shapely 1.6", + ), + ], ), ], ) @@ -473,11 +458,9 @@ def test_unary_predicates(attr): result = getattr(V, attr) - if attr == "is_simple" and (geos_version < (3, 8) or compat.USE_PYGEOS): + if attr == "is_simple" and geos_version < (3, 8): # poly.is_simple raises an error for empty polygon for GEOS < 3.8 # with shapely, pygeos always returns False for all GEOS versions - # But even for Shapely with GEOS >= 3.8, empty GeometryCollection - # returns True instead of False expected = [ getattr(t, attr) if t is not None and not t.is_empty else na_value for t in vals @@ -489,6 +472,9 @@ def test_unary_predicates(attr): else na_value for t in vals ] + # empty Linearring.is_ring gives False with Shapely < 2.0 + if compat.USE_PYGEOS and not compat.SHAPELY_GE_20: + expected[-2] = True else: expected = [getattr(t, attr) if t is not None else na_value for t in vals] assert result.tolist() == expected @@ -496,16 +482,23 @@ def test_unary_predicates(attr): # for is_ring we raise a warning about the value for Polygon changing @pytest.mark.filterwarnings("ignore:is_ring:FutureWarning") +@pytest.mark.skipif( + not compat.SHAPELY_GE_17, + reason="is_ring on empty Polygon doesn't work in Shapely 1.6", +) def test_is_ring(): g = [ shapely.geometry.LinearRing([(0, 0), (1, 1), (1, -1)]), shapely.geometry.LineString([(0, 0), (1, 1), (1, -1)]), shapely.geometry.LineString([(0, 0), (1, 1), (1, -1), (0, 0)]), shapely.geometry.Polygon([(0, 0), (1, 1), (1, -1)]), - shapely.geometry.Polygon(), + shapely.wkt.loads("POLYGON EMPTY"), None, ] - expected = [True, False, True, True, False, False] + expected = [True, False, True, True, True, False] + if not compat.USE_PYGEOS and not compat.SHAPELY_GE_20: + # empty polygon is_ring gives False with Shapely < 2.0 + expected[-2] = False result = from_shapely(g).is_ring @@ -525,7 +518,7 @@ def test_unary_float(attr): def test_geom_types(): cat = T.geom_type # empty polygon has GeometryCollection type - assert list(cat) == ["Polygon"] * (len(T) - 2) + ["GeometryCollection", None] + assert list(cat) == ["Polygon"] * (len(T) - 1) + [None] def test_geom_types_null_mixed():