Thursday, July 30, 2015

Creating Custom Tools

We have made it to Module 10! This is a good feeling. This week, we worked on writing code to create a Tool and then share the Tool for anyone to use. There are a number of advantages to using a script tool versus a stand-alone script. For instance:

1. A tool is an integral part of geoprocessing. It is possible to access the tool from Arc Catalog and the Search Window in ArcMap. It is also possible to use the tool in ModelBuilder and in the Python window to call it from another script.
2. A tool is fully integrated with the application it was called from. This means any environment settings are passed from the application, such as ArcMap, to the tool.
3. A tool includes a tool dialog box.
4. The use of tools makes it possible to write messages to the "Results" window.
5. Documentation can be provided for tools, which can be accessed like the the documentation for systems tools.
6. Sharing a tool makes it easier to share the functionality of a script with others.
7. A well-designed tool means a user requires no knowledge of Python to use the tool's functionality.

I made a tool, from a stand-alone script, that would clip multiple shape files. This was really cool, when I finally got it to work properly. Here's what the Script tool window looks like:

I worked on my script using the "trial and error" method along with debugging.  But, initially I was focused on the wrong area. My script did not look like it was running properly, but that was because it had code I had placed in it to run as a "Tool" not a Stand-Alone script. So the errors I received when I ran the stand-alone script, were not errors when I ran the Tool from the ToolBox. Below is a quick overview of how to create a tool. 

1.       To start the process of turning a stand-alone script into a tool, you must create a toolbox. You can do so using ArcCatalog.
o   Right click on the folder where you want to create the toolbox and tool. 
o   Select “New”, then “Toolbox.”

2.       Once you have a new toolbox, you can right click on it and select “Add” and then select “Script.”

3.       After you select “Script”, an “Add Script” template pops up for you to fill in.
o   Add, “Name”, “Label “and a “Description”. Then, add the script for your tool.

4.       At this point, you are on the last page of the Add Script template. Here you will add the parameters:
o   Click on the first line under “Display Name” and type the name of your first input parameter. Under “Data Type”, select the appropriate type from the pull down menu.
o   Do the same for as many parameters as you require; include an output location.
o   Under, “Parameter Properties”, select the appropriate values.
o   For “Direction” it can be “Input” since, when you create the script you can define where to save the “results”
o   Save when done.

5.       You can edit the tool by right clicking and selecting “Edit” or you can use another editor such as PythonWin.

One of the more interesting steps in creating a Tool, and where I learned my Lesson from, was: 

 To make a script into a tool, you must not have hard-coded file locations

o   File locations in the script would normally look like: “S:\GISProgramming\Module10\Data”
o   For the tool to work properly, the file locations should be in the form: arcpy.GetParameter(#), where (#) is the line number of the entry on the tool template.
o   This includes defining the workspace.

For me, the Big Lesson I learned about writing a script for a tool was this:

6.       The script may not run properly as a stand-alone script once you have made the changes (see above) so that the script will run properly as a Tool.  This is because the file paths are defined in the tool template and not explicit as they would be in a stand-alone script.

I spent a great deal of time adjusting my script going back and forth changing lines to get my script to run without an error. What I needed to do was make a single change and then run the "Tool" to check my script. As I said, this was a good Lesson Learned.  Here's what my final "Tool" looked like when it ran properly:

No comments:

Post a Comment