Skip to content

Commit

Permalink
PERF: Fix double check of is_empty in MultiPolygon() constructor (bec…
Browse files Browse the repository at this point in the history
…ause truthiness implies that) (#1655)

Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
Lyle-Alloy and jorisvandenbossche committed Oct 8, 2023
1 parent efa4692 commit 3c3a839
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 2 additions & 5 deletions shapely/geometry/multipolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ def __new__(self, polygons=None):
return polygons

polygons = getattr(polygons, "geoms", polygons)
polygons = [
p
for p in polygons
if p and not (isinstance(p, polygon.Polygon) and p.is_empty)
]
# remove None and empty polygons from list of Polygons
polygons = [p for p in polygons if p]

L = len(polygons)

Expand Down
4 changes: 4 additions & 0 deletions shapely/tests/geometry/test_multipolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def test_multipolygon(self):
]
]

# None and empty polygons are dropped
geom_from_list_with_empty = MultiPolygon([p, None, Polygon()])
assert geom_from_list_with_empty == geom

# Or from a list of multiple polygons
geom_multiple_from_list = MultiPolygon([p, p])
assert len(geom_multiple_from_list.geoms) == 2
Expand Down

0 comments on commit 3c3a839

Please sign in to comment.