ArcGIS: How to count the number of points in a polygon cell - batch Python processing GeoPandas

created at 06-02-2022 views: 8

problem

In ArcMap, the operation is very simple, directly use the spatial join (Spatial Join) tool, pay attention to two points: control the attribute fields to be included in the output in the output options (the default includes all, including First, Sum, Mode, Count and other options), in the matching conditions, you can select options such as intersect, lie, contain, border contact, fully contain, center within the feature range, etc. For the selection of these two important parameters, please refer to ArcGIS Spatial Connections (Analysis).

solution

In Python, we select GeoPandas for processing. The difference is that the generated DataFrame does not have the default summary function in ArcMap, but the summary function of Pandas can be used. The code is as follows:

import geopandas as gpd

input_path = "E:\\Data\\shp\\"
output_path = "E:\\Data\\join\\"
# Read the surface data, adjust the names of latitude and longitude, and set the coordinate system
gdf_polys = gpd.read_file(input_path+"poly.shp")
gdf_polys = gdf_polys.rename({'lng': 'lng_c', 'lat': 'lat_c'})
gdf_polys.to_crs(crs="EPSG:4326", inplace=True)
# Read point data, set the coordinate system if necessary
gdf_points = gpd.read_file(input_path+"point.shp")

# Spatial connection
gdfsjoin = gpd.sjoin(gdf_points, gdf_polys, how='left', op='within', lsuffix='_pt', rsuffix='_py')
# summary
df = gdfsjoin.drop('geometry', axis=1).groupby(by=['poly_index']).aggregate({'point_id': 'count'})
# output
df.to_csv(output_path+'join.csv', index=False, header=True, encoding='gbk')
created at:06-02-2022
edited at: 06-02-2022: