Skip to content

BUG: Gracefully handle calling orient() on empty polygon#2214

Merged
sgillies merged 4 commits intoshapely:mainfrom
sea-bass:patch-1
Feb 4, 2025
Merged

BUG: Gracefully handle calling orient() on empty polygon#2214
sgillies merged 4 commits intoshapely:mainfrom
sea-bass:patch-1

Conversation

@sea-bass
Copy link
Contributor

@sea-bass sea-bass commented Feb 4, 2025

I recently had a user run into an error they couldn't figure out. Upon analyzing the issue, I pinpointed it to a very simple use case of trying to call shapely.geometry.polygon.orient() on an empty Polygon as follows:

from shapely.geometry import Polygon
from shapely.geometry.polygon import orient
orient(Polygon())

This yields the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sebastian/python-virtualenvs/pyrobosim/lib/python3.12/site-packages/shapely/geometry/polygon.py", line 346, in orient
    if signed_area(ring) / s >= 0.0:
       ^^^^^^^^^^^^^^^^^
  File "/home/sebastian/python-virtualenvs/pyrobosim/lib/python3.12/site-packages/shapely/algorithms/cga.py", line 11, in signed_area
    xs, ys = np.vstack([coords, coords[1]]).T
                                ~~~~~~^^^
IndexError: index 1 is out of bounds for axis 0 with size 0

Here is a simple check on polygon emptiness to prevent this in the future!

@coveralls
Copy link

coveralls commented Feb 4, 2025

Pull Request Test Coverage Report for Build 13128809702

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 87.675%

Totals Coverage Status
Change from base Build 13060116458: 0.3%
Covered Lines: 2568
Relevant Lines: 2929

💛 - Coveralls

@sea-bass sea-bass changed the title Gracefully handle calling orient() on empty polygon BUG: Gracefully handle calling orient() on empty polygon Feb 4, 2025
Copy link
Contributor

@sgillies sgillies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sea-bass !

@sgillies sgillies added the bug label Feb 4, 2025
@sgillies sgillies merged commit d3c76fd into shapely:main Feb 4, 2025
26 checks passed
@sea-bass sea-bass deleted the patch-1 branch February 7, 2025 02:39
@theroggy theroggy added this to the 2.1 milestone Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants