How to create a polygon based on its vertex points?

I want to create a polygon from beautiful 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 an error of type TypeError: object of type 'Point' has no len()

How to create Polygon from slender Point objects?

+16
python polygon shapely
source share
4 answers

If you specifically want to build your Polygon from points of the correct geometry, then call their x, y properties in the list comprehension. In other words:

 from shapely import geometry poly = geometry.Polygon([[px, py] for p in pointList]) print(poly.wkt) # prints: 'POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))' 

Note that the shape is smart enough to close the polygon on your behalf, i.e. you don’t have to pass the first point at the end again.

+31
source share

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]]) 
+6
source share

The Polygon constructor does not expect a list of Point objects, but a list of point coordinates.

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

+2
source share

In version 1.7a2 they fixed it.

The code in question will just work.

Link to CHANGES.txt

0
source share

All Articles