Select to view content in your preferred language

Sequential numbering

4100
6
Jump to solution
09-10-2015 09:13 AM
CCWeedcontrol
Frequent Contributor

I have two layers with building footprints I need to update one of them. After merging layers I have some features with an sequential number in the field Bld_ID field but some don't. So i need to update the Bld_ID with sequential number and they start with "Bld": example bld000001, bld00002, bld00003, etc. So some how i need to find the last highest sequential number in the field Bld_ID then add 1 to that and use that to update the buildings i have selected.

I have a code i used for something similar but i am running into the an error with this any help would be appreciated:

Runtime error

Traceback (most recent call last):

  File "<string>", line 30, in <module>

IndexError: list index out of range

import arcpy, sys, re, os  
from arcpy import env  
import string 

arcpy.env.qualifiedFieldNames = False      
arcpy.env.overwriteOutput = True
       
Bld = "BuildingFootprints" #target point feature class

BldCount = int(arcpy.GetCount_management(Bld).getOutput(0))

dsc = arcpy.Describe(Bld) 
selection_set = dsc.FIDSet         
if len(selection_set) == 0:       
    print "There are no features selected"  
             
elif BldCount >= 1:
    #Gets the highest AddressID and calculates new point to assign new highest AddressID
    Bld_list = []      
    with arcpy.da.SearchCursor(Bld, ["Bld_ID"]) as cursor:      
        for row in cursor:      
            try:      
                if "Bld" in row[0]:      
                    Bld_list.append(int(row[0].strip("Bld")))         
            except TypeError:      
                pass              
    del cursor      
          
    Bld_list.sort()      
    Bld_ID = Bld_list[-1] + 1      
    Bld_ID = 'Bld' + str(Bld_ID)

    with arcpy.da.UpdateCursor(Bld, "Bld_ID") as rows:       
        for row in rows:       
            row[0] = Bld_ID             
            rows.updateRow(row)  
        del row       
        del rows 
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

I believe the problem is that you are running this in ArcMap, so your variable Bld is running off the selection of the feature class rather than the entire feature class.  This is why the list was returning empty.   You can set up two variables:

arcpy.env.workspace = r"C:\temp\python\test.gdb"

BldSelection = "BuildingFootprints"   
Bld = arcpy.env.workspace + os.sep + "BuildingFootprints" #target point feature class 

You will also need to update your updateCursor to add to the Bld_ID.  Try the following:

import arcpy, os
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

arcpy.env.qualifiedFieldNames = False        
arcpy.env.overwriteOutput = True  

BldSelection = "BuildingFootprints"         
Bld = arcpy.env.workspace + os.sep + "BuildingFootprints" #target point feature class  
  
BldCount = int(arcpy.GetCount_management(Bld).getOutput(0))  
  
dsc = arcpy.Describe(BldSelection)   
selection_set = dsc.FIDSet           
if len(selection_set) == 0:         
    print "There are no features selected"    
               
elif BldCount >= 1:  
    #Gets the highest AddressID and calculates new point to assign new highest AddressID  
    Bld_list = []        
    with arcpy.da.SearchCursor(Bld, ["Bld_ID"]) as cursor:        
        for row in cursor:
            try:        
                if "Bld" in row[0]:
                    
                    Bld_list.append(int(row[0].strip("Bld")))           
            except TypeError:        
                pass                
    del cursor        

    print Bld_list
        
    Bld_list.sort()        
    Bld_ID = Bld_list[-1] + 1        
      
  
    with arcpy.da.UpdateCursor(BldSelection, "Bld_ID") as rows:         
        for row in rows:            
            row[0] = 'Bld' + str(Bld_ID)
            Bld_ID += 1                    
            rows.updateRow(row)    
        del row         
        del rows   

Note:  You will need to update line 3 to the path of your feature class.

View solution in original post

6 Replies
DarrenWiens2
MVP Alum

Make sure len(Bld_list) > 0. If the list is empty, there is no item at Bld_list[-1].

0 Kudos
CCWeedcontrol
Frequent Contributor

The list shouldn't be empty, there are features that have a bld00001, bld00002, etc numbering in the Bld_ID field.

thanks for the reply.

0 Kudos
DarrenWiens2
MVP Alum

Just add a print Bld_list to figure out if the list is empty.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I believe the problem is that you are running this in ArcMap, so your variable Bld is running off the selection of the feature class rather than the entire feature class.  This is why the list was returning empty.   You can set up two variables:

arcpy.env.workspace = r"C:\temp\python\test.gdb"

BldSelection = "BuildingFootprints"   
Bld = arcpy.env.workspace + os.sep + "BuildingFootprints" #target point feature class 

You will also need to update your updateCursor to add to the Bld_ID.  Try the following:

import arcpy, os
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

arcpy.env.qualifiedFieldNames = False        
arcpy.env.overwriteOutput = True  

BldSelection = "BuildingFootprints"         
Bld = arcpy.env.workspace + os.sep + "BuildingFootprints" #target point feature class  
  
BldCount = int(arcpy.GetCount_management(Bld).getOutput(0))  
  
dsc = arcpy.Describe(BldSelection)   
selection_set = dsc.FIDSet           
if len(selection_set) == 0:         
    print "There are no features selected"    
               
elif BldCount >= 1:  
    #Gets the highest AddressID and calculates new point to assign new highest AddressID  
    Bld_list = []        
    with arcpy.da.SearchCursor(Bld, ["Bld_ID"]) as cursor:        
        for row in cursor:
            try:        
                if "Bld" in row[0]:
                    
                    Bld_list.append(int(row[0].strip("Bld")))           
            except TypeError:        
                pass                
    del cursor        

    print Bld_list
        
    Bld_list.sort()        
    Bld_ID = Bld_list[-1] + 1        
      
  
    with arcpy.da.UpdateCursor(BldSelection, "Bld_ID") as rows:         
        for row in rows:            
            row[0] = 'Bld' + str(Bld_ID)
            Bld_ID += 1                    
            rows.updateRow(row)    
        del row         
        del rows   

Note:  You will need to update line 3 to the path of your feature class.

CCWeedcontrol
Frequent Contributor

Jake the code you posted worked but it truncated the "zero's" like so Bld4, Bld5, Bld4. etc.

How can get them to be like so Bld00004, Bld00005, Bld00003, Bld001069?

0 Kudos
LanceCole
MVP Regular Contributor

Use ​'Bld%06d' %Bld_ID rather than 'Bld' + str(Bld_ID)