2

I would like to take sequential numbers up a level and only apply it to features of a certain type. Say I have two feature types, black and white features, and it's denoted by the field "Type". I want my sequential numbers code to skip all white features and pick up where it left off with the last black feature. I'm sure it's some type of If/Then statement but i can't figure it out. Here's the basic code where it returns sequential numbers by my sort field, which works just fine:


import arcpy

sortFeat = r"P:\path\Receivers.shp" #featurepath
sortField = 'Ycoord' #Base Field to sort, which is 
idField   = 'R_ID' #Field to populate sequential numbers
rec=0

def autoIncrement():
    global rec
    pStart = 36 #number of features
    pInterval = 1 
    if (rec == 0): 
        rec = pStart 
    else: 
        rec -= pInterval 
    return "R" + str(rec)

rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField)

for row in rows:
    row.setValue(idField, autoIncrement())
    rows.updateRow(row) 
del row, rows

print "Finish"

So how can i make this only apply to certain attribute type and not all within the layer?

I am using Python 3, but this currently only works in a 2.7 environment.

2
  • 1
    Use da.UpdateCursor instead of old one which is slower etc. Select the black ones first or use a sql statement in the da.UpdateCursor.
    – Bera
    Commented Jan 24, 2020 at 18:47
  • gis.stackexchange.com/questions/200150/…
    – FelixIP
    Commented Jan 24, 2020 at 19:50

1 Answer 1

1

This should work,

fields = ['R_ID', 'Type']
with arcpy.da.UpdateCursor(sorFeat, fields) as cursor:
    # Update R_ID field based on Type field.
    # Type is either WHITE, BLACK. 
    for row in cursor:
        # Update Id field if Type is BLACK.
        if row[1] == 'BLACK':
            row[0] = autoIncrement()
            cursor.updateRow(row)

You could alse use the where parameter of arcpy.da.UpdateCursor with the condition "Type='BLACK'",like @BERA mention in the comments, to get only the rows you are going to assign the new id.

Edited to include the proposed code it in the provided code. In this case I use the where parameter of arcpy.da.UpdateCursor to get only the features to update, that's why I've omitted the if statement. Finally, I add the sql_clause parameter to sort the features, which I forgot before.

import arcpy

sortFeat = r"P:\path\Receivers.shp" #featurepath
sortField = 'Ycoord' #Base Field to sort, which is 
idField   = 'R_ID' #Field to populate sequential numbers
rec=0

def autoIncrement():
    global rec
    pStart = 36 #number of features
    pInterval = 1 
    if (rec == 0): 
        rec = pStart 
    else: 
        rec -= pInterval 
    return "R" + str(rec)

fields = ['R_ID', 'Type']
with arcpy.da.UpdateCursor(
    sorFeat,
    fields,
    where_clause='Type="BLACK"',
    sql_clause=(None, 'ORDER BY Ycoord')
    ) as cursor:
    for row in cursor:
        row[0] = autoIncrement()
        cursor.updateRow(row)

print "Finish"
2
  • Would you mind applying this to the whole code that I provided in the question? I can't figure out how to implement it. Commented Jan 24, 2020 at 19:37
  • I've updated the answer, hope it help you.
    – cabesuon
    Commented Jan 24, 2020 at 20:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.