Select to view content in your preferred language

SearchCursor do not loop after select layer with empty result

1632
10
Jump to solution
11-13-2020 02:10 AM
by Anonymous User
Not applicable

I have noticed that after select layer by location/attribute with empty result (example, i tried select layer with attribute, which that do not have), SearchCursor do not see rows in layer table. I tried SearchCursor layer write with dataSource, it's work on *.gdb whom i have physical access, but for *.sde whom i do not have physical access it's do not work ('Cannot open file' error). If i can select all layers features, than SearchCursor will see rows. I know about arcpy.SelectLayerByAttribute, but it is slow. I work with setSelectionSet and after clear selection with setSelectionSet('new', []) is same problem. How me revive layer after select with empty result?

Please, help me and sorry for my English!

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

@Anonymous User wrote:

after this arcpy tools (Search\UpdateCursor, GetCount, SelectLayerByAttributes(with subset_selection method) or SelectLayerByLocation....) do not see rows


This is expected behavior so I am not exactly sure what you are trying to get at with this comment.  Most, if not all, geoprocessing tools honor selection sets on layers and table views.  If you have a selection set with no records, there is nothing for the tools to operate on.

View solution in original post

10 Replies
DuncanHornby
MVP Notable Contributor

You need to share your code if you want people to help. Make sure it's formatted correctly

0 Kudos
by Anonymous User
Not applicable

after clear selection

 

arcpy.mapping.Layer('TreesA').setSelectionSet('new', [])

 

SearchCursor do not loop rows (result is emty)

 

with arcpy.da.SearchCursor('TreesA', ['OBJECTID']) as rs:
...     for r in rs:
...         print r[0]

 

I know about

 

arcpy.SelectLayerByAttribute_management('TreesA', 'clear_selection')

 

 but this is slow

when i write

 

with arcpy.da.SearchCursor(arcpy.mapping.Layer('TreesA').dataSource, ['OBJECTID']) as rs:
...     for r in rs:
...         print r[0]

 

SearchCursor see rows (print all OBJECTID)

but when use on *.sde, whom i do not have access

i get:

Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
RuntimeError: cannot open 'K:\INSTRUKCIJAS\!SDE\backup_connections\PRODSQL_TOPO10_3421-25.sde\PRODSQL_TOPO10.DBO.TOPO10_3\PRODSQL_TOPO10.DBO.TreesA'

BlakeTerhune
MVP Regular Contributor

If the ObjectID field is what you're getting, try using the OID@ field name token instead.

0 Kudos
by Anonymous User
Not applicable

Yes, i try get features OID list (field name OBJECTID).

Sorry, i am not understood, can you write example!

0 Kudos
BlakeTerhune
MVP Regular Contributor
with arcpy.da.SearchCursor(arcpy.mapping.Layer('TreesA').dataSource, ['OID@']) as rs:
0 Kudos
by Anonymous User
Not applicable

Sorry, nothing changes!😔

0 Kudos
by Anonymous User
Not applicable

You can try

arcpy.SelectLayerByAttribute_management('layer', 'new_selection', 'select by attributes who do not exist in layer')

after this arcpy tools (Search\UpdateCursor, GetCount, SelectLayerByAttributes(with subset_selection method) or SelectLayerByLocation....) do not see rows

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

@Anonymous User wrote:

after this arcpy tools (Search\UpdateCursor, GetCount, SelectLayerByAttributes(with subset_selection method) or SelectLayerByLocation....) do not see rows


This is expected behavior so I am not exactly sure what you are trying to get at with this comment.  Most, if not all, geoprocessing tools honor selection sets on layers and table views.  If you have a selection set with no records, there is nothing for the tools to operate on.

JoshuaBixby
MVP Esteemed Contributor

Layer.setSelectionSet has some odd behaviors and Esri's documentation is wrong.  Passing an empty list with "New" doesn't clear the selection but creates a new selection with no records, hence why the search cursor is not working.  You need to pass an empty set with "New" to have it clear the selection.

0 Kudos