Using Update Cursor to populate 2 fields for Feature Class Name and OID

Todd picture Todd · Nov 14, 2012 · Viewed 7.7k times · Source

I am currently trying to populate 2 fields. They are both already created within a table that I want to populate with data from existing feature classes. The idea is copy all data from desired feature classes that match a particular Project #. The rows that match the project # will copy over to blank template with the matching fields. So far all is good except I need to push the data from the OBJECT ID field and the Name of the Feature Class in to 2 fields within the table.

**def featureClassName(table_path):
    arcpy.AddMessage("Calculating Feature Class Name...")
    print "Calculating Feature Class Name..."
    featureClass = "FeatureClass"
    SDE_ID = "SDE_ID"
    fc_desc = arcpy.Describe(table_path)
    lists = arcpy.ListFields(table_path)
    print lists
   with arcpy.da.SearchCursor(table_path, featureClass = "\"NAME\"" + " Is NULL") as  cursor:
        for row in cursor:
            print row
            if row.FEATURECLASS = str.replace(row.FEATURECLASS, "*", fc):
                cursor.updateRow(row)
                    print row
        del cursor, row
            else:
                pass**

The Code above is my attempt, out of many to populate the field with the Name of the Feature class. I have attemped to do the same with the OID.

**for fc in fcs:
print fc
if fc:
    print "Making Layer..."
    lyr = arcpy.MakeFeatureLayer_management (fc, r"in_memory\temp", whereClause)
    fcCount = int(arcpy.GetCount_management(lyr).getOutput(0))
    print fcCount
    if fcCount > 0:
        tbl = arcpy.CopyRows_management(lyr, r"in_memory\temp2")
        arcpy.AddMessage("Checking for Feature Class Name...")
        arcpy.AddMessage("Appending...")
        print "Appending..."
        arcpy.Append_management(tbl, table_path, "NO_TEST")
        print "Checking for Feature Class Name..."
        featureClassName(table_path)
        del fc, tbl, lyr, fcCount
        arcpy.Delete_management(r"in_memory\temp")
        arcpy.Delete_management(r"in_memory\temp2")
    else:
        arcpy.AddMessage("Pass... " + fc)
        print ("Pass... " + fc)
        del fc, lyr, fcCount
        arcpy.Delete_management(r"in_memory\temp")
        pass**

This code is the main loop for the feature classes within the dataset that i create a new layer/table to use for copying the data to the table. The data for Feature Class Name and OID dont have data to push, so thats where I am stuck.

Thanks Everybody

Answer

MikeHunter picture MikeHunter · Nov 18, 2012

You have a number of things wrong. First, you are not setting up the cursor correctly. It has to be a updateCursor if you are going to update, and you called a searchCursor, which you called incorrectly, by the way. Second, you used = (assignment) instead of == (equality comparison) in the line "if row.FEATURECLASS ... Then 2 lines below that, your indentation is messed up on several lines. And it's not clear at all that your function knows the value of fc. Pass that as an arg to be sure. Bunch of other problems exist, but let's just give you an example that will work, and you can study it:

def featureClassName(table_path, fc):
    '''Will update the FEATURECLASS FIELD in table_path rows with 
      value of fc (string) where FEATURECLASS field is currently null '''

    arcpy.AddMessage("Calculating Feature Class Name...")
    print "Calculating Feature Class Name..."

    #delimit field correctly for the query expression
    df = arcpy.AddFieldDelimiters(fc, 'FEATURECLASS')
    ex = df + " is NULL"
    flds = ['FEATURECLASS']
    #in case we don't get rows, del will bomb below unless we put in a ref 
    #to row 
    row = None
    #do the work
    with arcpy.da.UpdateCursor(table_path, flds, ex) as cursor:
        for row in cursor:
            row[0] = fc #or basename, don't know which you want
            cursor.updateRow(row)
    del cursor, row

Notice we are now passing the name of the fc as an arg, so you will have to deal with that in the rest of your code. Also it's best to use AddFieldDelimiter, since different fc's require different delimiters, and the docs are not clear at all on this (sometimes they are just wrong).

good luck, Mike