The following script works to update coordinates in my ArcGIS feature class, but if I run it twice in a row from ArcMap on the same geodatabase feature class, I get a "RuntimeError: Unexpected Operation[outputZoneTrack FeatureClass name]" error on the "updateCursor.updateRow(row)". If I run it on a dataset, then run it on a different dataset, I can then re-run it on the first dataset again with no problem. I just can't run it on the same dataset twice in a row. Why does this error occur when I run the script twice in a row on the same dataset?
outputZoneTrack = "C:\\Temp\\geodatabase.gdb\\ozt"
outputWorkspace = os.path.split(outputZoneTrack)[0]
outputZoneTrackName = os.path.split(outputZoneTrack)[1]
arcpy.env.overwriteOutput = True
if arcpy.Exists(outputZoneTrack):
arcpy.Delete_management(outputZoneTrack)
spatial_reference = arcpy.SpatialReference(4326) #Use WGS84 so coordinates are calculated in Lat/Long
with arcpy.da.UpdateCursor(outputZoneTrack, ["ZSTARTLAT", "ZSTARTLON", "ZENDLAT", "ZENDLON", "SHAPE@"], "", spatial_reference) as updateCursor:
for row in updateCursor:
row[0] = row[4].firstPoint.Y
row[1] = row[4].firstPoint.X
row[2] = row[4].lastPoint.Y
row[3] = row[4].lastPoint.X
updateCursor.updateRow(row)
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == "Segment Oil Zones":
if lyr.supports("DATASOURCE"):
lyr.replaceDataSource(outputWorkspace, "FILEGDB_WORKSPACE", outputZoneTrackName, True)
lyr.name = "Segment Oil Zones"
lyr.visible = True
arcpy.AddMessage("\nOiling Zone Snapped to ACP Segments: ON\n")
arcpy.RefreshTOC()
Note - this problem only occurs if the script is run from ArcMap and the feature class is loaded within the ArcMap TOC - I can run it multiple times in a row on the same dataset from ArcCatalog.
updateCursor.updateRow(row)
? Please edit your question to include any updatesoutputZoneTrack
when you are deleting it a few lines prior?