Describe method for ObjectID

6197
9
03-13-2013 07:31 AM
babakkasraei
New Contributor II
I know how to use describe method when there is an FID field in layer's attribute table.

I use the following codes


   # describe the feature layer to access the the selected set
      desc = arcpy.Describe(lyr)
   # FIDSet will contain the selected features
      selectedFids = desc.FIDSet


But when a layer is added to a map from a GeoDatabase it has an ObjectID* fields instead of FID.

How can I describe an OBJECTID* field usng describe method?

I appreciate if somebody helps me.

Thanks

Babak
Tags (2)
0 Kudos
9 Replies
ChrisSnyder
Regular Contributor III
You can use describe to figure out what the 'oidFieldName' is (whether it be FID, OID, OBJECTID, or something else).

oidFieldName = arcpy.Describe(myLayer).oidFieldName


Worth mentioning - If you have v10.1, the data access cursors allow a MUCH faster and better way of getting a list of the selected OID values than the .fidSet describe property:

oidValueList = [r[0] for r in arcpy.da.SearchCursor(myLayer, ["OID@"])]
0 Kudos
ChrisSnyder
Regular Contributor III
BTW: Per your actual question, the .fidset Describe property lists the OBJECTID value, no matter what it's called.
0 Kudos
LucasDanzinger
Esri Frequent Contributor
Babak-

Did you try it with a fc instead of shp? It seems to work just the same for me. Did you make sure to have a selection on the layer? The FIDSet will return a semicolon-delimited string of selected feature IDs.

Luke
0 Kudos
ChrisSnyder
Regular Contributor III
Per what Lucas said, the .fidset property only works when there is a selected set - Note that a definition query is NOT a selection.

The

oidValueList = [r[0] for r in arcpy.da.SearchCursor(myLayer, ["OID@"])].

method will return a list regardless of a selection or not. If there is a selected set on the feature layer/table view, then only those selected OID values will be returned. Otherwise, if there is no selected records, then all the OIDs in the feature layer/table view will be returned.
0 Kudos
babakkasraei
New Contributor II
You can use describe to figure out what the 'oidFieldName' is (whether it be FID, OID, OBJECTID, or something else).

oidFieldName = arcpy.Describe(myLayer).oidFieldName


Worth mentioning - If you have v10.1, the data access cursors allow a MUCH faster and better way of getting a list of the selected OID values than the .fidSet describe property:

oidValueList = [r[0] for r in arcpy.da.SearchCursor(myLayer, ["OID@"])]


Hi Chris

Thank you for your and other friends' help.

1- I have to write the codes for V 10.0.

2- When I use the following codes:


  # describe the feature layer to access the the selected set
      desc = arcpy.Describe(lyr)
  
  # FIDSet will contain the selected features
      selectedFids = desc.FIDSet


It returns a list of selected FIDs then I can manipulate them.

When there is no FID field and I use the following codes as you suggested.


desc = arcpy.Describe(lyr)
   
if desc.hasOID:
 
     selectedOID = desc.OIDFieldName



It returns only the name of OID for example OBJECTID nothing more.

I want it to work like FIDSet.

What is the problem.

I appreciate your help.

Babak
0 Kudos
RaphaelR
Occasional Contributor II
as the others before said, you can use the same code you used for a shapefile:

  # describe the feature layer to access the the selected set
      desc = arcpy.Describe(lyr)
  
  # FIDSet will contain the selected features
      selectedFids = desc.FIDSet


But you will only get a list of values if you have selected some features before running the code.
0 Kudos
babakkasraei
New Contributor II
as the others before said, you can use the same code you used for a shapefile:

  # describe the feature layer to access the the selected set
      desc = arcpy.Describe(lyr)
  
  # FIDSet will contain the selected features
      selectedFids = desc.FIDSet


But you will only get a list of values if you have selected some features before running the code.


Dear Rafael

You and other friends are right FIDSet still works in my script. My problem is in another part of the script. I first write the whole codes to illustrate what I am doing and then I show you the problem . These are the codes:


import sys,os,math,string,arcpy
from arcpy import env

# Read from current map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Data Themes")[0]
layer = arcpy.mapping.ListLayers(mxd, "*", df )

#layer_list = [layer for layer in arcpy.mapping.ListLayers(mxd) if 'FID' in [field.name for field in arcpy.ListFields(layer, 'FID')]]

for lyr in layer:
   if lyr.isGroupLayer == False:
    # describe the feature layer to access the the selected set
      desc = arcpy.Describe(lyr)
   # FIDSet will contain the selected features
      selectedFids = desc.FIDSet

    # If there are selectedFids (a selection set), write them to a new feature
    # class in the current workspace.
      if len(selectedFids) > 0:
           
          queryList = selectedFids.replace(';', ',')
          newName = arcpy.AddFieldDelimiters(lyr, 'OBJECTID')
          lyr.definitionQuery =  '{0} in ({1})' .format(newName, queryList)
          arcpy.RefreshActiveView() 


This script must exclude unselected features in the feature class. My problem is in the last three lines here


        queryList = selectedFids.replace(';', ',')

        newName = arcpy.AddFieldDelimiters(lyr, "OBJECTID")
 # This query will exclude unselected fatures      
       lyr.definitionQuery =  '{0} in ({1})' .format(newName, queryList)



For query it must be something like [OBJECTID] in ...... But if I don't put quotes in newName line it will be wrong.

How should I write it?

Thank you for your help.

Babak
0 Kudos
RaphaelR
Occasional Contributor II
not sure if i completely understand, but your script looks like you´re working with layers inside arcmap in the current mxd.
If you want to copy selected features of a layer to a new feature class, CopyFeatures will respect the selection:

Summary

Copies features from the input feature class or layer to a new feature class. If the input is a layer which has a selection, only the selected features will be copied. If the input is a geodatabase feature class or shapefile, all features will be copied.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000035000000
0 Kudos
babakkasraei
New Contributor II
not sure if i completely understand, but your script looks like you´re working with layers inside arcmap in the current mxd.
If you want to copy selected features of a layer to a new feature class, CopyFeatures will respect the selection:


http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000035000000


Dear Rafael

My problem looks simple. I need to get rid of quotes in my SQL expression.

I will post a new thread for it.

I appreciate if you please give me yor idea.

Thak you for your help.

Babak
0 Kudos