Table of Contents
The Video
Introduction
Learn how to count points within polygons in ArcGIS Pro using a custom ArcPy script tool and a single geoprocessing tool. In this practical Python tutorial, you’ll use Calculate Field and an Arcade expression to count intersecting points and write the results directly to a polygon attribute field.
ArcGIS Pro Definition Query Masterclass with ArcPy & Python
Free on YouTube! Witness the power of Definition Queries in ArcGIS Pro through this comprehensive course that dives managing and utilizing definition queries with spatial data using ArcPy and Python scripting. In this course, we will focus exclusively on Definition Queries, covering their creation, modification, and application through hands-on exercises and real-world scenarios.
Import ArcPy
arcpy module.
import arcpy
User Inputs
point_features: These are the point features that will be counted if the intersect the polygon_features.where_clause: An optional SQL statement to limit the points to count.polygon_features: The count of intersecting points are added as an attribute value to this polygon feature class.count_field: An existing numeric field for adding the count values
## the points to count
point_features = arcpy.GetParameterAsText(0)
## optional where clause
where_clause = arcpy.GetParameterAsText(1)
## the polygon feature classes to count the number of points
polygon_features = arcpy.GetParameterAsText(2)
## the field from the polygon_features to use for the count attribute
count_field = arcpy.GetParameterAsText(3)
Count Points in Polygon
CalculateField() tool and a dash of Arcade.
arcpy.management.CalculateField(
in_table = polygon_features,
field = count_field,
expression = f"""var points = Filter(FeatureSetByName($datastore, "{point_features}", ["{where_clause.split(" ")[0]}"], false), "{where_clause}")
return Count(Intersects(points, $feature))""",
expression_type = "ARCADE"
)
Create the custom tool in ArcGIS Pro
See the video.
If you’ve found these blogs helpful and would like to support the project, please consider making a donation. All learning material is provided free of charge to help GIS professionals, students, and developers learn ArcPy, the ArcGIS API for Python, ArcGIS Pro, and ArcGIS Online. Donations help cover website hosting, software licensing, domain costs, and the time involved in creating and maintaining tutorials, courses, and learning resources. Your support helps keep this content freely available to everyone and allows new courses and materials to be developed for the GIS community. If the content has helped you solve a problem, learn a new skill, or advance your career, please consider supporting the project with a donation. Every contribution, no matter the size, is greatly appreciated.
All the code in one place
import arcpy
########################################################################################
## Documentation Links:
## https://doc.esri.com/en/arcgis-pro/latest/arcpy//functions/getparameter.htm
## https://doc.esri.com/en/arcgis-pro/latest/arcpy/functions/getparameterastext.html
## https://doc.esri.com/en/arcgis-pro/latest/tool-reference/data-management/calculate-field.html?tabs=python
##
########################################################################################
########################################################################################
## USER INPUTS #########################################################################
## the points to count
point_features = arcpy.GetParameterAsText(0)
## optional where clause
where_clause = arcpy.GetParameterAsText(1)
## the polygon feature classes to count the number of points
polygon_features = arcpy.GetParameterAsText(2)
## the field from the polygon_features to use for the count attribute
count_field = arcpy.GetParameterAsText(3)
########################################################################################
## COUNT POINTS IN POLYGON #############################################################
arcpy.management.CalculateField(
in_table = polygon_features,
field = count_field,
expression = f"""var points = Filter(FeatureSetByName($datastore, "{point_features}", ["{where_clause.split(" ")[0]}"], false), "{where_clause}")
return Count(Intersects(points, $feature))""",
expression_type = "ARCADE"
)
########################################################################################