Alphashape和PolygonPatch:基本示例不起作用。为什么?

huangapple go评论101阅读模式
英文:

Alphashape and PolygonPatch: basic example doesn't work. Why?

问题

抱歉,我只会为您提供代码的翻译。以下是您提供的代码的翻译:

  1. 我尝试使用来自[此处](https://alphashape.readthedocs.io/_/downloads/en/latest/pdf/)的Alphashape包
  2. 尽管我已经正确复制并粘贴了初始页面上的示例但我遇到以下错误

File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py",第87行,在PolygonPatch
return PathPatch(PolygonPath(polygon), **kwargs)
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py",第62行,在PolygonPath
vertices = concatenate([
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py",第63行,在 concatenate([asarray(t.exterior)[:, :2]] +
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed

  1. 我正在尝试的代码:
  2. ```python
  3. import numpy as np
  4. from descartes import PolygonPatch
  5. import matplotlib.pyplot as plt
  6. import alphashape
  7. points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
  8. (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]
  9. fig, ax = plt.subplots()
  10. ax.scatter(*zip(*points_2d))
  11. alpha_shape = alphashape.alphashape(points_2d, 0.)
  12. ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))
  13. plt.savefig(f"./test.png&quot)

您知道这为什么不起作用吗?

非常感谢!

  1. <details>
  2. <summary>英文:</summary>
  3. I am trying to use the package Alphashape from [here](https://alphashape.readthedocs.io/_/downloads/en/latest/pdf/).
  4. Although I correctly copied and pasted the example in the initial pages, I get the following error:

File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py", line 87, in PolygonPatch
return PathPatch(PolygonPath(polygon), **kwargs)
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py", line 62, in PolygonPath
vertices = concatenate([
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py", line 63, in <listcomp>
concatenate([asarray(t.exterior)[:, :2]] +
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed

  1. The code I am trying:

import numpy as np
from descartes import PolygonPatch
import matplotlib.pyplot as plt
import alphashape

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))
alpha_shape = alphashape.alphashape(points_2d, 0.)
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))
plt.savefig(f"./test.png")

  1. Do you why this is not working?
  2. Many thanks!
  3. </details>
  4. # 答案1
  5. **得分**: 2
  6. 从我所能看出,这个问题似乎是由于descartesshapely的实现出现问题导致的。
  7. 我猜测shapely改变了如何处理多边形的外部,而descartes可能还没有更新。
  8. 我不知道这是否是最好的方法,但我直接编辑了我的descartes安装以修复这个问题:
  9. 1. 转到你的descartes安装目录并打开patch.py文件。
  10. 2. 在第62行,你应该看到这段代码:
  11. ```python
  12. vertices = concatenate([
  13. concatenate([asarray(t.exterior)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
  14. for t in polygon])

只需将其中的"t.exterior"改为"t.exterior.coords"。这应该可以修复你的问题。

  1. vertices = concatenate([
  2. concatenate([asarray(t.exterior.coords)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
  3. for t in polygon])
英文:

So from what I could tell, this issue comes from a broken implementation of shapely within descartes.

My speculation is that shapely changed how it handles Polygon exteriors and descartes simply hasn't been updated.

I don't know if it is the best idea, but I edited my installation of descartes directly to fix this issue:

  1. Navigate to your descartes installation and open patch.py.
  2. At line 62 you should see this piece of code:
  3. vertices = concatenate([
  4. concatenate([asarray(t.exterior)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
  5. for t in polygon])

Simply change t.exterior to t.exterior.coords. This hopefully should fix your issue.

  1. vertices = concatenate([
  2. concatenate([asarray(t.exterior.coords)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
  3. for t in polygon])

huangapple
  • 本文由 发表于 2023年2月27日 01:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定