Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 340
Sounak Avatar asked May 26 '15 11:05

Sounak


People also ask

How do you convert Linestring to polygons?

geometry. Polygon to simply convert to line string to a polygon. It will connect the first and last coordinates. Try Polygon([(0, 0), (1, 1), (1, 2), (0, 1)]) or Polygon(s1) to produce POLYGON ((0 0, 1 1, 1 2, 0 1, 0 0)).

What is Unary_union?

unary_union. Returns a geometry containing the union of all geometries in the GeoSeries . >>> >>> from shapely.geometry import box >>> s = geopandas. GeoSeries([box(0,0,1,1), box(0,0,2,2)]) >>> s 0 POLYGON ((1.00000 0.00000, 1.00000 1.00000, 0.... 1 POLYGON ((2.00000 0.00000, 2.00000 2.00000, 0....


1 Answers

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.

like image 84
songololo Avatar answered Oct 14 '22 00:10

songololo