Is there a way to make a SearchCursor ignore a selection?

3488
4
Jump to solution
08-27-2015 04:29 AM
DuncanHornby
MVP Notable Contributor

Arcpy Guru's,

When you use a TableView with a selection as the table in a SearchCursor, the cursor honours the existing selection. I was wondering is there a property or way of making a cursor simply ignore any existing selection?  Why would I want this? Well I want to summarize a field in the whole table but the table had a selection (which is important), I do not want to clear the selection as I use the selection in some later processing.

For example in ArcMap when you use the GUI tool Select By Location you have a check box that allows you to use or not use the selected features. I could not find a property or parameter of the cursor to say "ignore the selection, process all records, but don't clear the selection"?

Duncan

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You can use the catalogPath property of an ArcPy Describe object instead of the dataSource property of an ArcPy Mapping Layer object.  I prefer this approach because often times I am working with layer names as strings instead of layers as layer objects.  Also, using the ArcPy Describe function works if you are in a standalone script that doesn't involve a map document.

Depending on the circumstances, I either write a normal function or a lambda function to get the catalog path, and then call the function and pass the layer name in the search cursor:

dataSource = lambda in_layer: arcpy.Describe(in_layer).catalogPath
with arcpy.da.SearchCursor(dataSource(layer_name), fields) as cur:
    ...

View solution in original post

4 Replies
WesMiller
Regular Contributor III

Try using  the source lyr.dataSource for your search cursor that should bypass the selection.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You can use the catalogPath property of an ArcPy Describe object instead of the dataSource property of an ArcPy Mapping Layer object.  I prefer this approach because often times I am working with layer names as strings instead of layers as layer objects.  Also, using the ArcPy Describe function works if you are in a standalone script that doesn't involve a map document.

Depending on the circumstances, I either write a normal function or a lambda function to get the catalog path, and then call the function and pass the layer name in the search cursor:

dataSource = lambda in_layer: arcpy.Describe(in_layer).catalogPath
with arcpy.da.SearchCursor(dataSource(layer_name), fields) as cur:
    ...
DuncanHornby
MVP Notable Contributor

Joshua gets the star; particularly as he has introduced a new concept to me, these lambda functions. Never used these and had to go search the internet to find out what they were! Came across this website that explains what and why you would use them.

I have to admit I thought about accessing the table as a Table object rather than a TableView but thought it would be best to check with the community if there was some other whizzy way of doing it.

Thanks for the advice

0 Kudos
DanPatterson_Retired
MVP Emeritus

Duncan as Wes and Joshua have indicated, simply read it from disk to get all and from the layer to get the selection.  However, you could fudge that by reading the selection, switch the selection (in code) and getting the results, so you would have the part and the sum of the parts