<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Issue with UpdateCursor - cannot update branch versioned table in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/issue-with-updatecursor-cannot-update-branch/m-p/1601847#M94576</link>
    <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I'm trying to create a new GP Tool, that should retrieve a feature and update its geometry&lt;BR /&gt;&lt;BR /&gt;Here's the code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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"&lt;span class="lia-unicode-emoji" title=":white_heavy_check_mark:"&gt;✅&lt;/span&gt; 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"&lt;span class="lia-unicode-emoji" title=":cross_mark:"&gt;❌&lt;/span&gt; Error updating feature: {str(e)}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;But since the layer that I'm using is versioned I get this error :&amp;nbsp;cannot update branch versioned table&lt;BR /&gt;&lt;BR /&gt;Is there a way to bypass this issue OR I am blocked?&amp;nbsp;&lt;BR /&gt;Do you have any suggestions on how I can solve this issue/ use another method?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Wed, 02 Apr 2025 14:35:44 GMT</pubDate>
    <dc:creator>HamzaMerini</dc:creator>
    <dc:date>2025-04-02T14:35:44Z</dc:date>
    <item>
      <title>Issue with UpdateCursor - cannot update branch versioned table</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/issue-with-updatecursor-cannot-update-branch/m-p/1601847#M94576</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I'm trying to create a new GP Tool, that should retrieve a feature and update its geometry&lt;BR /&gt;&lt;BR /&gt;Here's the code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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"&lt;span class="lia-unicode-emoji" title=":white_heavy_check_mark:"&gt;✅&lt;/span&gt; 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"&lt;span class="lia-unicode-emoji" title=":cross_mark:"&gt;❌&lt;/span&gt; Error updating feature: {str(e)}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;But since the layer that I'm using is versioned I get this error :&amp;nbsp;cannot update branch versioned table&lt;BR /&gt;&lt;BR /&gt;Is there a way to bypass this issue OR I am blocked?&amp;nbsp;&lt;BR /&gt;Do you have any suggestions on how I can solve this issue/ use another method?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 02 Apr 2025 14:35:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/issue-with-updatecursor-cannot-update-branch/m-p/1601847#M94576</guid>
      <dc:creator>HamzaMerini</dc:creator>
      <dc:date>2025-04-02T14:35:44Z</dc:date>
    </item>
  </channel>
</rss>

