Branch Versioning Child Version Bulk Update using the ArcGIS Python API

456
0
05-23-2023 03:05 PM
ArizonaGIS
New Contributor III

Hello. We recently migrated to branch versioning, and I noticed our Python automation scripts no longer work because the scripts can't write against the DEFAULT Branch Version. I researched and found a possible solution, but I am having trouble with my script with changing the DEFAULT Parent Version to the Child Version to make bulk updates and then reconcile/post back into the DEFAULT Parent Version. I am unsure if I should use the ArcGIS Portal feature service or the feature class to perform the version change. I attached my proposal code below. The error message is cryptic and difficult to troubleshoot | "RuntimeError: Object: Error in executing tool"

 

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayer, Feature
import arcpy

# Connect to the GIS
gis = GIS(url="YOUR PORTAL URL", username="YOUR USERNAME", password="YOUR PASSWORD")

# Define version names
parent_version = "sde.DEFAULT"
child_version = "CHILD_VERSION"

# Get the feature service
service_id = "SERVICE ID"
service = gis.content.get(service_id)
layer = service.layers[0]

# Switch to the child version for editing
arcpy.management.ChangeVersion(layer, "BRANCH", f"{child_version}", None, "INCLUDE")

# Fetch the features to be updated
features = layer.query()  # You may need to refine this query

# Create a dictionary to match the prefixes to the correct "St_PreTyp" value
prefixes = {
    "Interstate": "Interstate"}

# Loop through the features
for feature in features:
    # Get the "St_Name" value
    st_name = feature.attributes["St_Name"]
    
    if st_name is not None:
        update_required = True
        # Check if St_Name contains only the Spanish prefix
        if st_name.strip() in ["Calle"]:
            update_required = False  # No update required
        # If update is required, loop through the prefixes to find a match
        if update_required:
            for prefix, pretyp in prefixes.items():
                if st_name.startswith(prefix):
                    # If additional characters are present, parse and update the fields
                    feature.attributes["St_PreTyp"] = pretyp
                    feature.attributes["St_Name"] = st_name.replace(prefix, "").lstrip().replace("Highway", "").lstrip()
                    break
    else:
        feature.attributes["St_PreTyp"] = None

# Submit the edits
layer.edit_features(updates=features)

# After performing your operations, reconcile and post the changes from the child version to the parent version
arcpy.ReconcileVersions_management(database, "ALL_VERSIONS", parent_version, child_version, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "KEEP_VERSION")

# Switch back to the parent version
arcpy.management.ChangeVersion(layer, "BRANCH", parent_version, None, "INCLUDE")

 

 

 

0 Kudos
0 Replies