Table of Contents
The Video
Introduction
Looking to get the number of features/records in a feature class or table in ArcGIS Pro using ArcPy?
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.
User Input
Just the one. The path to the feature class or table.
## the path to the feature class to get the feature count
fc_path = r"C:\path\to\fc_or_table"
The GetCount Geoprocessing Tool
We use the GetCount() geoprocessing tool from the Data Management Toolbox
feature_count = arcpy.management.GetCount(
in_rows = fc_path
)
Print and Assess
Geoprocessing tools in ArcPy return a Result object that contains the output(s) from the tool. It is good to have knowledge about the Result object and accessing the output generated such as the count of features in a Feature Class from the GetCount tool.
print(feature_count)
#print(int(feature_count)) ## this will fail (TypeError) cannot cast Result to int
print(feature_count[0])
print(type(feature_count[0])) # number is a string
print(int(feature_count[0])) # cast to integer
print(type(feature_count)) # Result object returned from geoproccessing tool.
print(feature_count.outputCount) # GetCount returns one output
print(feature_count.getOutput(0)) # access the output via the Result object
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.
All the code in one place
You can find the entire code workflow below with links to important components in the documentation that were used.
import arcpy
################################################################################
## ArcPy Reference Links:
## https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/get-count.htm
## https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/result.htm
##
################################################################################
################################################################################
## USER INPUT ##################################################################
## the path to the feature class to get the feature count
fc_path = r"C:\path\to\fc_or_table"
################################################################################
## GET COUNT ###################################################################
feature_count = arcpy.management.GetCount(
in_rows = fc_path
)
print(feature_count)
#print(int(feature_count)) ## this will fail (TypeError) cannot cast Result to int
print(feature_count[0])
print(type(feature_count[0])) # number is a string
print(int(feature_count[0])) # cast to integer
print(type(feature_count)) # Result object returned from geoproccessing tool.
print(feature_count.outputCount) # GetCount returns one output
print(feature_count.getOutput(0)) # access the output via the Result object
################################################################################
print("\nSCRIPT COMPLETE")