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

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

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

问题

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

我尝试使用来自[此处](https://alphashape.readthedocs.io/_/downloads/en/latest/pdf/)的Alphashape包

尽管我已经正确复制并粘贴了初始页面上的示例但我遇到以下错误

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


我正在尝试的代码:
```python
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&quot)

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

非常感谢!


<details>
<summary>英文:</summary>

I am trying to use the package Alphashape from [here](https://alphashape.readthedocs.io/_/downloads/en/latest/pdf/). 

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

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")


Do you why this is not working? 

Many thanks!

</details>


# 答案1
**得分**: 2

从我所能看出,这个问题似乎是由于descartes中shapely的实现出现问题导致的。

我猜测shapely改变了如何处理多边形的外部,而descartes可能还没有更新。

我不知道这是否是最好的方法,但我直接编辑了我的descartes安装以修复这个问题:

1. 转到你的descartes安装目录并打开patch.py文件。

2. 在第62行,你应该看到这段代码:

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

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

vertices = concatenate([
    concatenate([asarray(t.exterior.coords)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
    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:

Navigate to your descartes installation and open patch.py.

At line 62 you should see this piece of code:

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

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

vertices = concatenate([
concatenate([asarray(t.exterior.coords)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
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:

确定