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!
Solved! Go to Solution.
@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.
You need to share your code if you want people to help. Make sure it's formatted correctly
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'
If the ObjectID field is what you're getting, try using the OID@ field name token instead.
Yes, i try get features OID list (field name OBJECTID).
Sorry, i am not understood, can you write example!
with arcpy.da.SearchCursor(arcpy.mapping.Layer('TreesA').dataSource, ['OID@']) as rs:
Sorry, nothing changes!😔
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
@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.
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.