bellockk / alphashape

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

Cannot reproduce the example given in the documentation. #49

Closed MCruzReyes closed 3 months ago

MCruzReyes commented 5 months ago

Description

If I run the code given in the documentation:

points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
          (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]

fig, ax = plt.subplots()
ax.scatter(*zip(*points_2d))
plt.show()

alpha_shape = alphashape.alphashape(points_2d, 0.)
alpha_shape

fig, ax = plt.subplots()
ax.scatter(*zip(*points_2d))
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.5))
plt.show()

I get the following error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_19312/2363699982.py in <module>
     12 fig, ax = plt.subplots()
     13 ax.scatter(*zip(*points_2d))
---> 14 ax.add_patch(PolygonPatch(alpha_shape, alpha=0.5))
     15 plt.show()

~\AppData\Roaming\Python\Python39\site-packages\descartes\patch.py in PolygonPatch(polygon, **kwargs)
     85 
     86     """
---> 87     return PathPatch(PolygonPath(polygon), **kwargs)

~\AppData\Roaming\Python\Python39\site-packages\descartes\patch.py in PolygonPath(polygon)
     60                 "A polygon or multi-polygon representation is required")
     61 
---> 62     vertices = concatenate([
     63         concatenate([asarray(t.exterior)[:, :2]] +
     64                     [asarray(r)[:, :2] for r in t.interiors])

~\AppData\Roaming\Python\Python39\site-packages\descartes\patch.py in <listcomp>(.0)
     61 
     62     vertices = concatenate([
---> 63         concatenate([asarray(t.exterior)[:, :2]] +
     64                     [asarray(r)[:, :2] for r in t.interiors])
     65         for t in polygon])

IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed

Do you know why I am getting this error?

AlexanderJCS commented 3 months ago

Hi MCruzReyes,

I found this Stack Overflow article describing the same issue: https://stackoverflow.com/questions/75573668/alphashape-and-polygonpatch-basic-example-doesnt-work-why

There is a mismatch between the version of shapely Descartes is expecting versus what is installed with Alphashape. You can edit the Descartes installation itself to fix the issue.