How to create a shapely Polygon from a list of shapely Points?

前端 未结 4 724
故里飘歌
故里飘歌 2020-12-17 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 =          


        
相关标签:
4条回答
  • 2020-12-17 09:12

    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.

    0 讨论(0)
  • 2020-12-17 09:26

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

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

    0 讨论(0)
  • 2020-12-17 09:27

    In version 1.7a2 they have fixed this.

    The code in question will just work.

    Link to CHANGES.txt

    0 讨论(0)
  • 2020-12-17 09:28

    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]])
    
    0 讨论(0)
提交回复
热议问题