Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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")
share|improve this question
1  
What software are you using? Which version? What does your script look like so far? (Please edit the question to include these details) –  Vince Nov 1 at 18:55
 
It looks like you have an indentation error on your third last line of code where 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
 
I solved it! Final code posted in question. –  Brandon Nov 2 at 15:56
 
Awesome! Is there anything else you wanted to know about this question or is it solved? –  Paul Nov 2 at 16:04
2  
@whuber That was my first thought too but it seems like there is a clarified requirement that it "creates two buffer feature classes" so it would seem easiest to just Select out the Airports and buffer with one distance and then Select out the Seaplane Bases and buffer with the other i.e. four lines of code to do the actual work. I'll propose this as an Answer when/if the Question re=opens. –  PolyGeo Nov 3 at 0:06
show 4 more comments

1 Answer

These are the key four lines (plus an import arcpy) that I think you need:

import arcpy
arcpy.Select_analysis("C:/temp/fc.shp","C:/temp/airports.shp",""""Feature" = 'Airport'""")
arcpy.Buffer_analysis("C:/temp/airports.shp","C:/temp/apBuffer.shp","15000 Meters")
arcpy.Select_analysis("C:/temp/fc.shp","C:/temp/seaplaneBases.shp",""""Feature" = 'Seaplane Base'""")
arcpy.Buffer_analysis("C:/temp/seaplaneBases.shp","C:/temp/spbBuffer.shp","7500 Meters")

Naturally it can be embellished with some of your other code like setting your workspace and variables, etc, but I think cursors are overkill for something this straightforward.

share|improve this answer
 
Thanks for the tip! –  Brandon yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.