0

I'm attempting to write a script that will add an incremented value to a field based on a selection of records.

For example, the script will select the first 300 records in a feature class and then using calculate field assign a value of 1 to a field called "Week_Assignment". It will then select the next 300 records and assign a value of 2 to the same field, etc.

I'm able to select the records, but not able to calculate the field with an incremented value. I found some generic code on an Esri help forum that will auto increment every row in a field, but I need to modify it to add the same value to all rows in a selection.

Here's the sample code:

rec=0 
def autoIncrement(): 
 global rec 
 pStart = 1  
 pInterval = 1 
 if (rec == 0):  
  rec = pStart  
 else:  
  rec += pInterval  
 return rec

autoIncrement()
  • Select and calculate would require arcpy (or manual Calculate Field with the selection), and probably not very efficient. The autoIncrement function could be changed to include a different value based on the rec counter. Will you always be using 300 rows for each week? – smiller Apr 17 at 19:14
  • I'm attempting to create a tool that will allow for inputs from the user, like the input feature class and number of analysts who will analyze the feature class. The number of rows selected will change based on the number of records in the feature class and the number of analysts. – AEL Apr 17 at 19:18
  • 1
    Unless your set has records removed simple objectid // 300 will do. – FelixIP Apr 17 at 20:36
  • Perhaps this answer could assist as well? gis.stackexchange.com/questions/200150/… – Keagan Allan Apr 17 at 22:23
  • 1
    I did try using the suggested code in this thread and it just incremented over all of the rows in each selection. Selection 1 (rows 1 to 300) were assigned a value 1 to 300 in the "Week_Assignment" field, etc.. I suspect that this happened because I was using an integer in the autoIncrement expression rather than an ID field like in the example. Also, I'm using a variable to determine the number of rows that should be selected and I'm unclear about how to use the variable in the autoIncrement expression. I receive an error every time I try. – AEL Apr 18 at 0:12
1

I modified the sample code provided to account for a user-specified group interval. This uses division to identify the group number. I add pStart to account for the groups starting with 1.

rec=0 
def autoIncrement(groupinterval): 
 global rec
 pStart = 1  
 pInterval = 1 
 if (rec == 0):  
  pGroup = pStart
  rec = pStart 
 else:
  pGroup = int(rec/groupinterval) + pStart #check with your version of Python; should be floor operation for integer division. 
  rec += pInterval  
 return pGroup

autoIncrement(300)

Since autoincrement(300) is a string parameter for the CalculateField geoprocessing tool, you could modify to use a variable in place here and then build the string, e.g.

usrval = 300 
increxp = "autoincrement({})".format(usrval)
arcpy.CalculateField_management(week_lyr, "Week_Assignment", increxp, "PYTHON", "rec=0 \\ndef autoIncrement(groupinterval): \\n global rec\\n pStart = 1 \\n pInterval = 1 \\n if (rec == 0): \\n pGroup = pStart\\n rec = pStart \\n else:\\n pGroup = int(rec/groupinterval) + pStart \\n rec += pInterval \\n return pGroup")
  • Thanks for sharing this modified code. I tried using it in my script within the calculate field function and it is assigning everything a value of 1. I should note that I am creating the row selections within a loop and then using the calculate field function to update the "Week_Assignment" field within the selection. Not sure if that is contributing to the issue. – AEL Apr 18 at 0:00
  • The code from this answer uses the entire table, not a selection within the table. The groupinterval value controls how many rows are included for each "pGroup" value, so if you were to copy this code directly with a set of 300 rows you're only going to get 1 for every row; you'd get 1's and 2s if you have slightly more than 300; My recommendation would be to not use it with a selection. – smiller Apr 18 at 15:18
  • Ah, that makes sense. The number of records that will need to be calculated will change based on an input from the user. I tried using an inline variable in the autoIncrement expression (autoIncrement(%tiles_wk%)), but I'm receiving a syntax error. Do you know if it's possible to use a variable as part of the expression input? – AEL Apr 18 at 16:37
  • Please clarify, because my code above already uses the variable "groupinterval". – smiller Apr 18 at 16:39
  • It may be more helpful to show you how I'm using your modified code within my script: arcpy.CalculateField_management(week_lyr, "Week_Assignment", "autoIncrement(300)", "PYTHON", "rec=0 \\ndef autoIncrement(groupinterval): \\n global rec\\n pStart = 1 \\n pInterval = 1 \\n if (rec == 0): \\n pGroup = pStart\\n rec = pStart \\n else:\\n pGroup = int(rec/groupinterval) + pStart \\n rec += pInterval \\n return pGroup") – AEL Apr 18 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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