Monday, 11 January 2016

arcgis desktop - Using auto increment code with counter


I was wondering if it's possible to use the standard auto increment code to work on groups of records:


FeatureClass = "newFC" 
recs = collections.defaultdict(int) autoIncrementEXP = "autoIncrement()"
expCalc = """def autoIncrement(!Group!): recs[!Group!] += 1 return recs[!Group!]"""
arcpy.CalculateField_management(FeatureClass, "Number",autoIncrementEXP,"PYTHON",expCalc)

So instead of having the auto increment portion of the script stop when it reaches the end of the records, have it create sequential numbers using field value groups if that makes sense. You would start with something like the Group field below and end with the Number field containing the increment values:


Group   Number

A 1
A 2
A 3
B 1
B 2
C 1
C 2
C 3
C 4
C 5


Thanks for any insight you can provide.



Answer



You'll need to pass in the value of your group field to the autoIncrement function now, but this would work:


import collections

recs = collections.defaultdict(int)

def autoIncrement(field_value):
recs[field_value] += 1

return recs[field_value]

So something like this:


FeatureClass = "newFC"
expCalc = "import collections\n\nrecs = collections.defaultdict(int)\n\ndef autoIncrement(field_value):\n recs[field_value] += 1\n return recs[field_value]\n"
arcpy.CalculateField_management(FeatureClass, "Number", "autoIncrement(!Group!)","PYTHON", expCalc)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...