The goal of this lab is to familiarize the student with basic Python which can be applied to Model Builder to further its capabilities.
To create an ArcMap project without using a geodatabase, simply cancel the initial pop-up menu and then use the file tool to save an .mxd file into the appropriate folder.
The Spatial Analyst toolset was activated under Customize > Extentions
The data was downloaded and extracted into the appropriate folder. Then, a satellite band image, L5018024_02420100707_B10.TIF , was added to the data frame.
Figure 1. Satellite image with band 10 situated on Canadian Map.
The model properties must be sent to the extend of the zoomed in portion of the image on the display.
Model > Model Properties > Environments > Processing Extent > Values > Same as Display > Snap Raster > same as input tif (to line up the pixels exactly).
Importing/dragging the Times tool into the model builder.
ArcToolbox > Spatial Analyst Tools > Math > Times
Double clicking on the Times tool, you can multiply the image by a constant of 1 and then trim it to the extend so that you get a cropped image of the data representing the frame you desire.
Figure 2. Model builder for multiplying the image and setting its extent.
Figure 3. The resultant map from the Times tool with an arbitrary extent.
To export the model to a Python script: Select Model > Export > To Python Script
This must be saved one step above the location of the original image TIF file in the folder path.
Open the script in a simple text editor. Right click > Show more options > Edit with IDLE
Open the Python window built into ArcGIS (Icon on main panel of ArcMap)
Figure 4. The exported python code for the Times tool and manipulation performed in the previous image.
Numerical variables are simply created by typing the variable = the number. (e.g. x=2). To display the value, the command "print x" can be used. To combine a string with a number as a string, the number must be converted to a string before it can be concatenated to another string. This is done with the "str (variable)" function. This is all shown in the example code below.
Figure 5. Practice with python commands.
Figure 6. Tradition of writing "Hello World".
Data in lists are delimited by brackets and comma separated.
Figure 7. Formatting lists in Python.
The "for" command is used for executing loops in Python. A colon must be typed at the end of the "for" line and all of the statements inside of the loop must be preceded by an indentation. In the Python Editor in ArcMap, enter must be hit twice to execute the loop.
Figure 8. For loops with Python.
"IF" statements can be used in Python to see if something is true or false. (NOTE ! means "not" in Python). Else clauses are also often used in code in Python to address the false statements. It must be inline with the if statement and proceeded by a colon.
Figure 9. Formatting "IF" statements in Python.
The function "open("test.txt","w")" opens the text file named "test". The "w" is the mode and causes the function to overwrite a file if it already exists. The "r" mode is used to open the file in read mode. The file name in this case is called a "relative" path. This means that the file will appear in the folder that the script is executed in. An "absolute" path (C:/Users...) can also be specified. The open command sends a "handle" to the file in this case.
NOTE: the help text for all of the ArcToolbox tools in ArcGIS contains a code snippet showing how to use the tool in a programming context.
Figure 10. Exported script in a python window outside of ArcMAP.
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
ensures that the script does not stop if you try to overwrite a raster image that was already created in a previous run of the script.
The script was loaded into the ArcGIS Python shell by right clicking in the shell and clicking load. Then the code file was opened and enter was pressed to run it.
It was successful in creating the same cropped image of the single band.
The variables were renamed in the script to be more easy to follow/relevant. The script was then run and it successfully performed the same task that the model did.
Figure 10. Python Script associated with Model Builder.
A Python script that can reclassify each individual band of the image into five levels with "Natural Breaks" using the "Slice" command in Spatial Analyst for the entire image extent is desired. Further, the mean and standard deviation of the reclassified raster bands should be recorded in a text file.
The model builder template uses the slice function to divide the band through natural breaks into 5 output zones. It looks as follows and creates the ranked output as seen in the image below.
Figure 12. Sliced band into natural breaks which are reclassified using Model Builder.
The code was then exported to python and tested within the python script in ArcMap.
Model > Export > To Python Script.
Below is a picture of the functional script followed by the simplified/renamed script.
Figure 12. Original and renamed python script.
Figure 13. Final working code to slice and extract the mean and standard deviation for all of the bands.