Friday, May 10, 2013

Using Python in ArcGIS


During this, and the previous exercise we explored the use of Python in Arcgis. Python runs on script similar to other programs including Maple and R. We used python to run a loop program that would create concentric 1000 meter buffers out to 5 thousand meters around each mine in Trempealeau County (figure 1), WI. To do this I used the following script.

Fig.1. Mines located in Trempealeau County, WI.
Data used previously, projected in UTM zone 15N


 >>> import arcpy
>>> arcpy.env.workspace = "W:\geog\CHupy\geog491_s13\CAMRENSL\python2"
>>> i = 1
>>> bufdist = 1000
>>> while i <=  5:
...     arcpy.Buffer_analysis("mines2", "mines_buff" + str(bufdist) + ".shp", str(bufdist), "FULL", "ROUND","ALL")
...     bufdist += 1000
...     i+=1
...    
>>> 

This first section of code sets the workspace for python.
>>> arcpy.env.workspace = "W:\geog\CHupy\geog491_s13\CAMRENSL\python2"

The second and third lines of code tell it that each I is 1 iteration and each buffer distance is 1000 units, our projection was in meters so the units are in meters.
>>> i = 1
>>> bufdist = 1000

The next lines of code tell the program to run 5 iterations, run a buffer, what data to use and where to store the output. It also sets the parameters for the tool and finishes with the loop function which reruns the data the specified number (5) times.
>>> while i <=  5:
...     arcpy.Buffer_analysis("mines2", "mines_buff" + str(bufdist) + ".shp", str(bufdist), "FULL", "ROUND","ALL")
...     bufdist += 1000
...     i+=1
...    
>>> 

This type of operation is incredibly time saving versus running Euclidean distance on each individual raster used previously and then reclassifying the rasters. The simple loop script we used to create the buffered mines worked just as well as the multiple step process (figure 2).Using python you could feasibly write script to run all of the data sets with loop buffers (Euclidean and reclass in rasters) within a single batch of script instead of each a series of separate operations. This speeds up the process reducing both computer storage and operating costs. 

Fig.2. buffered mines in Trempealeau County, WI
using Python script to streamline the process.