Select to view content in your preferred language

Search Cursor not honoring selection

726
5
03-10-2020 02:27 PM
JoeBorgione
MVP Emeritus

ArcGIS Pro 2.5

I have made a manual selection of 38 polygons (of 240,000).  In the python window of my project, I run a search cursor that looks like this: 

with arcpy.da.SearchCursor(ng,'USNG')as cursor:
    for row in cursor:
        print(row)‍‍‍

And it prints the row until the cows come home.  I found this post from last year: Why is Search Cursor not honoring layer Selection? but it's about selections being made outside of Pro.  I just ran GetCount () and it returns all the records.  What the heck is going on?

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

Where is ng being defined?

DanPatterson_Retired
MVP Emeritus

did you set your workspace just to be sure

Get Count—Data Management toolbox | Documentation 

and it is a layer or table view?

If the input is a layer or table view containing a selected set of records, only the selected records will be counted.

JoeBorgione
MVP Emeritus

ng is defined earlier as 

r'Drive:\path\to\a\featureclass\named\NationalGrid'‍

And that my friends is the root of the problem.  I insert the variable name, so it's looking at whats out there on disc, not the feature layer ArcGIS Pro has selected in the project. Same holds true for Get Count.  When I let arcgis pro add the feature class or table name from the toc all is well.  Although when I type in arcpy.getcount, it auto completes it the slightly different syntax of:

arcpy.management.GetCount('WesternTiles')
<Result '2'>

in this case I'm inserting the suggested table (view) name of WesternTiles, which I have two records selected.

Thanks-

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

And that is knowledge wrap for today folks...

0 Kudos
JoeBorgione
MVP Emeritus

Yep!  My problem (at least on of them) is I spend more time in Spyder than than I do in Pro and have variables on the brain...

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