Table of Contents
The Video
Introduction
The OID field for your Feature Class in ArcGIS Pro might be called OBJECTID, it could also be OID, FID, or any other name that you have given it. This can make it difficult to create a script to process data that may not have a standard OID field name you expect. With ArcPy we can query the fields for a designated OID field type to access the OID field name.
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 OID field for
fc_path = r"C:\path\to\fc_or_table"
Get OID Field Name with arcpy.ListFields()
The arcpy.ListFields() function will return all fields as Field objects. We can interrogate the type property to access the OID field.
oid_field = [fld.name for fld in arcpy.ListFields(fc_path) if fld.type=="OID"][0]
print(oid_field)
Get the OID Field Name with Describe()
We can use the Data Access module (da) and the Describe function to return a dictionary where we can find the OID field name, or we can use the old school arcpy.Describe() function that returns a Describe object.
oid_field = arcpy.da.Describe(fc_path)["OIDFieldName"]
oid_field = arcpy.Describe(fc_path).OIDFieldName
print(oid_field)
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/arcpy/functions/listfields.htm
## https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/describe.htm
##
################################################################################
################################################################################
## USER INPUT ##################################################################
## the path to the feature class to get the OID field for
fc_path = r"C:\path\to\fc_or_table"
################################################################################
## GET OID FIELD WITH LISTFILEDS ###############################################
oid_field = [fld.name for fld in arcpy.ListFields(fc_path) if fld.type=="OID"][0]
print(oid_field)
################################################################################
## GET OID FIELD WITH DESCRIBE #################################################
oid_field = arcpy.da.Describe(fc_path)["OIDFieldName"]
oid_field = arcpy.Describe(fc_path).OIDFieldName
print(oid_field)
################################################################################
print("\nSCRIPT COMPLETE")