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
How to create a Polygon given its Point vertices?
提问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 Polygon
from shapely Point
objects?
如何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 Polygon
constructor doesn't expect a list of Point
objects 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
回答by Adam
In version 1.7a2
they have fixed this.
在版本中,1.7a2
他们已修复此问题。
The code in question will just work.
有问题的代码将正常工作。