Friday, May 10, 2013

Final GIS II Lab ~ Python

Introduction:
     The goal of this lab is merely to acclimatize myself to python script and coding.  Over the past week or so, the class has taken on the challenge of learning some python code and we have practiced with it both in ArcMap and using a python shell (IDLE).  This week, we graduate to performing iterations and while loops, culminating in the exercise posted here.  The end product a quickly and cleanly generated script which includes an iterative feedback loop to draw five buffers.  These buffers will be formed around a provided point feature class, representing Trempeleau County sand mines (sound familiar?).  Ultimately, the script will produce a series of buffers around each mine, representing a distance of one to five kilometers from the nearest mine.

Methodology:
Fig 1:  Python script utilizing the Arcpy module to iteratively buffer around 

     Using the Python window in Arc, the above script was entered (fig 1) and run in the ArcMap environment.  The first task which it accomplishes is to import the arcpy module and the environments from the .mxd in which the script is being run (line 3, env.workspace= ...). Then, the script sets a "counter" variable on line 4 (i=1), which will hold the number of times the loop as been completed; a similar representative variable for buffer distance is formed.
     On line 6, the while loop begins.  What this bit of python syntax does is parse the nested code (indented, lines 7-9) if the condition provided (line 6, i<=5) is true.  After  performing the nested code, the the computer then returns to line 6 and reevaluates whether the condition has changed, and if it has changed while remaining true it will reiterate the nested code again until either the condition no longer changes or is no longer true.  Specifically, what this while loop does is create a buffer (line 7), then increase the buffer distance by 1000 meters and increase the counter variable by 1; thus each iteration will create progressively larger buffers while increasing the counter value.  Once that value exceeds 5, the loop will cease to be parsed and the script is done running.

Results:

Fig 2: buffers of up to 5km around sand mine addresses in Trempealeau County, WI.


Discussion:

     I found this exercise to be an easy and effective method of completing a project.  I almost wish that I had started GIS by running scripts in Arc, I wonder how much time I could have saved myself by now!  At the same time, while working with other students I was quickly reminded of the finnicky nature of coding which can be a quick draw on one's patience.  This is not a skill that one can simply pick up by looking at it; but the potential value that scripts bring as processes for automation seems nearly endless.