About Me

My photo
Geógrafa pela Unicamp (2014), incluindo um ano de intercâmbio universitário na Universidade de Wisconsin (EUA). Possui experiência na área de geotecnologias, GIS e planejamento urbano, tendo realizado estágios na Agemcamp, American Red Cross e - atualmente - no Grupo de Apoio ao Plano Diretor da Unicamp.

Tuesday, May 7, 2013

Python Scripting

While it's common to think that Python is the "programming language" for ArcGIS, the actual purpose of this language in the geospatial context is for scripting. ArcGIS is built in Java, and therefore, any programming related wouldn't be relate to Python. The main difference between programming and scripting is that the first builds a software, a program; while scripting automates common procedures made within a already-made software. Some examples might be having to project an entire geodatabase with over 500 feature classes. It's not realistic to think about converting them individually - that's where scripting (or even using modeling) come in play. There are other situations where scripting increases the efficiency of a task. For example, if an area is considered risky - like active volcanoes or nuclear plants - it's important for the disaster relief manager to establish evacuation areas within a certain period of time. In other words, it's needed to create one buffer for the first - let's say - 20 minutes, a second for other 20 minutes - which will be related to how fast it's spreading. If you want to do that for the entire county, state or even country, it would be time consuming to repeat the process many times, even though the only thing changing would be the buffer distance. For that, a python loop could do it automatically and then the user would just need to wait for the results. This example is applied for the sand mines in Wisconsin, by using the following code:

##Name: Beatriz Viseu Linhares
##Date: May 7th, 2013
##Description: This script creates a buffer every 1000 meter for five times for the mine feature class.

import arcpy

#These lines define the output location for the buffer results
from arcpy import env
env.workspace = "W:\geog\CHupy\geog491_s13\VISEULB\Ex11-ScriptBuffer"

#These lines define the parameters and tools for the loop
bufdist = 1000
while bufdist <= 5000:
arcpy.Buffer_analysis("Mines","Mine"+str(bufdist)+".shp",bufdist,"FULL","ROUND","ALL","")

    bufdist = bufdist + 1000




The result can be seen below (Figure 1), where each mine contains five different buffers with 1km interval between them. The first line is giving the initial value for the buffer distance "bufdist = 1000"Then, the "while" operator is used to start the loop, containing the condition "while bufdist <= 5000:". In other words, the following action should happen as long as the buffer distance is smaller lower or equal to 5000. The third line "arcpy.Buffer_analysis("Mines","Mine"+str(bufdist)+".shp",bufdist,"FULL","ROUND","ALL","")" should be iterated (which means including the : after the last line, so this one will be related to it) and contains the actual action that have to be taken while the condition is being met - in this case, the buffer tool. For last, it's necessary to define - again - the value for the buffer distance in this specific loop, otherwise, it would just run one buffer of 1000. Then, it's being said the the buffer distance will be the previous buffer distance added to 1000 meters, on the last line "arcpy.Buffer_analysis("Mines","Mine"+str(bufdist)+".shp",bufdist,"FULL","ROUND","ALL","")". When this number reaches 5000 in the geoprocessing, the loop will stop. It's very important that the loop stops - when a limit is not defined, the loop runs forever until the software will eventually crash.

Figure 1 - Buffer Script Applied to Sand Mines


In the previous exercises, a lot of tools were repeated with the same parameters - projections changed, euclidean distance applied, as well as number of reclassification of rasters - a lot with the same classification breaks. As the ModelBuilder is a great tool to keep track of the steps taken and allow some iteration and the use of lists to apply the same tool on it; Python scripting can offer more adjusts and is more flexible to be changed - since you are the one who is writing it, instead of following the tools of a built tool or software.