![](/img/trans.png)
[英]using Geopandas, How to randomly select in each polygon 5 Points by sampling method
[英]Making rows of points in dataframe into POLYGON using geopandas
我试图找出点是否在封闭的多边形中(在这个问题中: Finding if a point in a dataframe is in a polygon and assignment polygon name to point )但我意识到可能还有另一种方法可以做到这一点:
我有这个 dataframe
df=
id x_zone y_zone
0 A1 65.422080 48.147850
1 A1 46.635708 51.165745
2 A1 46.597984 47.657444
3 A1 68.477700 44.073700
4 A3 46.635708 54.108190
5 A3 46.635708 51.844770
6 A3 63.309560 48.826878
7 A3 62.215572 54.108190
我想把它变成
id Polygon
0 A1 POLYGON((65.422080, 48.147850), (46.635708, 51.165745), (46.597984, 47.657444), (68.477700, 44.073700))
1 A3 POLYGON((46.635708,54.108190), (46.635708 ,51.844770), (63.309560, 48.826878),(62.215572 , 54.108190))
并对点做同样的事情:
df1=
item x y
0 1 50 49
1 2 60 53
2 3 70 30
至
item point
0 1 POINT(50,49)
1 2 POINT(60,53)
2 3 POINT(70,30)
我从来没有使用过geopandas,在这里有点不知所措。
因此,我的问题是:如何从 pandas dataframe 到 dataframe 与 geopandas 属性?
感谢您的任何见解!
您可以按以下方式实现,但您必须设置正确的 dtype。 我知道在 ArcGIS 中您必须将 dtype 设置为几何;
df.groupby('id').apply(lambda x: 'POLYGON(' + str(tuple(zip(x['x_zone'],x['y_zone'])))+')')
我建议以下直接从您的 df 获取 GeoDataFrame:
from shapely.geometry import Polygon
import geopandas as gpd
gdf = gpd.GeoDataFrame(geometry=df.groupby('name').apply(
lambda g: Polygon(gpd.points_from_xy(g['x_zone'], g['y_zone']))))
它首先使用 geopandas 的points_from_xy
创建一个点列表,然后从此列表中创建一个多边形 object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.