Gis Tricks

accessing objectid field in a python script

accessing objectid field in a python script

This one was bugging me in the past. When you use python and script some geoprocessing tools after one another ArcGIS makes a total mess of your object id fieldname. it can be:

  • objectid
  • object_id
  • objectid_1
  • objectid_2
  • etc

Or when the user decides that the temp directory should contain shapefiles, it can be any variation of this starting with FID.

The following principle works for any arcgis version as far as I know, except that the example here is only for arcgis 10+

Here is a piece of code that simply copies the object id contents to a different fieldname (which already exists in this case):


desc = arcpy.Describe(fc)
cursor = arcpy.UpdateCursor(fc)
row = cursor.next()
while row:
    ident = row.getValue(desc.OIDFieldName)
    row.setValue(fieldname, ident)
    cursor.updateRow(row)
    row = cursor.next()

Yeah!

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 2 =