Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
I agree it is very straightforward. I've just noticed Describe can be a bit slow. Thanks. –  ian Jul 9 at 18:04
    
@ian yes, thanks, I have updated –  radouxju Jul 9 at 18:24

Examine the code example in the help files The describe object and the OIDFieldName property will allow you to get its name if it has one

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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