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
Unlock the power of Definition Queries in ArcGIS Pro through this comprehensive course that dives deep into the intricacies of managing and utilizing definition queries with spatial data using ArcPy and Python scripting. Definition Queries provide a dynamic way to filter and display specific subsets of your data, enabling you to create more insightful maps and analyses. In this course, we will focus exclusively on Definition Queries, covering their creation, modification, and application through hands-on exercises and real-world scenarios. Accredited by the Association for Geographic Information (AGI) for 1 CPD point.
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
Unlock the full potential of ArcPy Cursors with our intensive and in-depth course. Designed for GIS professionals, analysts, and enthusiasts, this course delves into the intricacies of Search, Insert, and Update Cursors in the ArcPy library, empowering you to manipulate and manage spatial data with precision and efficiency.
ArcPy Cursors enable you to streamline your GIS workflows, automate repetitive tasks, and witness the full potential of spatial data manipulation in ArcGIS Pro.
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")