Python 从 Shapely 中的多边形中提取点/坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20474549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract points/coordinates from a polygon in Shapely
提问by ryanjdillon
How do you get/extract the points that define a shapelypolygon?
Thanks!
如何获取/提取定义shapely多边形的点?谢谢!
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)
采纳答案by ryanjdillon
So, I discovered the trick is to use a combination of the Polygonclass methods to achieve this.
所以,我发现诀窍是使用Polygon类方法的组合来实现这一点。
If you want geodesic coordinates, you then need to transform these back to WGS84 (via pyproj, matplotlib's basemap, or something).
如果您想要测地线坐标,则需要将它们转换回 WGS84(通过pyproj、matplotlib'sbasemap或其他方式)。
from shapely.geometry import Polygon
#Create polygon from lists of points
x = [list of x vals]
y = [list of y vals]
some_poly = Polygon(x,y)
# Extract the point values that define the perimeter of the polygon
x, y = some_poly.exterior.coords.xy
回答by mkosmala
You can convert a shapely Polygon to a NumPy array using NumPy.array. I find using NumPy arrays more useful than the arrays returned by coords.xy, since the coordinates are paired, rather than in two one-dimensional arrays. Use whichever is more useful to your application.
您可以使用 NumPy.array 将匀称的多边形转换为 NumPy 数组。我发现使用 NumPy 数组比 coords.xy 返回的数组更有用,因为坐标是成对的,而不是两个一维数组。使用对您的应用程序更有用的那个。
import numpy as np
x = [1, 2, 3, 4]
y = [9, 8, 7, 6]
polygon = Polygon(x,y)
points = np.array(polygon)
# points is:
[[ 1 9]
[ 2 8]
[ 3 7]
[ 4 6]]
回答by Ikar Pohorsky
You can use the shapely mappingfunction:
您可以使用 shapelymapping函数:
>>> 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)),)}
回答by s.k
Update (2017-06-09):
更新(2017-06-09):
As the last answer seems not to work anymore with newest version of shapely, I propose this update.
由于最后一个答案似乎不再适用于最新版本的 shapely,我建议进行此更新。
shapely provides the Numpy array interface (as the doc says: http://toblerity.org/shapely/project.html)
shapely 提供了 Numpy 数组接口(如文档所述:http: //toblerity.org/shapely/project.html)
So, let polybe a shapely polygon geometry:
所以,让我们poly成为一个匀称的多边形几何:
In [2]: type(poly)
Out[2]: shapely.geometry.polygon.Polygon
This command will do the conversion to a numpy array:
此命令将转换为 numpy 数组:
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:
提示:
必须为多边形提供 external.coords,因为提供直接几何体似乎也不起作用:
In [4]: coordinates_array = np.asarray(poly)
Out[4]: array(<shapely.geometry.polygon.Polygon object at 0x7f627559c510>, dtype=object)
回答by Anton vBR
I used this:
我用过这个:
list(zip(*p.exterior.coords.xy))
Polygon created with: p = Polygon([(0,0),(1,1),(1,0),(0,0)])returns:
创建的多边形:p = Polygon([(0,0),(1,1),(1,0),(0,0)])返回:
[(0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]
回答by Jayanth
You can use any of the two following methods.
您可以使用以下两种方法中的任何一种。
1)
1)
p = Polygon([(1,0),(1,1),(0,1),(0,0)])
for x,y in p.exterior.coords:
print(x,y)
The above code prints the following. Note that (1,0) is printed twice, since exterior.coords returns an ordered sequence that completes the polygon.
上面的代码打印以下内容。请注意, (1,0) 打印了两次,因为 external.coords 返回完成多边形的有序序列。
1.0 0.0
1.0 1.0
0.0 1.0
0.0 0.0
1.0 0.0
2)
2)
p.exterior.coords.xy
It outputs the following
它输出以下内容
(array('d', [1.0, 1.0, 0.0, 0.0, 1.0]), array('d', [0.0, 1.0, 1.0, 0.0, 0.0]))
回答by Rick supports Monica
If you really want the shapely point objectsthat make up the polygon, and not just tuples of coordinates, you can do that this way:
如果你真的想要构成多边形的匀称点对象,而不仅仅是坐标元组,你可以这样做:
points = MultiPoint(polygon.boundary.coords)
回答by pnklein
It took me a while to learn that a Polygon has an exterior boundary and possibly several interior boundaries. I am posting here because some of the answers don't reflect that distinction, though to be fair the original post did not use as an example a polygon with interior boundaries.
我花了一段时间才知道多边形有一个外部边界,可能还有几个内部边界。我在这里发帖是因为一些答案没有反映这种区别,尽管公平地说,原始帖子没有使用具有内部边界的多边形作为示例。
The points forming the exterior boundary are arranged in a CoordinateSequence, which can be obtained as
形成外部边界的点排列在一个 CoordinateSequence 中,可以得到
polygon.exterior.coords
You can find the length of this object using len(polygon.exterior.coords)and can index the object like a list. To get the first vertex, for example, use polygon.exterior.coords[0]. Note that the first and last points are the same; if you want a list consisting of the vertices without that repeated point, use polygon.exterior.coords[:-1].
您可以使用找到该对象的长度,len(polygon.exterior.coords)并且可以像列表一样索引该对象。例如,要获取第一个顶点,请使用polygon.exterior.coords[0]。注意第一个点和最后一个点是一样的;如果您想要一个由没有重复点的顶点组成的列表,请使用polygon.exterior.coords[:-1].
You can convert the CoordinateSequence (including the repeated vertex) to a list of points thus:
您可以将 CoordinateSequence(包括重复顶点)转换为点列表,因此:
list(polygon.exterior.coords)
Similarly, the CoordinateSequence consisting of the vertices forming the first interior boundary is obtained as polygon.interiors[0].coords, and the list of those vertices (without the repeated point) is obtained as polygon.interiors[0].coords[:-1].
类似地,由形成第一个内部边界的顶点组成的 CoordinateSequence 作为 获得polygon.interiors[0].coords,并且这些顶点的列表(没有重复点)作为 获得polygon.interiors[0].coords[:-1]。

