Select to view content in your preferred language

Clear selection in Python

12-02-2010 12:23 PM
New Contributor II
I have run into a quirk of using python for geoprocessing, I think.  It seems one cannot clear a selection on a FC using python GP when your passed input is a FC with a selection in place.  To do it, you have to convert the FC to a layer (with makefeaturelayer) and then use selectlayerbyattribute clear_selection option.  But, doing this, you loose all the other features (the unselected ones) in the layer.  Is this correct, or am I missing something in my delerium?  I see that you can build a model and in the end issue a clear_selection (with selectlayerbyattribute), and then you will still have all features (previously selected and unselected), and can then pass this onto your python script (which mine is calculating a frequency on a field which had selected values recalculated and then selection is cleared so that I can get the new frequency on all values).

Am I missing something?  Is there a non-GP method to clear the selection on the actual FC (shapefile, gdb FC, whatever)?

It seems odd to me that one cannot do the most basic and quintessential GIS operations on any and all feature classes (without converting to a layer) -- select data by location/attribute.

0 Kudos
6 Replies
New Contributor III
Where are you running your Python script from?  If you are running it in ArcMap and you've already made your selection, then you would pass the script your data as a Feature Layer parameter.

For example, in the following, TestPolys is a Feature Layer parameter that has a selection on it.  The first calculation is run on "CalcField" and only affects the selected features/records.  That selection is then cleared and the second calculation is then run, affecting all of the layer's features/records.
# Process: Calculate Field...
gp.CalculateField_management(TestPolys, "CalcField", FirstCalc, "PYTHON_9.3", "")

# Process: Select Layer By Attribute...
gp.SelectLayerByAttribute_management(TestPolys, "CLEAR_SELECTION", "")

# Process: Calculate Field (2)...
gp.CalculateField_management(TestPolys, "CalcField", SecondCalc, "PYTHON_9.3", "")
0 Kudos
New Contributor II
In my case, the workflow, for whatever reason, is that a user will select features from a shapefile using several methods (graphic select, select by other features, select by attribute) and once satisfied with the selection set, will then run a tool that calculates a field for only the selection and then needs to run a frequency on the full set of data (nothing selected)... in order to clear the selection, gotta use selectlayerbyattribute clear_selection option which requires a layer.  So, the shapefile, with its selection, has to be converted to a layer, which will only take the selections into the layer, all unselected data is lost.
0 Kudos
MVP Emeritus
selections are only honored when a layer is the input, if you then need a summary operation on the same file, then do it on the featureclass on disk associated with the layer.
0 Kudos
New Contributor II
I'm also encountering this same "quirk" as a problem and would like to find a solution or at least a clarification.

I'm currently working on a script that will be attached to a tool to be activated in ArcMap.  Its primary goal is to perform some data integrity checks on some parcel data stored in two separate feature classes in a file geodatabase.  If the parcel data in either feature class is already added to the ArcMap session by the user (which is expected) and has some selected records (unexpected, but I'm trying to idiot-proof this for when accidents happen), then the MakeFeatureLayer code only brings in the selected records for analysis.  I really need a way in python to test a feature class already in ArcMap to see if it has any selected records, and if so, unselect them before creating the layer in code.  Since a "Clear Selected Features" button exists within ArcMap, the lattter functionality has to exist, but I can't find any indication of it in the help or documentation.

Any insight is appreciated.
0 Kudos
New Contributor
Dear Esri

I have been using ModelBuilder to batch process some selections that I need to do on my data. I am trying to use Select By Locations to find all points within one polygon and to get a count of the total as a separate table. The task involves repeating the process by selecting the points in one polygon, counting the selected rows, clearing that selection and repeating the process for another polygon. The problem is that I cannot readily implement the "Clear Selection". I am using the attached model graphic to do the selection but I then manually have to click the "Clear Selected Features" icon in ArcMap. I realise there must be an easier way to batch this procedure by using clear selected but I have failed to find it. I am not a Python programmer so any guidance in ModelBuilder would be greatly appreciated!

Many thanks

0 Kudos
Regular Contributor III
A "feature layer" and/or "table view" is basically a handy in-memory pointer to an on disk feature class or table. Basically it stores, in-memory, a number of descriptors concerning the feature class or table. One of those things is the OBJECTIDs that are currently selected. One of the great things about a feature layer / view is that most of the geoprocessing tools can operate directly on the feature layer / view itself (e.g. the tools honor definition queries and selections).

Therefore there is no need to make an export of your selected set. Just run the stats tool (or maybe the GetCount tool?) directly on the feature layer that is the output of the SelectByLocation or the SelectByAttributes tool. To clear the selection, just run the SelectByLocation or the SelectByAttributes tool again on the feature layer, but this time don't specify a SQL statement or spatial selection type (you will note these are optional tool parameters) and just specify the "CLEAR_SELECTION" selection type parameter.
0 Kudos