If you are stuck at "if" – Part 3 – Does Extension Exists model example

This blog was supposed to be the last of a 3-part blog. But we discovered that we had more to say, so this third part isn’t the last; we’ve got a couple more to go.

Recap

If you haven’t already, take a quick look at:

Part 1 on examples of using the Calculate Value tool to create branches using if-else logic.

Part 2 builds on Part1 highlighting how to create a script tool from the Python script, creating a value list, exposing tool parameters and creating model parameters.

This part 3 of the blog highlights:

  • Branching using a script tool with if-then-else logic
  • Working with Value lists values
  • Using custom messaging tool

 
Data

Download the script tool and model from here.

 
We start with

An example model that uses the Hillshade tool from 3D Analyst extension, similar to the example in blog Part 1. This example shows only a part of a larger workflow and can be extended as required.

The model is created for sharing, but it requires the 3D Analyst extension which might not be available with all those the model is shared with. To account for such a case a simple “if” script tool is added to check the availability of the extension. If the extension is not available, the model uses a custom messaging tool to inform the user about the unavailability of the license and that the model cannot be run.

Illustration 1- Does Extension Exists model

The Does Extension Exist? script tool uses the CheckExtension Acrpy function to check the availability of the extension and the CheckOutExtension function to check out the extension for use.

Illustration 2- Does Extension Exists Script tool portion highlighting the ArcPy functions

 
Creating the model

The model is created by:

To make this script tool a generic tool that can be used to check the availability of any extension, a value list filter is set on the Extension to Check parameter from the script tool properties as shown below.

Illustration 3- Does Extension Exists value list parameter

The list that appears in the drop down list on the tool dialog are the actual names of the extensions available with ArcGIS such as, 3D Analyst, Business Analyst, etc. However, these extensions have a code name that is used to look up the extension license by the script tool. To account for this difference in the name that appears on the tool dialog and the name code that is used in the ArcGIS system, the script tool uses a list of if-then-else statements as shown below.

Illustration 4- Handling extension names and their codes in the script

  • Adding and connecting the script tool to other tools in ModelBuilder
  • Setting precondition

The Exists Boolean parameter is set as a precondition to the Hillshade tool. This ensures that the Hillshade tool will run only if the Exists Boolean parameter is True. The Does not Exist Boolean parameter is connected as a precondition to another script tool that adds a custom warning if the extension is not available. This custom script tool can be created easily with just three lines of code as shown below:

Illustration 5- Simple Add Warning script tool

To create a more generic tool that can add an error, a message or a warning see the Add Error, Message or a Warning script tool with the download. This tool has three optional input parameters, one each for a custom error, a message or a warning. The script uses if-then-else statements to check which input parameter has a value and based on that creates an appropriate output.

Illustration 6- Generic Add Error, Message or Warning script tool

Any inline variable can be used as an input to this script tool parameters. For example to add an error using the name of the extension that the user of this tool has selected from the drop down list the variable Extension to Check is used as inline variable as shown below:

Illustration 7- Using inline variable substitution in the error message

This entry was posted in Analysis & Geoprocessing and tagged , , , , , . Bookmark the permalink.

Leave a Reply

5 Comments

  1. sephy59 says:

    What are the benefits in mixing a script tool and a model? Why not use either one for the entire process?

  2. shitijmehta says:

    Hi Sephy59,

    It’s a very good question. Thanks!

    There is nothing like one is better than the other. It’s more of a preference thing. If you are more comfortable with Python, that is the way to go. You can do almost all the things using a script.

    It is more or an organizational matter i.e. “How can I organize my workflow more efficiently?”. If you are doing higher level logical processing or a number of logical processing things, then script is the way to go. You would not want to add 3 or 4 script tools in a model with 5 tools.

    If you are not very comfortable with Python/scripting ModelBuilder and its visual elements are good for you. For the things you cannot do with tools in ModelBuilder you can use small snippets of Python, thus limiting your frustration with understanding too much code.

    With ArcGIS 10 iterators have made the batch processing/looping very easy to use and understand, visualize and control.

  3. curtvprice says:

    I put together a nifty script tool that outdoes your message tool a bit by supporting id messages. Sure would be nice if there was a tool in Model Builder out of the box.


    Executing: Message WARNING "id 591 First,Last"
    Start Time: Thu Oct 18 11:58:10 2012
    Running script Message...
    WARNING 000591: First parameter not Last.
    Completed script Message...
    Succeeded at Thu Oct 18 11:58:10 2012 (Elapsed Time: 0.00 seconds)


    #
    # GP Script tool - Message
    # for use in ModelBuilder

    import arcgisscripting
    gp = arcgisscripting.create()

    # Message type: MESSAGE, WARNING, ERROR
    msgType = gp.GetParameterAsText(0).upper()
    # Message text
    msgText = gp.GetParameterAsText(1)

    try:
    # id message, format: "id 345 arg1,arg2"
    if msgText[:2].lower() != "id" or msgType == "MESSAGE":
    raise
    else:
    msgList = msgText[3:].split()
    msgID = int(msgList[0])
    # pick up arguments, comma-separated
    msgArgs = " ".join(msgList[1:]).split(",")
    msgArgs = [msgType,msgID] + msgArgs
    gp.AddIDMessage(*msgArgs)
    except:
    # text messages
    if msgType == "MESSAGE":
    gp.AddMessage(msgText)
    elif msgType == "WARNING":
    gp.AddWarning(msgText)
    elif msgType == "ERROR":
    gp.AddError(msgText)
    else:
    gp.AddMessage(msgText)

    gp.SetParameterAsText(2,True)