Select to view content in your preferred language

Issue with UpdateCursor - cannot update branch versioned table

118
0
04-02-2025 07:35 AM
HamzaMerini
Occasional Contributor

Hello,

I'm trying to create a new GP Tool, that should retrieve a feature and update its geometry

Here's the code:

import arcpy

# Define input parameters
feature_id = arcpy.GetParameterAsText(0)
new_coordinates = arcpy.GetParameterAsText(1)
output_layer = arcpy.GetParameterAsText(2)

# Split the coordinates into latitude and longitude
coords = new_coordinates.split(',')
latitude = float(coords[0].strip())
longitude = float(coords[1].strip())

arcpy.AddMessage(f"Coordinates: Latitude = {latitude}, Longitude = {longitude}")

# Example transformation: shifting the point
new_latitude = latitude + 0.002
new_longitude = longitude + 0.001

arcpy.AddMessage(f"Calculated Coordinates: Latitude = {new_latitude}, Longitude = {new_longitude}")


# Get spatial reference of the output layer
spatial_ref = arcpy.Describe(output_layer).spatialReference

# Get the workspace of the feature layer (e.g., file geodatabase or enterprise geodatabase)
workspace = arcpy.Describe(output_layer).path  # Get the workspace from the feature layer

# Start editing session using arcpy.da.Editor (to handle versioned data properly)
edit = arcpy.da.Editor(workspace)

# Begin editing on the specified version
edit.startEditing(with_undo=False, multiuser_mode=True)
edit.startOperation()

try:
    # Print out the query for debugging
    arcpy.AddMessage(f"Query being executed: OBJECTID = {feature_id}")

    # Use UpdateCursor to find and update the existing feature by OBJECTID
    with arcpy.da.UpdateCursor(output_layer, ["OBJECTID", "SHAPE@"], "objectid = {feature_id}") as cursor:
        for row in cursor:
            # Create a new point geometry with the calculated coordinates
            new_point = arcpy.PointGeometry(arcpy.Point(new_longitude, new_latitude), spatial_ref)
            
            # Update the feature's geometry
            row[1] = new_point  # Assign the new geometry to the SHAPE@ field
            cursor.updateRow(row)

    # Stop the operation and commit the changes
    edit.stopOperation()
    edit.stopEditing(save_changes=True)  # True commits changes

    arcpy.AddMessage(f" Feature ID {feature_id} successfully updated.")

except Exception as e:
    # If an error occurs, stop the operation and discard changes
    if 'edit' in locals():
        edit.stopOperation()
        edit.stopEditing(False)  # False discards changes
    
    arcpy.AddError(f" Error updating feature: {str(e)}")

 

But since the layer that I'm using is versioned I get this error : cannot update branch versioned table

Is there a way to bypass this issue OR I am blocked? 
Do you have any suggestions on how I can solve this issue/ use another method? 

Thank you!

0 Kudos
0 Replies