mercredi 5 septembre 2018

Not sure how to phrase this "Updating inserted row in field with max value plus 1 in a for loop in an else statement"

My problem is in the else statement. I want the Pole_Old "uid" field in the inserted row to be the max value of the Pole_Old "uid" field plus 1. For some reason it's taking the max value of the Pole_New instead of the Pole_Old, and labeling both uid numbers of the inserted rows the same number.

import arcpy, operator

#Input feature class (Old)
Pole_Old = 'C:\\Temp\\MISC\\Client_Data_Processing\\2018_06_05\\Processing.gdb\\Pole'

#Target feature class (New)
Pole_New = 'C:\\Temp\\MISC\\Client_Data_Processing\\2018_08_29\\Processing.gdb\\Pole'

def CompareAndUpdateOldUID(Pole_Old, Pole_New):
#Get list of values in field from target feature class
    checkValues1 = [r[0] for r in arcpy.da.SearchCursor(Pole_Old, 'POLE_NO')]
    checkValues2 = [r[0] for r in arcpy.da.SearchCursor(Pole_Old, 'cat')]
    uidList = [r[0] for r in arcpy.da.SearchCursor(Pole_Old, 'uid')]

#Get max uid value of Pole_Old
    maxUID = max(uidList)
    arcpy.AddMessage(maxUID)

#Get list of fields
    fields = ["POLE_NO", "cat", "uid"]

#Get index of check fields
    index1 = fields.index("POLE_NO")
    index2 = fields.index("cat")

#Create search cursor to iterate through fields of Pole_New
    inCursor = arcpy.da.SearchCursor(Pole_New, fields)

#Create insert cursor to iterate through fields of Pole_Old and add new features from Pole_New
    cursor = arcpy.da.InsertCursor(Pole_Old, fields)

#Iterate
    for row in inCursor:
        #Get value to check
        checkValue1 = row[index1]
        checkValue2 = row[index2]
        #Check if value is in target fc
        if checkValue1 in checkValues1:
            if checkValue2 in checkValues2:
                continue

        else:
            cursor.insertRow(row)
            with arcpy.da.UpdateCursor(Pole_Old, fields) as cursor:
                for row in cursor:
                    row[2] = maxUID = row[2] + 1
                    cursor.updateRow(row)
                    uidList.append(row[2])
            cursor = arcpy.da.InsertCursor(Pole_Old, fields)

    del inCursor
    del cursor

    CompareAndUpdateOldUID(Pole_Old, Pole_New)

Aucun commentaire:

Enregistrer un commentaire