ArcPy.mapping: expose SelectionSet property of given layer

12-04-2010 12:41 PM
Status: Open
Labels (1)
New Contributor II

Many of the geoprocessing tools work on a selection set, if one has been made prior to running the tool.  In the ArcObjects .NET framework, there is a SelectionSet property of the IFeatureSelection interface.  This allows one to determine whether or not a selection has been made, and to manipulate that selection.  In the ArcPy site package, the Layer class in the mapping module does not expose anything involving selections.  It would be extremely useful to expose the SelectionSet property in this module so that the Python coder could easily:
- Determine if a selection had been made.
- Get a count of how many features/records were selected.
- Further manipulate that selection.

If you create a describe object from a layer you can get access to the FIDSet property which returns a semi-colon delimited string of the FID of all selected features in the layer. Based on this below is some sample code to accomplish the items you mention:

- Determine if a selection had been made.

desc = arcpy.Describe(lyr)
if not  desc.FIDSet  == '':
   print 'Layer has a selection'

- Get a count of how many features/records were selected.

2 options:

result = arcpy.GetCount_management(lyr)
print result.getOutput(0)


desc = arcpy.Describe(lyr)

- Further manipulate that selection.

Use select layer by attribute and Selection types, Add_To_Selection, Remove_From_Selection, or Subset_Selection to modify the existing selection.
This issue here is the difficulty in determining actual Selection Count.  If there is no Selection, the  getcount method returns the total count, not zero.  Likewise, the FIDSet returns all records in the class if there is no selection.
@davenicht, I can not reproduce what you are describing. If there is no selection FIDSet should return an empty string.
@davenicht, I was able to reproduce what you are describing. It seems that the FIDSet works differently depending upon whether or not your layer is itself based upon a selection (subset) of features from an actual feature class:

Case 1 (@Chris_Fox)
  1. Added feature class to ArcMap
  2. FIDSet returns nothing.
  3. Selected some features.
  4. FIDSet return OIDs of selected features.
Case 2 (@davenicht)
  1. Added feature class to ArcMap
  2. Selected some features.
  3. Chose 'Create layer from selected features' (Right-click>Selection).
  4. FIDSet on new layer (without selection) returned OIDS of all features in layer.
  5. Selected some features in the new layer.
  6. FIDSet returned OIDs of selected features.
Just in case anyone is interested, the workaround I've had to use is to detect whether a selection is present in all cases (see post below) is to temporarily switch the selection set and then check if the count on this is zero. If it is zero then there's not a selection set and conversley if it's not there is.

This is kind of acceptable for small datasets, but when using large datasets, switching the selection set leads to a large overhead.