12

What's the best way to get the OBJECTID name of a feature class?

I know you can use "OID@" in a cursor to get the field, but can you get the name from that as well?

I'm writing a script, and need to take into account varying OBJECTID names, like OBJECTID_2, OBJECTID_12, FID, etc. that will be used in a query.

Is using Describe the only way to get the name?

For example:

oid_field = [field.name for field in arcpy.ListFields("FC") if field.type == "OID"]

I'm just wondering if there's another way.

Thanks.

22

I would say that using describe is the best

oid_fieldname = arcpy.Describe(fc).OIDFieldName

is quite straightforward. Otherwise, you can directly filter when you use listFields, so you could try

oid_fieldname = arcpy.ListFields(fc,"","OID")[0].name

but this will return an error if there is no oidField (unlikely, but...)

  • 1
    I agree it is very straightforward. I've just noticed Describe can be a bit slow. Thanks. – ianbroad Jul 9 '14 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.