Select to view content in your preferred language

da.SearchCursor() not honoring selected set in ArcGIS Pro

3716
9
Jump to solution
08-22-2018 06:59 AM
JoeBorgione
MVP Emeritus

(ArcGIS Pro 2.2)

In ArcMap 10.5.1 when I select a number of features and apply a search cursor to it, the cursor examines the selected features only but in ArcGIS Pro it does not:

#arcmap, python 2.x:

import arcpy
with arcpy.da.SearchCursor("National_Grid",'USNG') as cursor:
    for row in cursor:
        print row[0]
...         
12TVL320100
12TVL320120
12TVL340120
12TVL340140
12TVL360120
12TVL360140


#####################

#ArcGIS Pro python 3.x:

import arcpy
fc = r'C:\LAS_Data\NationalGrid\National_Grid.shp'
fields = 'USNG'
gridlist = []
with arcpy.da.SearchCursor(fc,fields)as cursor:
    for row in cursor:
        gridlist.append(row[0])

gridlist
['12TUK920880', '12TUK920900', '12TUK920920', '12TUK920940', '12TUK920960', '12TUK920980', '12TUK940840', '12TUK940860', '12TUK940880', '12TUK940900', '12TUK940920', '12TUK940940', '12TUK940960', '12TUK940980', '12TUK960840', '12TUK960860', '12TUK960880', '12TUK960900', '12TUK960920', '12TUK960940',etc,etc,etc....]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Bummer.

Is this a new known (intended) feature?  Is there a way to convince the search cursor to honor the selected set?

That should just about do it....
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I am not sure what is going on.  I am running 2.2.1, and I just tested passing a layer with a selection set to a search cursor, and the cursor only enumerated the selected records.  And, the layer was based on a shape file.

View solution in original post

0 Kudos
9 Replies
MichaelVolz
Esteemed Contributor

It looks like you are using a shapefile as your source.  Can you try the same script on another source such as a file gdb and see if the same behavior occurs?

JoeBorgione
MVP Emeritus

Good idea, but same results none the less... 

del fc
fc = r'J:\BuildingFootPrints\Emigration\EmigrationFootPrints\EmigrationFootPrints.gdb\NatGrid_FC'
gridlist = []
with arcpy.da.SearchCursor(fc,fields)as cursor:
    for row in cursor:
        gridlist.append(row[0])
gridlist
['12TUK920880', '12TUK920900', '12TUK920920', '12TUK920940', '12TUK920960', '12TUK920980', '12TUK940840', '12TUK940860', '12TUK940880', '12TUK940900', '12TUK940920', '12TUK940940', '12TUK940960', '12TUK940980', '12TUK960840', '12TUK960860', '12TUK960880', '12TUK960900', '12TUK960920', '12TUK960940', '12TUK960960', '12TUK960980', '12TUK980780', '12TUK980800', '12TUK980820', '12TUK980840', '12TUK980860', '12TUK980880', '12TUK980900', '12TUK980920', '12TUK980940', '12TUK980960', '12TUK980980', '12TUL920000', blah, blah,blah...]
That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Where are you running the code, from a Command Prompt, and IDE, or the interactive Python window in Pro?

0 Kudos
JoeBorgione
MVP Emeritus

for now in the ArcGIS Pro python window.  That's typically how I test code: run commands there, and when the work, copy and paste into a python file and execute as such...

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Your code snippets above are not equivalent.  Your ArcMap snippet is passing a layer to the search cursor while your Pro snippet is passing a feature class.  Feature classes and tables don't have selections against them, layers and table views do.  Try passing the layer to the Search Cursor in Pro.

JoeBorgione
MVP Emeritus

Another good idea but with the same results... Sigh....

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am not sure what is going on.  I am running 2.2.1, and I just tested passing a layer with a selection set to a search cursor, and the cursor only enumerated the selected records.  And, the layer was based on a shape file.

0 Kudos
JoeBorgione
MVP Emeritus

Of course it did!!!

I'll keep trying it out; seems like a no brainer, right?!

That should just about do it....
0 Kudos
JoeBorgione
MVP Emeritus

Okay.  Another Joe loves Pro so much thread. It's not software; it's an adventure...

I opened up a new project and added a polygon feature class.  I created a layer file from it (arcpy.MakeFeatureLayer_management()).

Selecting 4 polygons in the layer file, I ran the search cursor and it returned the field data in the specified selected records.  Then I ran it directly on the feature class with a few features selected.  Works just fine.

Please refer to the above line that starts with Okay...

That should just about do it....
0 Kudos