In an ArcPro python script I have created a feature layers from a GDB feature class and made a selection.
field_values = {}
# Field names are identical between the source and the target
row_cnt = int(arcpy.GetCount_management(selected_layer).getOutput(0))
cnt = 0
with da.SearchCursor(selected_layer, update_fields) as _cursor:
for _row in _cursor:
update_values = _row[:-1]
_geo = _row[-1]
for i in range(len(update_values)):
fld = update_fields[i]
if fld not in field_values:
field_values[fld] = [update_values[i]]
else:
field_values[fld].append(update_values[i])
# add this row to the excel table for visual comparison of source data to the final row
test_rows.append(update_values)
cnt += 1
The row count of the selected layer is 21, but when I increase the cnt variable by one each time through the cursor, the total number of lines is 167.
Has anyone experienced this issue with ArcPy for Pro? Do layer selections in Pro not work with da.Cursors?
I was thinking that a naming conflict between the feature class and the layer could be happening so I appended "_lyr" to the end of the layer name.
cnt is used on row 4 and 17, what is its purpose?
or is this a code formatting issue?
How are you running this code? In ArcGIS Pro, either the interactive Python window or script tool, or from outside the application?
And, what version of Pro are you running?
Same questions as the other guys, and I'll add:
When/how are you making the selection?
but when I increase the cnt variable by one each time through the cursor,
You increase cnt on line 17, but it's after the the cursor is done. (Same as Dan's question)
I'm not real sure what line # 3 is doing for you....
So what I've come to realize is that the reference to the feature layer with selection must be made using the feature layer name. It seems that in the past I've used the output from the MakeFeatureLayer_management function as input into other tools, but what actually should be passed is the name of the layer. So I end up using layer.name.
I'm seeing this as new behavior in 2.3. First observed from a Python Toolbox, then taken to the Python window to investigate when it no longer worked as expected. Make a selection on the layer in the map. Pass the layer name to SearchCursor. All rows are processed, not just those selected.
For a layer with 3134 features and 72 are selected:
i = 0
with arcpy.da.SearchCursor('Feature Service Name\\layer_name',['ObjectID']) as cur:
for row in cur:
i += 1
i is 3134. In this case the layer comes from a Portal Feature Service. I'll bet that's the source of the trouble. Even tried a brand new project. Same issue.
Likely related to a defect already logged:
#BUG-000118589 Search Cursor does not recognize selection sets on table views or feature layers outside of ArcGIS Pro
I has been marked as Implemented in 2.4
Good to know. That's the kind of bug that makes you crazy....
I wonder what does "outside of ArcGIS Pro" mean? Everything I am doing is very much "inside" of ArcGIS Pro...
I suspect 'Outside of ArcGIS Pro' refers to using cursors in a stand alone script, possibly calling it as a scheduled task etc..