the best way to get the OBJECTID OID name in arcpy?

 

What's the best way to get the OBJECTID OID name in arcpy?

https://gis.stackexchange.com/questions/104664/whats-the-best-way-to-get-the-objectid-name

 

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.

arcpy objectid

1 Answer

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  follow 

edited Jul 9 '14 at 18:23

answered Jul 9 '14 at 17:59

 

radouxju

45.3k11 gold badge5252 silver badges130130 bronze badges

  • 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

  • Is ListFields faster than Describe? – Learner Mar 3 at 7:12
  • From my experience, no, they are both relatively slow... I've tested on two different medium size) gdb and the results are ~6.4 for Describe and ~7.4 for ListFields (from scratch). If the fc is already in memory, the time is ~1.6 and 2.5, respectively. – radouxju Mar 3 at 9:13

     

原文地址:https://www.cnblogs.com/xiexiaokui/p/13467100.html