Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've got a python script that runs on the Field Calculator of a ESRI File Database. Right now it runs correctly in that it will increment the last three digits of the value. But this is a script that is run as part of post processing so it will be run everytime there are new entries in the feature class. My question is how can I modify this so that every time it runs it will look at the last four digits of the FacilityID and increment from there below is a screenshot of my attribute table:

enter image description here

EDIT: And below is my Python script I need to modify.

cur = arcpy.SearchCursor("Telecommunications Line")
fac_id_values = [int(row.FACILITYID[-4:]) for row in cur]

maximum_value = max(fac_id_values)
if maximum_value:
 rec= maximum_value 
else:
 rec = 0

def autoIncrement(): 
 global rec 
 pStart = 1  
 pInterval = 1 
 if (rec == 0):  
  rec = pStart  
 else:  
  rec += pInterval  
 return "COMLI" + "11" +  format(rec, '03d')

Ok after running the code above I got this as a result. It overwrote the previous values as shown above. The First item should have a FacilityID of COMLI111001. enter image description here

share|improve this question

1 Answer 1

You can loop through the FC first and find them maximum value.

Example:

import arcpy
nullvalues = ["", " ", None, 0]
# Loop through and get Max value
cur = arcpy.SearchCursor("Telecommunications Line")
fac_id_values = [int(row.FACILITYID[-4:]) for row in cur if row.FACILITYID not in nullvalues]

max_value = max(fac_id_values)

# Loop through and Calculate new values
cur = arcpy.UpdateCursor("Telecommunications Line")
for row in cur:
    max_value = max_value + 1
    if row.FACILITYID in nullvalues:
        row.FACILITYID = "COMLI1" + str(format(max_value,'04d'))
        cur.updateRow(row)

This should ensure that you only update Null values.

share|improve this answer
    
list comprehensions are awesome! –  Kevin R Dyke May 9 at 18:23
    
Yes, this could all be put into 1 line, but for readablity, i chose to seperate it into 3. max_value = max([int(row.FACILITYID[-4:]) for row in arcpy.SearchCursor("YOUR FC")]) –  dklassen May 9 at 18:27
    
I'm always a fan of readability, and the list comprehension allowed you to take what would be even more verbose language, compress it to a single line, and still end up with three highly readable lines. Features like these make me enjoy working with Python after toiling for too long with Javascript ;) –  Kevin R Dyke May 9 at 18:31
    
Totally agree!! –  dklassen May 9 at 18:37
    
So I was going to implement this so if Maximum_value is not null then set rec=maximum_value right? –  Mark Basler May 9 at 19:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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