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
Solved! Go to Solution.
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.
Make sure len(Bld_list) > 0. If the list is empty, there is no item at Bld_list[-1].
Just add a print Bld_list to figure out if the list is empty.
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.