Creating script tools with Python

As I previously said, we can export any ModelBuilder model to Python. Python is an open-source language that is used to automate computing tasks through programs called scripts. It is a high-level language, meaning you don’t have to understand the “nuts and bolts” of how computers work in order to use it. Python syntax (how the code statements are constructed) is relatively simple to read and understand. Finally, Python requires very little overhead to get a program up and running.

In the case of ArcGIS 10, it only works with the version 2.6 of Python (although there are higher versions of Python available). Python comes with a simple default editor called IDLE; however, I highly recommend you to use PythonWin Integrated Development Environment (IDE) to help you write code. PythonWin is free, has basic debugging capabilities, and is included with ArcGIS, but it’s not installed by default. Alternatively, you can download PythonWin (32 bits) in the following link:

Once we have exported our previous Watershed Delimitation model to Python, we can open it using PythonWin. It should look something like this:

# Import arcpy module
import arcpy

# Define paramaters
Output_surface_raster = arcpy.GetParameterAsText(0)
Output_flow_direction_raster = arcpy.GetParameterAsText(1)
Output_accumulation_raster = arcpy.GetParameterAsText(2)
Output_raster_1 = arcpy.GetParameterAsText(3)
Output_raster_2 = arcpy.GetParameterAsText(4)
Output_raster_3 = arcpy.GetParameterAsText(5)
Output_polyline_features = arcpy.GetParameterAsText(6)
StreamT_raster1_FeatureVerti = arcpy.GetParameterAsText(7)
Output_raster_4 = arcpy.GetParameterAsText(8)

# Local variables
dem = "C:\\Users\\Sergio\\Desktop\\BarrancoAras\\dem3"
Output_drop_raster = Output_surface_raster

# Process: Fill
tempEnvironment0 = gp.scratchWorkspace
arcpy.env.scratchWorkspace = "C:\\Users\\Sergio\\Desktop\\BarrancoAras\\modelbuilder.gdb"
tempEnvironment1 = gp.workspace
arcpy.env.workspace = "C:\\Users\\Sergio\\Desktop\\BarrancoAras\\modelbuilder.gdb", Output_surface_raster, "")
arcpy.env.scratchWorkspace = tempEnvironment0
arcpy.env.workspace = tempEnvironment1

# Process: Flow Direction, Output_flow_direction_raster, "NORMAL", Output_drop_raster)

# Process: Flow Accumulation, Output_accumulation_raster, "", "FLOAT")

# Process: Raster Calculator"Con(\"%Output accumulation raster%\" > 20000,1)", Output_raster_1)

# Process: Stream Link, Output_flow_direction_raster, Output_raster_3)

# Process: Stream to Feature, Output_flow_direction_raster, Output_polyline_features, "SIMPLIFY")

# Process: Feature Vertices To Points
arcpy.FeatureVerticesToPoints_management(Output_polyline_features, StreamT_raster1_FeatureVerti, "END")

# Process: Watershed, StreamT_raster1_FeatureVerti, Output_raster_4, "ORIG_FID")

# Process: Stream Order, Output_flow_direction_raster, Output_raster_2, "STRAHLER")

Take a look at line 2 of the code. Here we use the “import” command to import the ArcPy module into Python. ArcPy is a site package that is a successor to the arcgisscripting module of ArcGIS 9.2 and 9.3. This package provides a wide range of special scripting abilities included with ArcGIS 10.

From line 5 to line 13 we have to define the script parameters with the “arcpy.GetParameterAsText()” method. This allows the user to enter the path of the output datasets.

In line 16, we define the input dataset. In this case it has been hard-coded, meaning that it cannot be changed by the user.

From line 20 to 26 the script “environments” are provided, using “arcpy.env.scratchWorkspace” and “arcpy.env.workspace” tools.

Finally, from line 29 to 50 the geoprocessing tools are executed.  For more information on an specific tool syntax, you can open it through “System toolboxes” in ArcMap 10 Catalog window, and click on “Tool Help” in the lower-right side of the window.

Once you have checked the code for errors, save it on your local disk with a .py extension.

Now we are ready to implement the script code as a tool into ArcMap.

Open ArcMap 10. In the Catalog window, right-click on “My Toolboxes” > New > Toolbox.

Right-click on the newly created Toolbox > Add > Script… The Add Script wizard will appear:

Give the script a name (no spaces) a label and a description (optional) and click Next.

In the next window, browse to the .py file you previously saved and click Open.

The next window is the most important. Here we have to define what the nine script parameters are in the same order provided in the ” arcpy.GetParameterAsText()” code block. You see two columns: “Display Name” (the name that will be shown on the tool window) and “Data Type” (the type of data we have to provide as outputs). We leave “Parameter Properties” as default.

The window should look like this:

Click Finish.

Open the Script tool. It should look like this:



About Sergio Perez
An environmental scientist passionate for the world of Geographical Information Systems and its application to the field of hydrological management.

One Response to Creating script tools with Python

  1. Koloeii says:

    I saw that you are using the raster calculator in your script with “” function. I’m using it too but I’m confused with what I read on various website about its functionality on a python script.
    On ESRI website I just saw (I didn’t notice it before) a remark about Raster Calculator being not suited to be used in python script. That information being spreaded on other websites.
    However my script works fine and I suppose yours works too.
    What is your opinion on that situation ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: