Select to view content in your preferred language

Sequential ID by Two Fields

1079
4
08-28-2012 01:24 PM
AllenSmith
Deactivated User
Greetings.  I am working on a project trying to get sequential Plan ID numbers for polygons produced in different year and different counties.  I am using the field "County" to derive my County ID number (it is a string) and a "Year" field to derive my year value (alos a string.  The format I am looking for is:
2012-401-1
2012-401-2
2012-401-3
2012-363-1
2012-363-2
2011-401-1
2011-401-2

Any help that y'all could provide would be greatly appreciated.
Tags (2)
0 Kudos
4 Replies
RichardFairhurst
MVP Alum
Greetings.  I am working on a project trying to get sequential Plan ID numbers for polygons produced in different year and different counties.  I am using the field "County" to derive my County ID number (it is a string) and a "Year" field to derive my year value (alos a string.  The format I am looking for is:
2012-401-1
2012-401-2
2012-401-3
2012-363-1
2012-363-2
2011-401-1
2011-401-2

Any help that y'all could provide would be greatly appreciated.


Use the code and tool configuration shown in my post at position # 87 of this thread on auto-sequential numbers to fill in a long field with a sequential number that restarts with each new instance of a unique multi-field case value grouping.  The code and tool design in that post allows you to subsort on fields that are not part of the multi-field case grouping, so you can ensure that the sequence is numbered according to the sort order of the actual dates of the plans, despite the fact that those actual dates do not contribute to the unique multi-field case groupings. 

While you are welcome to adapt the code from what I have posted to directly populate your text field, I suggest using the tool as it is written to first store the sequential group subnumbers in a Long field and then do a separate calculation to concatenate the three fields into your desired text field format.  Numbers are always better than text for analysis and sorting of numeric values and I always try to have concatenated components in separate fields for manipulations I don't imagine I will need when I first create them.  For the concatenation you will need to be sure to account for leading zero formatting if in any case there are more than 9 instances within a single year and county combination (it is bound to happen in at least one year, either in the past of in the future).  I would always make allowance for at least 99 plans per year per County.  Search on the Python format syntax for creating strings with standarized leading zero formatting.
0 Kudos
ChrisSnyder
Honored Contributor
How about something like this:
***untested code***

import arcpy
myFC = r"C:\temp\test.shp"
yearField = "YEAR"
countyField = "COUNTY_CD"
sequenceField = "SEQUENCE" #Integer
concatField = "CONCAT" #text of sufficient length
trackingDict = {}
updateRows = arcpy.UpdateCursor(myFC)
for updateRow in updateRows:
    yearValue = updateRow.getValue(yearField)
    countyValue = updateRow.getValue(countyField)
    if (yearValue, countyValue) in trackingDict:
        trackingDict[(yearValue, countyValue)] = trackingDict[(yearValue, countyValue)] + 1
    else:
        trackingDict[(yearValue, countyValue)] = 1
    sequenceId = trackingDict[(yearValue, countyValue)]
    updateRow.setValue(sequenceField, sequenceId)
    updateRow.setValue(concatField, str(yearValue) + "-" + str(countyValue) + "-" + str(sequenceId))
updateRows.updateRow(updateRow)
del updateRow, updateRows 
0 Kudos
RichardFairhurst
MVP Alum
How about something like this:
***untested code***

import arcpy
myFC = r"C:\temp\test.shp"
yearField = "YEAR"
countyField = "COUNTY_CD"
sequenceField = "SEQUENCE" #Integer
concatField = "CONCAT" #text of sufficient length
trackingDict = {}
updateRows = arcpy.UpdateCursor(myFC)
for updateRow in updateRows:
    yearValue = updateRow.getValue(yearField)
    countyValue = updateRow.getValue(countyField)
    if (yearValue, countyValue) in trackingDict:
        trackingDict[(yearValue, countyValue)] = trackingDict[(yearValue, countyValue)] + 1
    else:
        trackingDict[(yearValue, countyValue)] = 1
    sequenceId = trackingDict[(yearValue, countyValue)]
    updateRow.setValue(sequenceField, sequenceId)
    updateRow.setValue(concatField, str(yearValue) + "-" + str(countyValue) + "-" + str(sequenceId))
updateRows.updateRow(updateRow)
del updateRow, updateRows 


This code will definitely be faster, provided that the assignment of the sequential ID has no relationship to the order of any other field that is not included in the concatenated name.  That never applies for my needs (the sequential number always represents the order of one or more fields that are not included in the concatenation, such as dates, street names, or street names and measure), but if it did this would be faster.
0 Kudos
AllenSmith
Deactivated User
Thanks so much for your help.  Chris's code worked well in that it populated the field with the proper number.  The problem I am having is that newly digitized polygons are also numbered as 1.  Should the code be ran on the entire dataset each time after a new polygon is created?  I was hoping to assign this sequential number after the polygon is digitized when I am using sys.argvs to assign other attributes.  I can always create another tool button that runs this script on the full dataset.  Thoughts?  Thanks again.
0 Kudos