Sequential Number?

540
4
Jump to solution
07-10-2013 05:54 AM
ionarawilson1
Occasional Contributor III
Does anybody know if it is possible to calculate a sequential number for an attribute for a feature class that has a definition query for just one record selected? Thank you!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor
Ionara,

Don't really have time to try/test this, but here is what I would try.

Sort the table by the field you want to update and get the max value with
maxValue = arcpy.SearchCursor(infc, "", "", "", sortfield+" D").next().getValue("OBJECTID")
  or the code you have above.  However, make sure the infc is the actuall feature class, not a featurelayer.  If in tool, don't pick from dropdown list, click browse and navigate to the FC itself or hardcode in the script)

I say to use the FC itself as the selection you have is on the featureLayer (since that is what is supported) and the def query is in the mxd, so you can iterate through all the values, regardless of def query or selection if you use the FC directly. (I think this is true.  It works as stand alone, but not positive from within ArcMap, it may on the fly it again, you will have to test)
Now you have the max value, you can increment as you did above maxValue += 1
You already have a selection on the feature layer, you now have the next value, so, since it is only one record that you are updating, I'd just use arcpy.CalculateField_management to update that one record.

If you have multiple records in your selection, that is where I'd use your UpdateCursor, filtered to null values only AND sorted by OID (that way, they will be "sequential" relative to the order in the table).

Then just increment the maxValue each loop and updateRow.

Hope this gives you some ideas that actually help,

R_

View solution in original post

0 Kudos
4 Replies
JimCousins
MVP Regular Contributor
Not too certain what you are asking. If you need to calculate sequential numbers, simple python code in field calculator (from Arc Help):

Calculate a sequential ID or number based on an interval.

Parser:
Python

Expression:
autoIncrement()

Code Block:
rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
  rec = pStart
else:
  rec = rec + pInterval
return rec

And if you have a definition query, it will only calculate values for the selected records.

Regards,
Jim
0 Kudos
ionarawilson1
Occasional Contributor III
Hi Jim,

Thanks but I am writing a script, I am not using the field calculator.
0 Kudos
RhettZufelt
MVP Frequent Contributor
Ionara,

Don't really have time to try/test this, but here is what I would try.

Sort the table by the field you want to update and get the max value with
maxValue = arcpy.SearchCursor(infc, "", "", "", sortfield+" D").next().getValue("OBJECTID")
  or the code you have above.  However, make sure the infc is the actuall feature class, not a featurelayer.  If in tool, don't pick from dropdown list, click browse and navigate to the FC itself or hardcode in the script)

I say to use the FC itself as the selection you have is on the featureLayer (since that is what is supported) and the def query is in the mxd, so you can iterate through all the values, regardless of def query or selection if you use the FC directly. (I think this is true.  It works as stand alone, but not positive from within ArcMap, it may on the fly it again, you will have to test)
Now you have the max value, you can increment as you did above maxValue += 1
You already have a selection on the feature layer, you now have the next value, so, since it is only one record that you are updating, I'd just use arcpy.CalculateField_management to update that one record.

If you have multiple records in your selection, that is where I'd use your UpdateCursor, filtered to null values only AND sorted by OID (that way, they will be "sequential" relative to the order in the table).

Then just increment the maxValue each loop and updateRow.

Hope this gives you some ideas that actually help,

R_
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Rhett, I will try that!!!
0 Kudos