Geometry Not Updating With Update Cursor

728
5
05-01-2019 01:30 PM
BrandonZaitz
New Contributor III

So, I have a script that calculates polylines for well borepaths from CSV directional survey files and then updates the geometry of existing features with that calculated polyline; it also writes a "Y" to a attribute field called "REBUILT" so i know which features have the new updated calculated polyline. I have ran this script at least a dozen times without error. The last time i ran it, it completed with no errors reported, however the geometry was not updated for any of the features BUT the "REBUILT" field was created and written to. To put it simply, the update cursor is updating normal attribute fields but not geometry. Has anyone ran into something like this before? I would post the code here but it is hundreds of lines and is being run on proprietary data that i cannot share. 

The snippet below is the bit of the script where the Update Cursor is called:

edit = arcpy.da.Editor(workspace)
edit.startEditing()
edit.startOperation()
with arcpy.da.UpdateCursor(clean_borepath_shapefile,["API_Text","REBUILT","SHAPE@"]) as borepath_cursor:
polylinecount = 0
for borepath in borepath_cursor:
api = borepath[0]
for county,coordinatesdict in countycoordinatesdict.iteritems():
if api in coordinatesdict:
coordinates = coordinatesdict.get(api)
if len(coordinates) > 0:
polyline = CreatePolyline(coordinates,spatial_reference)
borepath[1] = "Y"
borepath[2] = polyline
borepath_cursor.updateRow(borepath)
polylinecount = polylinecount + 1
edit.stopOperation()
edit.stopEditing(True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

UPDATE: Just ran it again for the 5th or 6th time and it worked. The inputs didn't change, the code didn't change. Utterly baffled.

0 Kudos
5 Replies
LukeWebb
Occasional Contributor III

Theres a slight chance if you had the layer in the map the whole time, that ArcGIS had cached the Shape column so its not loading all the data from disk permanantly.

You could try dragging the layer into a new ArcMap window to be sure!  

Or add a call at the end of your script to http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/refreshactiveview.htm

For me, normally if i zoom in and out, then it seems to reload the data as well. (Panning not so much)

0 Kudos
BrandonZaitz
New Contributor III

I don't normally have ArcMap open when i run this script. So that wouldn't make sense for my case. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Move your edit session closing statement outside of your with statement.

BrandonZaitz
New Contributor III

Oops, it actually is outside of it in the actual program. That was just an error when i copied and pasted i guess.

0 Kudos
MartinSirkovsky1
New Contributor III

Hi Brandon.

I have experiencing this behaviour on insert cursor, however not on update. But it might be, because I am running cursor on points FC and not polygons. But here are my observation, maybe it helps.

I am not sure if you have solved this issue. I have experienced this many times on insert cursor. I have found out that this is happening if you are opening a cursor using an edit session with a feature class which has attachments. And if feature class has attachments, you have to use the edit session to open a cursor. When running the insert cursor, all the inserted features will end up on coordinates of last inserted features.

As workaround, after I insert the features, I run an update cursor which then updates the shape field with the original coordinates and it usaully works. For that to work, you will need to store the x, y coordinates in the attribute table, or somehow link the the insersted features with the source data. 

Another workaround, but slower one is to insert one featuere at a time, but that requires that you open an edit session, open the insert cursor,  insert the feature, and close the edit session and then repeat this workflow. This could take a long time if you are inserting many records.

Below is a sample code, which can prove my case. If I only run the inserst_features() function, the points will be inserted and all end up on the location of the last point.

However if I also run the function update_coord(), the points will be placed at their correct position.

I have tested this with feature class stored in Microsoft SQL DBMS, and also in FGDB. The issue was present in both cases.

Hopefully we can get some explanation from ESRI.

# -*- coding: utf-8 -*-
#!/usr/bin/python

import datetime, arcpy


sde = "c:/connection.sde"
inputFC = "test_fc"

arcpy.env.workspace = sde

def insert_features(xcoord, ycoord, number_of_features_to_insert):
now = datetime.datetime.now()

edit = arcpy.da.Editor(sde)
# Edit session is started without an undo/redo stack for versioned data
edit.startEditing(False, False)
# Start an edit operation
edit.startOperation()

fields = ['SHAPE@XY', 'insert_time', 'xcoord', 'ycoord']
cursor = arcpy.da.InsertCursor(inputFC, fields)

for i in range(0, number_of_features_to_insert):

y = ycoord + (i*10)

geometry = [xcoord, y]
print "Inserting x, y: {0}, {1}".format(xcoord, y)
cursor.insertRow([geometry, now, xcoord, y])
continue


edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
del cursor

return


def update_coords():
edit = arcpy.da.Editor(sde)
# Edit session is started without an undo/redo stack for versioned data
edit.startEditing(True, False)
# Start an edit operation
edit.startOperation()

whereClause = None
fields = ['SHAPE@XY', 'xcoord', 'ycoord', 'objectid']
with arcpy.da.UpdateCursor(inputFC, fields, whereClause) as cursor:
for row in cursor:

print "Updating row oid: {0}".format(row[3])

row[0] = [row[1], row[2]]
cursor.updateRow(row)

edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
del cursor


# Functions creates new points and place all points on same x coordinate,
# but increases the y coordinate by 10 for each next feature
insert_features(xcoord = 200010, ycoord = 800000, number_of_features_to_insert = 3)

# fucntion updates shape field of input feature class using,
# values in xcoord and ycoord fields.
update_coords()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos