Friday, May 10, 2013

GIS II: Geoprocessing with Python Script

Introduction

This exercise dealt with using Python scripting to run an iterative script to run buffers around frac sand mine locations in Wisconsin. Python gives the analyst a dynamic and versatile way to run geoprocesssing tools. This language is the preferred scripting language used in ArcGIS. There are a variety of options in ArcGIS for running geoprocessing tools. First of all, there is the standard Toolbox in which there is a template where you enter the variables, output location, and any additional parameters. The downside of using the standalone toolbox is that you have to run the tool for each dataset separately which can take a lot of time and create a large amount of files. Another option is to use Model Builder in which multiple tools can be implemented at the same time in order to streamline data processing. Using Python scripting, the analyst can directly write out the process and define the variables directly withing the scripting window. Python also gives the analyst the advantage of creating tools or making adjustments to other tools. Either way, it can serve as a very powerful tool for geoprocessing tasks.

The objective for this exercise was to write an iterative feedback loop using Python to buffer mine locations 5 times; each time adding 1000 meters onto the previous buffer. This required using a "loop" structure which is basically a way of telling the computer the number of iterations to run as well as the increment value for each iteration. The looping function runs over a range of numbers which has three arguments: start, stop, and step. The start argument defines the beginning integer and up to, but not including (stop) the highest range value using an increment of size (step).

Methodology

So for this exercise, we want 5 buffers of 1000 meters which translates into: start = 1, stop = 5, and step = 1000m. Below is how this workflow is translated into Python scripting where each variable needs to be defined before writing the model.
Fig. 1 - Python script for running a buffer iteration on mine locations.
Once the workspace was defined (where the "mines.shp" is located), the variables were defined and then inserted into the appropriate spots within the Buffer_analysis template. Basically, this is what the Buffer tool looks like in Python script. Here, i = 1 is the starting integer and "while i <= 5" is telling the computer to stop at 5. Within the Buffer tool, "mines_buff" + str(bufdist) + ".shp" means that the script will add 1000 onto each name of the respective iteration shapefile. At the end of the Buffer_analysis parameters, "bufdist += 1000 means that for each iteration, add 1000m for the next buffer and "i += 1" is referred to as the counter which increases the value of i by 1.

Results/Discussion


Fig. 2 - Results from buffer iteration script.

Fig. 3 - Zoomed in view of some sand mines in Trempeleau County, WI.

Using Python scripting to run the buffer processing saved a lot of time as using the Buffer tool via toolbox would have meant running each iteration separately. This exercise shows how powerful Python scripting can be and that it really has no limitations. While using Model Builder was useful in creating risk and suitability models, Python scripting would have streamlined alot of the processes as it could have been used to loop all of the buffers instead of using Euclidean Distance on every feature. After running Euclidean Distance, you still have to parse the distances into classes whereas with running a loop, you can define the distance break right away. The only downside is that Python takes awhile to learn whereas Model Builder is very straightforward. Investing the time into learning Python scripting would be very beneficial though as it is not as limited as Model Builder is.