Why isn't my search cursor working?

1106
3
09-30-2016 10:02 AM
TravisPreston2
New Contributor II

I am working through a tutorial book and trying to create a buffer based on a search cursor. When I run the script it creates a buffer but all of the buffers are at 100 rather than what I have set as the output parameters for the buffer. any help would be great. Thanks!

#Import module and environment
import arcpy
from arcpy import env

env.environment = r"M:\NRD_Maps\Travis_Working\Training\GIS Tutorial for Python Scripting\GISTPython\Data\City of Oleander.gdb\Well_Data"
env.overwriteoutput = True

#Create variables with the name of the subject feature class
Lights = 'StreetLights'

#The buffer distance is dependant on the light types
#Check to see the light type
LightCursor = arcpy.da.SearchCursor(Lights,['Type'])
for row in LightCursor:
   Type = row[0]
#MV = 125 ft
#MVH = 160 ft
#SV = 100 ft
#SVH = 200 ft
   if Type == 'MV':
      Buffer = 125
   elif Type == 'MVH':
      Buffer = 160
   elif Type == 'SV':
      Buffer = 100
   else:
      Buffer = 200

#Perform the buffer
arcpy.Buffer_analysis(Lights, \
r"M:\NRD_Maps\Travis_Working\Training\GIS Tutorial for Python Scripting\GISTPython\MyExercises\MyAnswers.gdb\EX_2_3\lightBuffer", \
Buffer)
print "New buffer created"

0 Kudos
3 Replies
DarrenWiens2
MVP Honored Contributor

The parameters for Buffer are (input, output, buffer distance or field, ...). Since you provide a buffer distance (e.g. 100) not a field, it uses that distance. If you want to use different distances, you would need to write the distance to a field using an Update Cursor or possibly Field Calculator and use that field for the third parameter.

I'm not sure whether your Buffer is executed within the Search Cursor or outside because we can't see your indentation. If inside, Buffer will run for each feature, overwriting the file each time, leaving you with the last run. If outside, Buffer will run once, using the last value as the buffer distance.

MicahBabinski
Occasional Contributor III

I second Darren Wiens' assessment. From the looks of it you want a data access update cursor. Make sure to put the line

cursor.updateRow(row)

at the end of each iteration through the cursor. This will update the table with your specified values.

Micah

EarlSarow
New Contributor III

The "Buffer" command as you're using it doesn't work on a feature-by-feature basis.  It buffers every feature in your feature class by a fixed distance, in your case, buy 100 meters.  If I read the indentation correctly in your code, what is happening is this:

For each feature in the cursor:

   Get the Light type

    Set the buffer distance based on light type

next feature

Run "Buffer"

The last feature you read in the cursor just happens to be type "SV", so you get an output where all the  lights are buffered by a distance of 100.

Darren points you in the right direction:  You will need to add a field to your lights layers, then fill it with the buffer distance for each light type.  Then, when you run the buffer command, give it this field instead of a fixed distance.