Get a Feature Class OID (ObjectID) field Name using ArcPy

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

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 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

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/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")
				
			

Leave a Comment