如何对位于 geopandas 边界内的数据框中的条目数求和
How to sum the number of entries in a dataframe that are located within a geopandas boundary
我有两个地理数据框:一个包含名为 bird_df
的特定物种的动物目击(以位置为点),另一个详细描述了我所在州内每个城市的边界,名为 map_df
.
我想使用 geopandas 方法 .contains(x) 来计算每个城市中发现的动物数量,并将该总数添加到边界数据框中,以便生成等值线。
我的 pandas 有点生疏,但我已经尝试过类似
map_df[map_df["geometry"].contains(bird_df["geometry"]) == True]
我只是不知道如何解决这个问题。一些帮助将不胜感激。
谢谢
我建议为此使用连接。
import geopandas as gpd
import pandas as pd
# make some dummy points
df = pd.DataFrame()
df['x'] = [0.61, 0.62, 0.63, 0.64, 0.65]
df['y'] = [41.60, 41.62, 41.63, 41.64, 41.65]
points_gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy(df.x, df.y))
# buffer 2 rows of points to make some dummy polys
poly_gdf = points_gdf.head(2).copy(deep=True)
poly_gdf['geometry'] = poly_gdf['geometry'].buffer(0.02)
# do the spatial join, index right is the polygon idx values
sjoin_gdf = gpd.sjoin(points_gdf, poly_gdf)
# count the values with value counts
count_dict = sjoin_gdf['index_right'].value_counts().to_dict()
# map the count_dict back to poly_gdf as new point count column
# alternatively you could do a join here, but new col name is nice
poly_gdf['point_count'] = poly_gdf.index.map(count_dict)
输出poly_gdf:
geometry point_count
0 POLYGON ((0.63000 41.60000, 0.62990 41.59804, ... 1
1 POLYGON ((0.64000 41.62000, 0.63990 41.61804, ... 2
即:
cat_df = pd.concat([points_gdf, poly_gdf])
cat_df.plot(facecolor="none")
我有两个地理数据框:一个包含名为 bird_df
的特定物种的动物目击(以位置为点),另一个详细描述了我所在州内每个城市的边界,名为 map_df
.
我想使用 geopandas 方法 .contains(x) 来计算每个城市中发现的动物数量,并将该总数添加到边界数据框中,以便生成等值线。
我的 pandas 有点生疏,但我已经尝试过类似
map_df[map_df["geometry"].contains(bird_df["geometry"]) == True]
我只是不知道如何解决这个问题。一些帮助将不胜感激。
谢谢
我建议为此使用连接。
import geopandas as gpd
import pandas as pd
# make some dummy points
df = pd.DataFrame()
df['x'] = [0.61, 0.62, 0.63, 0.64, 0.65]
df['y'] = [41.60, 41.62, 41.63, 41.64, 41.65]
points_gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy(df.x, df.y))
# buffer 2 rows of points to make some dummy polys
poly_gdf = points_gdf.head(2).copy(deep=True)
poly_gdf['geometry'] = poly_gdf['geometry'].buffer(0.02)
# do the spatial join, index right is the polygon idx values
sjoin_gdf = gpd.sjoin(points_gdf, poly_gdf)
# count the values with value counts
count_dict = sjoin_gdf['index_right'].value_counts().to_dict()
# map the count_dict back to poly_gdf as new point count column
# alternatively you could do a join here, but new col name is nice
poly_gdf['point_count'] = poly_gdf.index.map(count_dict)
输出poly_gdf:
geometry point_count
0 POLYGON ((0.63000 41.60000, 0.62990 41.59804, ... 1
1 POLYGON ((0.64000 41.62000, 0.63990 41.61804, ... 2
即:
cat_df = pd.concat([points_gdf, poly_gdf])
cat_df.plot(facecolor="none")