从Python图形多边形中提取点/坐标

时间:2021-01-25 21:23:04

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

如何得到/提取定义形状多边形的点?谢谢!

Example of a shapely polygon

一个shapely多边形的例子。

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)

7 个解决方案

#1


35  

So, I discovered the trick is to use a combination of the Polygon class methods to achieve this.

我发现诀窍在于使用多边形类方法的组合来实现这一点。

If you want geodesic coordinates, you then need to transform these back to WGS84 (via pyproj, matplotlib's basemap, or something).

如果需要测地线坐标,则需要将它们转换回WGS84(通过pyproj、matplotlib的basemap或其他)。

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

#2


9  

You can use the shapely mapping function:

可以使用shapely映射函数:

>>> 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)),)}

#3


2  

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将一个shapely多边形转换为一个NumPy数组。我发现使用NumPy数组比coords返回的数组更有用。因为坐标是成对的,而不是在二维数组中。使用对您的应用程序更有用的任何一个。

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]]

#4


2  

I used this:

我用这个:

list(zip(*p.exterior.coords.xy))

Polygon created with: p = Polygon([(0,0),(1,1),(1,0),(0,0)]) returns:

多边形创建:多边形p =(((0,0),(1,1),(1,0)、(0,0)])的回报:

[(0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]

#5


0  

Update (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数组接口(如doc所述:http://tobleriy.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:

此命令将执行对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:

提示:你必须给出外在的东西。一个多边形的坐标因为给出了直接的几何图形似乎也没有作用:

In [4]: coordinates_array = np.asarray(poly)
Out[4]: array(<shapely.geometry.polygon.Polygon object at 0x7f627559c510>, dtype=object)    

#6


0  

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)打印了两次,因为是外部的。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]))

#7


0  

If you really want the shapely point objects that make up the polygon, and not just tuples of coordinates, you can do that this way:

如果你真正想要的是构成多边形的形状的点,而不只是坐标的元组,你可以这样做:

points = MultiPoint(polygon.boundary.coords)

#1


35  

So, I discovered the trick is to use a combination of the Polygon class methods to achieve this.

我发现诀窍在于使用多边形类方法的组合来实现这一点。

If you want geodesic coordinates, you then need to transform these back to WGS84 (via pyproj, matplotlib's basemap, or something).

如果需要测地线坐标,则需要将它们转换回WGS84(通过pyproj、matplotlib的basemap或其他)。

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

#2


9  

You can use the shapely mapping function:

可以使用shapely映射函数:

>>> 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)),)}

#3


2  

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将一个shapely多边形转换为一个NumPy数组。我发现使用NumPy数组比coords返回的数组更有用。因为坐标是成对的,而不是在二维数组中。使用对您的应用程序更有用的任何一个。

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]]

#4


2  

I used this:

我用这个:

list(zip(*p.exterior.coords.xy))

Polygon created with: p = Polygon([(0,0),(1,1),(1,0),(0,0)]) returns:

多边形创建:多边形p =(((0,0),(1,1),(1,0)、(0,0)])的回报:

[(0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]

#5


0  

Update (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数组接口(如doc所述:http://tobleriy.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:

此命令将执行对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:

提示:你必须给出外在的东西。一个多边形的坐标因为给出了直接的几何图形似乎也没有作用:

In [4]: coordinates_array = np.asarray(poly)
Out[4]: array(<shapely.geometry.polygon.Polygon object at 0x7f627559c510>, dtype=object)    

#6


0  

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)打印了两次,因为是外部的。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]))

#7


0  

If you really want the shapely point objects that make up the polygon, and not just tuples of coordinates, you can do that this way:

如果你真正想要的是构成多边形的形状的点,而不只是坐标的元组,你可以这样做:

points = MultiPoint(polygon.boundary.coords)