This question is related to Correct usage of a counter using arcpy.da.UpdateCursor. What I would like to do is use a cursor with a where clause to select a number of records. For each row, the values in two fields, TYPE_A
and TYPE_B
should be compared. If those two values are not the same, the field NUM_ID
should be updated with the current value of the counter.
Once that is done, all but one of the rest of the records in the selection should be updated with the value of the incremented counter.
Some example data:
FEATURE 1 :NUM_ID = 4 TYPE_A = NEW TYPE_B = NEW
FEATURE 2 :NUM_ID = 4 TYPE_A = NEW TYPE_B = EXISTING
FEATURE 3 :NUM_ID = 4 TYPE_A = NEW TYPE_B= NEW
FEATURE 4 :NUM_ID = 4 TYPE_A = NEW TYPE_B= NEW
FEATURE 5 :NUM_ID = 11 TYPE_A = NEW TYPE_B = EXISTING
FEATURE 6 :NUM_ID = 14 TYPE_A = NEW TYPE_B = NEW
FEATURE 7 :NUM_ID = 11 TYPE_A = NEW TYPE_B = EXISTING
The 4 features with NUM_ID = 4
would be selected and Feature 2 will be given the NUM_ID 21, as its values for TYPE_A and TYPE_B are different. The next step would be to update all but one of the other 3 records by incrementing the counter. The next selection would be the 2 features with NUM_ID = 11
. As both the records have different values for both fields, they would both be updated. The last selection will be for NUM_ID = 4. As both its values for the fields are the same, and it is the only record in its selection set, no update is needed.
The result:
FEATURE 1 :NUM_ID = 23 TYPE_A = NEW TYPE_B = NEW
FEATURE 2 :NUM_ID = 21 TYPE_A = NEW TYPE_B = EXISTING
FEATURE 3 :NUM_ID = 22 TYPE_A = NEW TYPE_B= NEW
FEATURE 4 :NUM_ID = 4 TYPE_A = NEW TYPE_B= NEW
FEATURE 5 :NUM_ID = 11 TYPE_A = NEW TYPE_B = EXISTING
FEATURE 6 :NUM_ID = 14 TYPE_A = NEW TYPE_B = NEW
FEATURE 7 :NUM_ID = 25 TYPE_A = NEW TYPE_B = EXISTING
The code I have:
getNum("%Selected Features%")
def getNum(ftr):
lst = [4, 11, 14, 15] #values for SQL expression
x = 20
fields = ('TYPE_A', 'TYPE_B', 'NUM_ID')
assert "NUM_ID" in [f.name for f in arcpy.ListFields(ftr)], "NUM_ID field no in feature class"
for l in lst:
y = x + 1
whereclause = "%(sql_name)s = '%(l)s' """ {"sql_name":arcpy.AddFieldDelimiters(ftr, "NUM_ID"), "l":l}
with arcpy.da.UpdateCursor(ftr, fields, whereclause) as cursor:
for row in cursor:
if row[0] ! = row[1]:
row[2] = y
#some condition here
cursor.updateRow(row)
y += 1
TYPE_A
only has one value in it for all features i.e NEW
. TYPE_B
could have any value. It does not matter which feature gets updated amongst those which have the same values for both fields (NEW
), as long as one of them keeps the original value for NUM_ID. While this code updates the row correctly where the two fields don't match, I do not know how to let one of the records keep the original value, while the rest should be updated.
slct
variable is undefined in this example. Also, in your previous question the whereclause selected on the NUM_ID field, but in this one it selects on the LABEL field, which is not defined in thefields
list you pass to the cursor. So which is it?