UpdateCursor requires Spatial Index Recalculation bug

424
4
02-16-2023 10:34 AM
ZacharyUhlmann1
Occasional Contributor III

I'll keep it simple.  Every time I run arcpy.da.UpdateCursor() either via the Python console, a Notebook or with a Python script to update an attribute table, the entire feature layer disappears from the map.  Additionally, other feature layers disappear from the map (still present Table of Contents).

with arcpy.da.UpdateCursor('feat_in_TOC', ['fld_name']) as cursor:
    for r in cursor:
        new_val = '{}_new'.format(r[0])
        r[0] = new_val
        cursor.updateRow(r)
del cursor

After running (successfully!) the feature layer has disappeared from the map.  To fix this, I have to go to Recalculate the Index via the feature Properties --> Indexes --> Spatial Index dropdown --> Recalculate.  The feature layer returns.  

Note that the code sample has been run with different variations, field types, etc. - always same result.  Just a simple example.

Note that this has been occurring since version 2.<8 and 9> ( https://community.esri.com/t5/forums/postpage/board-id/arcgis-pro-questions ), so it's not a version thing. 

What is going on?

thanks,

Zach

Pro 3.02

Tags (3)
4 Replies
JosephMelchiors
New Contributor

Any update on this?  It's killing me.  Anyone know of a workaround?

-Joe

0 Kudos
ZacharyUhlmann1
Occasional Contributor III

Oh man!  Glad somebody else noticed this issue.  I haven't heard anything.  I never reported the bug.  If I do, I'll add a comment here. 

Is arcpy.da.updateCursor the culprit for you?

with arcpy.da.<something>Cursor as cursor:
    # do some stuff; 
    # I imagine it's when using da.UpdateCursor
del cursor

 

0 Kudos
JosephMelchiors
New Contributor

I found a workaround that works quickly, at least for me.  If I just copy the feature layer to itself (after I'm done with my cursor loop), it updates both my deletes and edits and keeps everything else in place.  Not elegant, but at least I'm not banging my head against the wall anymore.

#this refreshes the feature layer so deleted are removed
arcpy.management.CopyFeatures("flyrData", "flyrData") 

0 Kudos
ZacharyUhlmann1
Occasional Contributor III

Have you tried Recalculate Index from the properties of the feature layer in the TOC?  Perhaps there is a programmatic way to do that, but it's a 15 second thing, then wait a minute + for the maps to refresh.

0 Kudos