Skip to content

BUG: raise informative error for MultiPolygon in corner-based shape metrics (#739)#763

Merged
martinfleis merged 1 commit into
pysal:mainfrom
gaoflow:bug/corner-metrics-multipolygon
Jun 23, 2026
Merged

BUG: raise informative error for MultiPolygon in corner-based shape metrics (#739)#763
martinfleis merged 1 commit into
pysal:mainfrom
gaoflow:bug/corner-metrics-multipolygon

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #739.

Problem

corners, squareness and centroid_corner_distance crash or silently misbehave on MultiPolygon input. With include_interiors=False they use GeoSeries.exterior, which is undefined (None) for MultiPolygons. get_coordinates() then drops those rows, so the grouped result is shorter than the input index:

  • corners / squarenessValueError: Length mismatch (cryptic)
  • centroid_corner_distance → silently returns all-NaN (with RuntimeWarning: 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_multipolygons guard raises an actionable ValueError (pointing to geometry.explode(ignore_index=True) or include_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_multipolygon over the three metrics: asserts the informative ValueError, that include_interiors=True still works, and that the .explode() workaround works. Without the fix all three fail (two with the length-mismatch, one with the silent NaN); with it test_shape.py passes (20).

Disclosure: I used AI assistance (Claude) to help locate and draft this fix, under my direction and review.

…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

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.3%. Comparing base (4037c70) to head (2d906f9).
⚠️ Report is 162 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           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     
Files with missing lines Coverage Δ
momepy/shape.py 99.2% <100.0%> (+7.6%) ⬆️
momepy/tests/test_shape.py 100.0% <100.0%> (ø)

... and 17 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@martinfleis martinfleis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Certainly useful.

@martinfleis martinfleis merged commit 74d297e into pysal:main Jun 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueError when running "corners" and "squareness"

2 participants