Select to view content in your preferred language

Python: How do I use a list loop with the buffer analysis tool to create three buffer zones with user input distance in feet?

3333
5
02-29-2020 09:59 AM
CateEliz
Emerging Contributor

I think I can make my analysis work if I can fill in the blanks.  I know the first two and the distanceList values, but beyond that everything I have tried does not work.

0 Kudos
5 Replies
DavidPike
MVP Frequent Contributor

This will be very simple, however please provide more info such as the intro to your homework and your data.

0 Kudos
CateEliz
Emerging Contributor

The directions are not very detailed.  It says I am to use river.shp as the input and I can define any distances as long as they are in feet. It says to provide user input for the three parameters when implementing the data in PythonWin and each buffer should be named based on its buffer distance (ex. river100).

I attached the python code to the original question and here is an example of the river.shp data.

0 Kudos
DavidPike
MVP Frequent Contributor
#set a list of 100, 200 and 300ft
#variables should be snake case btw...

distanceList = [100, 200, 300]

#buffer river by ieterating values supplied in distanceList

#for every number in distanceList, input this numbe as a variable in the 
#buffer function below

#within the for loop, the 'distance' variable will represent a distance in your list
#If you dont pass this in with 'Feet' it will default to metres

#for the out_feature_class, I'll take this distance value, covert it to a string (str)
#and add it on to the end of the buffer name (concatenating).

for distance in distance_List:

    out_feature_class = "riverbuffer_" + str(distance) + "_ft"
    
    # turn your distance into a string with 'Feet' on the end
    #e.g. "100 Feet"
    distance_ft = str(distance) + " Feet"

    arcpy.Buffer_analysis(in_features, out_feature_class, distance_ft)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

    print "buffer for distance " + distance_ft + " is done"
CateEliz
Emerging Contributor

Thank you so much!  What you did makes sense.  I am trying to fit that to the template I have since I am only supposed to fill in the blanks, but I cannot figure out how to fill the three middle lines without having to change the rest of the code, which I do not think we are supposed to do.  All of the lines are to have something filled in, but those three do not seem necessary for it to work, so I am not sure what would go there. Possibly argument variables?  But I think if I did that then I would need to also import sys and change the parameters listed within the buffer analysis.

0 Kudos
DavidPike
MVP Frequent Contributor

I can only guess it wants you to pass them as parameters before the list rather than as magic numbers e.g

buff_dist1 = 100

buff_dist2 = 200

buff_dist3 = 300

distanceList = [ buff_dist1' .... 

Also be aware your "riverbuffer" + ".shp" needs to change name during the iteration to reflect the input distance. Currently it will just overwrite with the same name.

Use something like

"Riverbuffer_" + str(distance) + "_ft.shp"