Python 给定点顶点如何创建多边形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30457089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:27:01  来源:igfitidea点击:

How to create a Polygon given its Point vertices?

pythonpolygonshapely

提问by Sounak

I want to create a polygon from shapely points.

我想从匀称的点创建一个多边形。

from shapely import geometry
p1 = geometry.Point(0,0)
p2 = geometry.Point(1,0)
p3 = geometry.Point(1,1)
p4 = geometry.Point(0,1)

pointList = [p1, p2, p3, p4, p1]

poly = geometry.Polygon(pointList)

gives me an type error TypeError: object of type 'Point' has no len()

给我一个类型错误 TypeError: object of type 'Point' has no len()

How to create a Polygonfrom shapely Pointobjects?

如何Polygon从匀称的Point对象创建一个?

采纳答案by songololo

If you specifically want to construct your Polygon from the shapely geometry Points, then call their x, y properties in a list comprehension. In other words:

如果您特别想从匀称的几何点构建多边形,请在列表推导式中调用它们的 x、y 属性。换句话说:

from shapely import geometry

poly = geometry.Polygon([[p.x, p.y] for p in pointList])

print(poly.wkt)  # prints: 'POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))'

Note that shapely is clever enough to close the polygon on your behalf, i.e. you don't necessarily have to pass-in the first point again at the end.

请注意,shapely 足够聪明,可以代表您关闭多边形,即您不必在最后再次传入第一个点。

回答by dlask

The Polygonconstructor doesn't expect a list of Pointobjects but a list of point coordinates.

Polygon构造并不期望列表Point对象,但点的坐标表。

See https://shapely.readthedocs.io/en/latest/manual.html#polygons

https://shapely.readthedocs.io/en/latest/manual.html#polygons

回答by Malik Brahimi

A Polygonobject requires a nested list of numbers, not a list of Pointobjects.

一个Polygon对象需要一个嵌套的数字列表,而不是一个Point对象列表。

polygon = Polygon([[0, 0], [1, 0], [1, 1], [0, 1]])

回答by Adam

In version 1.7a2they have fixed this.

在版本中,1.7a2他们已修复此问题。

The code in question will just work.

有问题的代码将正常工作。

Link to CHANGES.txt

链接到 CHANGES.txt