bellockk / alphashape

Toolbox for constructing alpha shapes.
MIT License
258 stars 28 forks source link

Incompatible with shapely 2.0 #36

Closed HanatoK closed 1 year ago

HanatoK commented 1 year ago

Description

After upgrading to shapely 2.0, when I am trying to get the optimized alpha from a set of points, I encounter the following error:

Traceback (most recent call last):
  File "/home/hanatok/HDD/Documents/playground/python/test_alphashape/./test.py", line 5, in <module>
    alpha = 0.95 * alphashape.optimizealpha(points)
  File "/home/hanatok/mambaforge/lib/python3.10/site-packages/alphashape/optimizealpha.py", line 100, in optimizealpha
    if _testalpha(points, test_alpha):
  File "/home/hanatok/mambaforge/lib/python3.10/site-packages/alphashape/optimizealpha.py", line 41, in _testalpha
    return all([polygon.intersects(point) for point in points])
TypeError: 'MultiPoint' object is not iterable

After debugging I have found that the issue is related to the changes in shapely 2.0. If I apply the following patch:

diff --git a/alphashape/optimizealpha.py b/alphashape/optimizealpha.py
index 6c0b72f..a3391c2 100644
--- a/alphashape/optimizealpha.py
+++ b/alphashape/optimizealpha.py
@@ -37,7 +37,7 @@ def _testalpha(points: Union[List[Tuple[float]], np.ndarray], alpha: float):
     polygon = alphashape(points, alpha)
     if isinstance(polygon, shapely.geometry.polygon.Polygon):
         if not isinstance(points, MultiPoint):
-            points = MultiPoint(list(points))
+            points = MultiPoint(list(points)).geoms
         return all([polygon.intersects(point) for point in points])
     elif isinstance(polygon, trimesh.base.Trimesh):
         return len(polygon.faces) > 0 and all(

then it works as expected.

What I Did

The traceback is attached above.

import alphashape
import numpy as np

points = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]])
alpha = 0.95 * alphashape.optimizealpha(points)
hull = alphashape.alphashape(points, alpha)
hull_pts = hull.exterior.coords.xy

What I Expected

The above code runs without throwing the traceback.

Acceptance Criteria

No runtime exceptions.

ltjh123 commented 8 months ago

thank you,help me