How to create a Polygon given its Point vertices?

2019-02-21 08:34发布

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

How to create a Polygon from shapely Point objects?

3条回答
The star\"
2楼-- · 2019-02-21 09:02

A Polygon object requires a nested list of numbers, not a list of Point objects.

polygon = Polygon([[0, 0], [1, 0], [1, 1], [0, 1]])
查看更多
唯我独甜
3楼-- · 2019-02-21 09:15

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:

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.

查看更多
Emotional °昔
4楼-- · 2019-02-21 09:24

The Polygon constructor doesn't expect a list of Point objects but a list of point coordinates.

See http://toblerity.org/shapely/manual.html#polygons

查看更多
登录 后发表回答