import arcpy
import os
def getFieldNames(fc):
fieldList = []
for f in arcpy.ListFields(fc):
fieldList.append(str(f.name))
return fieldList
Recalss_area = """
def Reclass(area1):
if (area1 <= 250):
return 0
elif (area1 > 250 and area1 <= 500):
return 1
elif (area1 > 500 and area1 <= 1000):
return 2
elif (area1 > 1000 and area1 <= 2000):
return 3
elif (area1 > 2000 and area1 <= 4000):
return 4
elif (area1 > 4000 and area1 <= 7000):
return 5
elif (area1 > 7000 and area1 <= 10000):
return 6
elif (area1 > 10000):
return 7
"""
Reclass_shape = """
def Reclass(shp1):
if (shp1 <= 18):
return 0
elif (shp1 <= 25):
return 1
elif (shp1 > 25 and shp1 <= 30):
return 2
elif (shp1 > 30 and shp1 <= 40):
return 3
elif (shp1 > 40 and shp1 <= 55):
return 4
elif (shp1 > 55 and shp1 <= 70):
return 5
elif (shp1 > 70 and shp1 <=90):
return 6
elif (shp1 > 90):
return 7
"""
Reclass_angle = """
def Reclass(angle):
if (angle <= -77.5):
return 0
elif (angle > -77.5 and angle <= -45):
return 1
elif (angle > -45 and angle <= -22.5):
return 2
elif (angle > -22.5 and angle <=0):
return 3
elif (angle > 0 and angle <= 22.5):
return 4
elif (angle > 22.5 and angle <= 45):
return 5
elif (angle > 45 and angle <=77.5):
return 6
elif (angle > 77.5):
return 7
"""
Reclass_extn = """
def Reclass(extn):
if (extn <= 1.5):
return 0
elif (extn > 1.5 and extn <= 2):
return 1
elif (extn > 2 and extn <= 3):
return 2
elif (extn > 3 and extn <=5):
return 3
elif (extn > 5 and extn <= 7):
return 4
elif (extn > 7 and extn <= 9):
return 5
elif (extn > 9 and extn <=10):
return 6
elif (extn > 10):
return 7
"""
list = ['Area_8', 'Shape_8', 'Angle_8', 'extn_8']
arcpy.env.workspace = r"C:\Users\user\Downloads\data2\b1"
fcl = arcpy.ListFeatureClasses()
for shp in fcl:
print shp
fieldList = getFieldNames(shp)
print fieldList
for p in list:
if p in fieldList:
arcpy.DeleteField_management(shp, p)
arcpy.AddField_management(shp, "Area_8", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(shp, "Shape_8", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(shp, "Angle_8", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(shp, "Extn_8", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
try:
arcpy.CalculateField_management(shp, "Area_8", "Reclass(!area1!)", "PYTHON_9.3", Recalss_area)
except arcpy.ExecuteError:
print arcpy.GetMessages()
try:
arcpy.CalculateField_management(shp, "Shape_8", "Reclass(!shp1!)", "PYTHON_9.3", Reclass_shape)
except arcpy.ExecuteError:
print arcpy.GetMessages()
try:
arcpy.CalculateField_management(shp, "Angle_8", "Reclass(!angle!)", "PYTHON_9.3", Reclass_angle)
except arcpy.ExecuteError:
print arcpy.GetMessages()
try:
arcpy.CalculateField_management(shp, "Extn_8", "Reclass(!extn!)", "PYTHON_9.3", Reclass_extn)
except arcpy.ExecuteError:
print arcpy.GetMessages()
fieldList2 = getFieldNames(shp)
print fieldList2
|