About Me

I graduated from the University of Wisconsin - Eau Claire (UWEC) in May 2012 with a B.A. in Criminal Justice. I returned to UWEC the following semester after finding out about a Geospatial Certificate. I took Geographic Information Systems I in Fall 2012 and loved it. I am taking the remaining 9 credits this semester (Spring 2013). Those classes include: Introduction to Remote Sensing, Geospatial Field Methods, and Geographic Information Systems II.

Thursday, May 9, 2013

Python Scripting

Python is a programming language often used in scripting, and is the scripting language used in ArcGIS software.  The purpose of incorporating Python into ArcGIS is to allow for scripting commands that save time and simplify processes. For this assignment we were required to write a script that would run the buffer tool on our mine locations, creating a buffer for every 1000 meters up to 5000 meters.  Normally, one would be required to either run each process individually, or use model builder.  However, with Python the following code was used to accomplish this task:

(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 1: The Table of Contents (left side) shows the files created and added to the display.
The buffered mines are seen in the display in the center, and at the bottom you can see
the Python script used to run the Buffer tool.
Figure 2: Exported map of the buffers generated from the Python script.