ClassList = ["Forest", "Vegetation"] FieldList = ["Land_Acres","Forest_Acres", "Forest_Percent", "Veg_Acres", "Veg_Percent"] m2ac = 0.0002471054 FcList = arcpy.ListFeatureClasses("*", "") for fc in FcList: areaTab1 = "Metrics_" + os.path.basename(fc) for field in FieldList: arcpy.AddField_management(fc, field, "DOUBLE") TabulateArea(fc, "GID", LC, "Class", areaTab1 , 1) #"UTC_Metrics_" + os.path.basename(fc) arcpy.JoinField_management(fc, "GID", areaTab1, "GID", ClassList)#"trees11_tab_" + os.path.basename(fc) rows = arcpy.UpdateCursor(fc) print str(fc) for row in rows: row.setValue(FieldList[0], (row.Shape_Area * m2ac))#Total Acres rows.updateRow(row) row.setValue(FieldList[1], row.getValue(ClassList[0]) * m2ac) #Forest Acres rows.updateRow(row) row.setValue(FieldList[2], (row.getValue(FieldList[1]) / row.Land_Acres) * 100) #Forest Percent rows.updateRow(row) row.setValue(FieldList[3], row.getValue(ClassList[1]) * m2ac) #Veg Acres rows.updateRow(row) row.setValue(FieldList[4], (row.getValue(FieldList[3]) / row.Land_Acres) * 100) #Veg Percent rows.updateRow(row) del row del rows
for fc in FcList: try: areaTab1 = "Metrics_" + os.path.basename(fc) for field in FieldList: arcpy.AddField_management(fc, field, "DOUBLE") TabulateArea(fc, "GID", LC, "Class", areaTab1 , 1) #"UTC_Metrics_" + os.path.basename(fc) arcpy.JoinField_management(fc, "GID", areaTab1, "GID", ClassList)#"trees11_tab_" + os.path.basename(fc) rows = arcpy.UpdateCursor(fc) print str(fc) for row in rows: try: row.setValue(FieldList[0], (row.Shape_Area * m2ac))#Total Acres rows.updateRow(row) row.setValue(FieldList[1], row.getValue(ClassList[0]) * m2ac) #Forest Acres rows.updateRow(row) row.setValue(FieldList[2], (row.getValue(FieldList[1]) / row.Land_Acres) * 100) #Forest Percent rows.updateRow(row) row.setValue(FieldList[3], row.getValue(ClassList[1]) * m2ac) #Veg Acres rows.updateRow(row) row.setValue(FieldList[4], (row.getValue(FieldList[3]) / row.Land_Acres) * 100) #Veg Percent rows.updateRow(row) except: pass del row del rows except: pass
Richard,
Did you ever find a way to address this? I'm doing the same thing with a tabulation of land cover types within watershed polygons. Not all of my seven possible land cover types are present in each watershed, so the update cursor has problems with fields that may or may not be present.
Greg
There are ways to avoid your code from crashing and only update those field that exist. I would probably use some dictionaries for this purpose and switch to the da cursors. If you are willing to drop a small part of your data I can have a look what I can do.
Kind regards, Xander
I'm posting a simplified table for this example. The table is the result of the Tabulate Area tool and it will be inside a file gdb created by my model. Let's just try to convert the square feet for each cover type to acres. The problem is that you never know which of seven possible cover types you'll have in this table. The path I've started down is this:
import arcpy
arcpy.env.workspace = r'G:\users\gis\gbacon\Watershed_Testing\valleyave\WatershedDelineation.gdb'
fldlist = arcpy.ListFields("TabSummary","","Double")
with arcpy.da.UpdateCursor("TabSummary",fldlist) as cursor:
for row in cursor:
row[0:] = row/43560
cursor.updateRow(row)
The update cursor needs a list of fields, but the fldlist variable can't be used here. The second issue seems to be in my use of row[0:] where I'm trying to say take each value from the first to the last of the possible seven and divide each by 43560. I'm very new to Python and I appreciate your help.
Hi Greg,
I'm afraid you added the XML and not the DBF file itself. Can you add it?
If I read the DBF into a fgdb I will have a similar situation as you have?