Search code examples
python-3.xversioningarcgisarcpy

Python Script not working on Versioned Feature Class


I have a geoprocessing script that worked great, until moving the data source to a feature dataset where versioning must be enabled.

When this script is pointing to the layer outside the feature dataset, it runs fine. When I point to the layer inside the feature dataset, the script still runs...no errors, but no updates are saved. Any ideas on why this is happening? Sample code below:

import arcpy, os
arcpy.env.overwriteOutput = 1

# Set workspace and input fc variables
folderPath = r"S:\Data\Fires\Fire_Mapping_Analysis"
arcpy.env.workspace = folderPath + "\EmerOps as GIS.sde\EmerOps.GIS.FireBoundaries"
scratch_workspace = r"S:\Data\Fires\Fire_Mapping_Analysis\Fire_Map_Reports_Review.gdb"
BrushFire_fc = "\EmerOps.GIS.BrushFireAnalysis"

# Geoprocessing - Calculate fire acreage, perimeter, and class

print("Calculating Fire acreage, perimeter, and class...")
coords = "PROJCS['NAD_1983_StatePlane_California_V_FIPS_0405_Feet',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['False_Easting',6561666.666666666],PARAMETER['False_Northing',1640416.666666667],PARAMETER['Central_Meridian',-118.0],PARAMETER['Standard_Parallel_1',34.03333333333333],PARAMETER['Standard_Parallel_2',35.46666666666667],PARAMETER['Latitude_Of_Origin',33.5],UNIT['Foot_US',0.3048006096012192]]"
arcpy.management.CalculateGeometryAttributes(BrushFire_fc, "GIS_ACRES AREA;PERIMETER PERIMETER_LENGTH", "MILES_US",
                                             "ACRES", coords, "SAME_AS_INPUT")

# Fire Classes by acreage

fields = ['GIS_ACRES', 'CLASS']

with arcpy.da.UpdateCursor(BrushFire_fc, fields) as cursor:
    for row1 in cursor:
        if (row1[0] > 4999.5):
            row1[1] = "G"
            cursor.updateRow(row1)
        elif (row1[0] > 999.5 and row1[0] < 4999.4):
            row1[1] = "F"
            cursor.updateRow(row1)
        elif (row1[0] > 299.5 and row1[0] < 999.4):
            row1[1] = "E"
            cursor.updateRow(row1)
        elif (row1[0] > 99.95 and row1[0] < 299.4):
            row1[1] = "D"
            cursor.updateRow(row1)
        elif (row1[0] > 9.95 and row1[0] < 99.94):
            row1[1] = "C"
            cursor.updateRow(row1)
        elif (row1[0] > 0.255 and row1[0] < 9.94):
            row1[1] = "B"
            cursor.updateRow(row1)
        elif (row1[0] <= 0.254):
            row1[1] = "A"
            cursor.updateRow(row1)

        else:
            print("No records found")
        # print(str(row1.FIRE_NAME) + ", Class " + row1.CLASS)    
    

The layer is versioned. I think this is playing a big role here too.


Solution

  • The key was using arcpy.da.Editor. The script would run, but the edits would not save without the Editor. This seems to be the key when working with versioned datasets.

    #Start edit session on versioned dataset
    edit = arcpy.da.Editor(workspace, exclude feature dataset from path)
    edit.startEditing(False, True)
    edit.startOperation()
    
    #geoprocessing during the edit session then...
    
    #Stop editing and save edits
    edit.stopOperation()
    edit.stopEditing(True)