Is there any way to optimize this code? It creates two buffer feature classes based on two different fields in the shapefile attribute table
import arcpy
from arcpy import env
## Set Workspace
env.workspace = "I:\GIS Programming\Week 11 - Manipulating Spatial Data\data"
fc = "airports.shp"
## Details for new field in attribute table
newfield = "BUFFER_DISTANCE"
fieldtype = "TEXT"
fieldname = arcpy.ValidateFieldName(newfield)
arcpy.AddField_management(fc, fieldname, fieldtype, "", "", 12)
print "New Field" + newField + "Created."
print "Updating Buffer Distances"
## BUFFER_DISTANCE is added to table as BUFFER_DIS for some reason
fields = ("FEATURE", "BUFFER_DIS")
## Create update cursor for feature class
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[0] == "Airport":
row[1] = "15000 METERS"
else:
row[1] = ""
cursor.updateRow(row)
print "Airport BUFFER_DISTANCE field updated"
print "Creating buffer feature class"
##Create buffer
arcpy.Buffer_analysis(fc, "airport_buffer", "BUFFER_DIS")
## Create update cursor for feature class
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[0] == "Seaplane Base":
row[1] = "7500 METERS"
else:
row[1] = ""
cursor.updateRow(row)
print "Seaplane BUFFER_DISTANCE field updated"
print "Creating Buffer feature class"
##Create buffer
arcpy.Buffer_analysis(fc, "seaplane_buffer", "BUFFER_DIS")
cursor.update(row)
needs to be indented another space. If that is not it, then can you let us know what is wrong when you run this code - does it give an error or just not give the result you hope for. Either way can you edit your Question to make clear what part of what you observed did not match what you were expecting, please? – PolyGeo Nov 2 at 8:18