Shapely: 从点列表转换为多边形

创建于 2012-08-24  ·  3评论  ·  资料来源: Toblerity/Shapely

大家好,

我没有找到论坛的邮件列表,所以我要在这里问。 是否有“规范”形式可以从点列表转换为多边形? 例如,

list_points = []
vertices = [Point(1,1), Point(2,2), Point(3,3)]
for i in xrange(len(vertices)):
    list_points.append(vertices[i])
polygon = Polygon(list_points)

此代码不起作用。 我不得不使用convex_hull:

poly = MultiPoint(list_points).convex_hull

什么不是我要找的东西。

感谢您的耐心和出色的工作!

最有用的评论

再次稍微快一点:

points = [Point(0,0), Point(2,2), Point(2,0)]
coords = [p.coords[:][0] for p in points]
poly = Polygon(coords)
poly.area

所有3条评论

事情是这样的:多边形环(LinearRing 的实例)不是由点定义的,它们是由坐标元组序列定义的。 点和坐标与 Shapely 不同。 您需要将点的坐标传递给 Polygon 构造函数。 像这样:

>>> points = [Point(0,0), Point(2,2), Point(2,0)]
>>> coords = sum(map(list, (p.coords for p in points)), [])
>>> poly = Polygon(coords)
>>> poly.area
2.0

当我不时地搜索这个时,这是我更喜欢的方式(我发现它更易读):

>>> points = [Point(0,0), Point(2,2), Point(2,0)]
>>> coords = [(p.x, p.y) for p in points]
>>> poly = Polygon(coords)
>>> poly.area
2.0

timeit 说事情可能会被缓存,但@sgillies的方法似乎始终快 2/3(我的 ~65µs 对他的 ~45µs)所以如果你需要超级超级硬核性能来获得无数的点或多边形,请考虑一下。

再次稍微快一点:

points = [Point(0,0), Point(2,2), Point(2,0)]
coords = [p.coords[:][0] for p in points]
poly = Polygon(coords)
poly.area
此页面是否有帮助?
0 / 5 - 0 等级