Creating a simple Python Geoprocessing tool and adding it to ArcToolbox in AV 9.3

Ask and thou shalt receive… Well it seems as though there is a LOT of interest out there in Python Geoprocessing for ESRI ArcView 9.x.  This increased interest may be due to the release of ArcView 10 with ArcPy.  For those of you that do not know, Python has been shipped with ArcView for many years now.  The ArcToolbox tools are based on Python and when you create a model with the model builder you can export the Python code.

This week’s tutorial will be creating a very simple Python geoprocessing script and then creating a new ArcToolbox tool and adding the Python script to the new toolbox.

I once thought it would be nice to have a simple tool that told me all of the field names in a shapefile.  Sure I could open up the attribute table and see them but if I did that I wouldn’t have written my simple tool.

The Script

I think this script is simple enough to explain it with remark statements.

import sys, os, glob

# We need to create the geoprocessor object
import arcgisscripting
gp = arcgisscripting.create(9.3)

# We need to create an argument and assign it to the value of "file"
file = sys.argv[1]

# The workspace always needs to be identified and here we will use the location of "file"
gp.workspace = os.path.dirname(sys.argv[1])

# A good friend to use is the AddMessage.  This will print to the tool window.  Very useful with troubleshooting as well.
gp.AddMessage("Starting Process.....")

# Using ListFields, we get a list of the fields in the inputted "file"
fieldlist = gp.ListFields(""+file+"")
for field in fieldlist:
    FieldName = field.Name

    # As we go thru the list, AddMessage will print each field name out to the tool window

# And we add one last message so that we know the entire process completed
gp.AddMessage("Process Complete")

Pretty simple stuff there, but I just wanted to use a simple script for this tutorial.

The Toolbox

Now we will create a new Toolbox where the list field’s script will go.

  1. In ArcToolbox, right click on the ArcToolbox folder at the top and select New Toolbox…

  2. Give your toolbox a name

  3. Right click on your new toolbox and select Add => Script

  4. Fill in the fields on the Add Script and click “Next”. I always use the “Store relative path names…..” to make sharing the toolbox easier.

  5. Select the listfields.py file and check the “Run Python script in process” then click “Next”

  6. Now the hard part. In the script that we created, there was one argument that we used “file = sys.argv[1]” The beauty of Python geoprocessing is that there is a user interface to work with and you do not have to write all of the requirements for your inputs or output.
    1. The display name is what you will see in the tool. For this example we will use “Select your file:”
    2. The Data Type produces a drop down to choose what type of file is to be selected. For this example we will use “Shapefile”

    3. The default Parameter Properties will all be correct for this example. These properties are used to control each of the Parameters (in this example we only have the “Select your file:”) Settings vary from Required or optional field, input or output direction etc.. Click “Finish”

  7. That is all there is to it. You now have your new toolbox entitled “My tools” and within that toolbox a new tool entitled “ListFields”

  8. As with any Toolbox tool, click on the new tool to open it:

Go ahead and select a file and click “OK”.

Obviously this is a basic example of what can be done but it is a start.

3 comments on “Creating a simple Python Geoprocessing tool and adding it to ArcToolbox in AV 9.3

  1. […] This post was mentioned on Twitter by Mark Verschuur, Bob Roberts. Bob Roberts said: Creating a simple Python Geoprocessing tool and adding it to ArcToolbox in AV 9.3 http://wp.me/p11Kpe-4C […]

  2. Would you kindly show a screen capture with the results?. Maybe I have a problem with the instalation but for me the tool runs and ends and does not show the messages.

    • After looking at the script you sent me screen shots of, I see that my example does not have the required imports at the top of the Python file:

      import sys, os, glob

      Add that in and it should work.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: