Table of Contents
The Video
Introduction
Learn the easiest way to count feature attachments in ArcGIS Pro using ArcPy. In this practical Python tutorial, you’ll use the Calculate Field geoprocessing tool and an Arcade expression to automatically count the number of attachments for every feature or record.
Mastering ArcGIS Pro ArcPy Search, Insert, & Update Cursors
Free on YouTube! ArcPy Cursors from the data access module enable you to streamline your GIS workflows, automate repetitive tasks, and witness the full potential of spatial data manipulation in ArcGIS Pro by accessing, inserting, and updating features in your datasets.
Import ArcPy
arcpy module.
import arcpy
User Inputs
We have two user inputs.
in_features: The feature layer to count the attachments for.count_field: An existing numeric field for adding the count values
## the feature layer to count the attachments for
in_features = arcpy.GetParameter(0)
## the field to update the counts
count_field = arcpy.GetParameterAsText(1)
Count Points in Polygon
CalculateField() tool and a dash of Arcade.
arcpy.management.CalculateField(
in_table = in_features,
field = count_field,
expression = "Count(Attachments($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
##
## Note: this will not tell you if attachments are enabled or not. If attachments
## are not enabled, 0 (zero) will be returned for each feature.
##
########################################################################################
########################################################################################
## USER INPUTS #########################################################################
## the feature layer to count the attachments for
in_features = arcpy.GetParameter(0)
## the field to update the counts
count_field = arcpy.GetParameterAsText(1)
########################################################################################
## GET ATTACHMENT COUNT PER FEATURE ####################################################
arcpy.management.CalculateField(
in_table = in_features,
field = count_field,
expression = "Count(Attachments($feature))",
expression_type = "ARCADE"
)
########################################################################################