Extract points/coordinates from a polygon in Shape

2020-02-17 05:52发布

How do you get/extract the points that define a shapely polygon? Thanks!

Example of a shapely polygon

from shapely.geometry import Polygon

# Create polygon from lists of points
x = [list of x vals]
y = [list of y vals]

polygon = Polygon(x,y)

8条回答
聊天终结者
2楼-- · 2020-02-17 06:56

You can use the shapely mapping function:

>>> from shapely.geometry import Polygon, mapping
>>> sh_polygon = Polygon(((0,0), (1,1), (0,1)))
>>> mapping(sh_polygon)
{'type': 'Polygon', 'coordinates': (((0.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)),)}
查看更多
▲ chillily
3楼-- · 2020-02-17 06:57

Update (2017-06-09):

As the last answer seems not to work anymore with newest version of shapely, I propose this update.

shapely provides the Numpy array interface (as the doc says: http://toblerity.org/shapely/project.html )

So, let poly be a shapely polygon geometry:

In [2]: type(poly)
Out[2]: shapely.geometry.polygon.Polygon

This command will do the conversion to a numpy array:

In [3]: coordinates_array = np.asarray(poly.exterior.coords)

Hint:
One must need to give the exterior.coords for a polygon because giving the direct geometry seems not to work either:

In [4]: coordinates_array = np.asarray(poly)
Out[4]: array(<shapely.geometry.polygon.Polygon object at 0x7f627559c510>, dtype=object)    
查看更多
登录 后发表回答