BUG: raise informative error for MultiPolygon in corner-based shape metrics (#739)#763
Merged
Merged
Conversation
…etrics (pysal#739) corners, squareness and centroid_corner_distance use GeoSeries.exterior when include_interiors=False, but exterior is undefined (None) for MultiPolygons. The dropped rows make the grouped result shorter than the input index, so corners/squareness raise a cryptic 'Length mismatch' and centroid_corner_distance silently returns all-NaN. Add a shared _check_no_multipolygons guard that raises an actionable ValueError pointing to explode() or include_interiors=True. Adds a parametrized test over the three metrics. Per the discussion on pysal#739 this takes the informative-error path; happy to extend to full MultiPolygon support instead if preferred.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #763 +/- ##
=======================================
+ Coverage 97.4% 98.3% +0.9%
=======================================
Files 26 28 +2
Lines 4328 4573 +245
=======================================
+ Hits 4214 4495 +281
+ Misses 114 78 -36
🚀 New features to boost your workflow:
|
martinfleis
approved these changes
Jun 23, 2026
martinfleis
left a comment
Member
There was a problem hiding this comment.
Thanks! Certainly useful.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #739.
Problem
corners,squarenessandcentroid_corner_distancecrash or silently misbehave on MultiPolygon input. Withinclude_interiors=Falsethey useGeoSeries.exterior, which is undefined (None) for MultiPolygons.get_coordinates()then drops those rows, so the grouped result is shorter than the input index:corners/squareness→ValueError: Length mismatch(cryptic)centroid_corner_distance→ silently returns all-NaN(withRuntimeWarning: Mean of empty slice)Fix
Per the discussion on #739, this takes the informative-error path rather than the design-gated full-MultiPolygon-support path. A shared
_check_no_multipolygonsguard raises an actionableValueError(pointing togeometry.explode(ignore_index=True)orinclude_interiors=True) in the three affected functions. This turns a cryptic crash / silent-NaN into a clear message. Happy to extend to full MultiPolygon support instead if that's preferred.Tests
Added a parametrized
test_corner_metrics_multipolygonover the three metrics: asserts the informativeValueError, thatinclude_interiors=Truestill works, and that the.explode()workaround works. Without the fix all three fail (two with the length-mismatch, one with the silent NaN); with ittest_shape.pypasses (20).Disclosure: I used AI assistance (Claude) to help locate and draft this fix, under my direction and review.