(1)>>>import arcpy
(2)>>>arcpy.env.workspace = "E:/Geog_337_gis2/Ex10"
(3)>>>i = 1
(4)>>>bufdist = 1000
(5)>>>while i <= 5 :
(6)... arcpy.Buffer_analysis("AllMines_Project","mines_buff" + str(bufdist) + ".shp", str(bufdist), "FULL", "ROUND","ALL","")
(7)... bufdist += 1000
(8)... i += 1
The first two lines of the script merely sets the environment workspace, which is the location generated files will be saved to, unless otherwise specified (will discuss shortly). Lines three and four are defining variables that will be used in the while loop, with "i" being the number of iterations and "bufdist" being the initial buffer distance. Line five starts the while loop, with "i <= 5:" establishing that the specified conditions (lines six through 8) will be repeated until "i" is greater than 5.
Line six runs the actual Buffer tool, with all of its parameters being set within the parentheses. The first two arguments, separated by commas, define the file (shapefile, feature class, etc.) to be Buffered (in this case AllMines_Project), and then the name and location of the resulting file. In this instance multiple files were created, so to ensure all files created are not merely overwriting the first one, the "+ str(bufdist) +" was included to add the bufdist variable to the file name. Since there is no file path included in the parameter, the file will be saved to the environment settings; however, if a different location was desired, then the file path could be included (Ex: "C:/ArcGIS/exercises/mines_buff"). The third parameter sets the buffer distance to the value of the "bufdist" variable.
Lines seven and eight add 1000 and 1 to the "bufdist" and "i" variables, respectively. The "+=" is equivalent to saying x = x + 1000, or in this instance, bufdist = bufdist + 1000. These line are increasing the value of the variable after each iteration, which is what causes the while loop to end, rather than continuing on in an endless loop. Knowing this, the while loop could also have been completed without using the "i" variable at all, by removing lines three and eight, and replacing line five with "while bufdist <= 5000:". This correction would be the preferred method, as it would decrease the amount of scripting, thus making it simpler. Though this may not be as much of a problem with short scripts, it becomes more of a problem as the script length increases.
Figure 1 shows the resulting buffers created using the Python script, and Figure 2 is the map I created from the generated buffers.
Figure 2: Exported map of the buffers generated from the Python script. |
No comments:
Post a Comment