ArcPy.mapping: expose SelectionSet property of given layer

1684
6
12-04-2010 12:41 PM
Status: Implemented
Labels (1)
JoeVondracek1
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.

6 Comments
ChrisFox
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)

or

desc = arcpy.Describe(lyr)
len(desc.FIDSet.split(";"))

- 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.
DavidNichter
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.
ChrisFox
@davenicht, I can not reproduce what you are describing. If there is no selection FIDSet should return an empty string.
DarrenSmith
@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.
DarrenSmith
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.
JeffBarrette
Status changed to: Implemented

This has been implemented in ArcGIS Pro (arcpy.mp) for a while.  Changes will not be made to the arcpy.mapping API.

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layer-class.htm

See specifically:

Layer.getSelectionSet()

Layer.setSelectionSet({oidList}, [{method})