Sadhu Baba Sadhu Baba - 2 years ago 158
Python Question

Python, Shapely: How to create a Polygon given its Point vertices?

I want to create a polygon from shapely points.

p1 = Point(0,0)
p2 = Point(1,0)
p3 = Point(1,1)
p4 = Point(0,1)

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

poly = Polygon(pointList)

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

How to create a
from shapely

Answer Source

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:

poly = 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download