Geoprocessing IV: - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Geoprocessing IV:

Description:

Shared scripts should have a user-friendly interface and be robust ... the interactive window (or Python Shell), user will be prompted with specified string ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 32
Provided by: jasonp5
Category:

less

Transcript and Presenter's Notes

Title: Geoprocessing IV:


1
Geoprocessing IV
  • Creating ArcTools

2
Sharable scripts
  • Scripts are often created to be shared with
    others.
  • Shared scripts should have a user-friendly
    interface and be robust
  • 3rd party users will not necessarily know how to
    work with Python or troubleshoot a problem.
  • Importing a script into ArcToolbox provides a
    user-friendly interface and allows you to provide
    thorough documentation and instruction.
  • Creating a robust script needs to be done before
    bringing the script into ArcToolbox.

3
Handling errors
  • Nobody likes it when a program crashes without an
    explanation.
  • Python has tools that allow you to catch
    errors
  • deal with them and continue running the script
    if possible
  • or exit the script and provide an explanation for
    the failure if possible.

4
Try / except
  • The try / except statement is the main tool for
    handling errors in Python.
  • The script attempts to run code in the try part.
    If an error occurs, the script immediately skips
    to the except part and runs the associated code.
  • Can trap any type of error or trap specific types
    of errors.

5
Example try / except
Try adding a field
If unsuccessful, print message
  • The above example is the simplest way to address
    the issue of adding a field when there is a
    possibility that it may already exist but what
    are the potential problems with this solution?
  • An alternative is to use ListFields and then
    check to see if the field is present in the table
    and only attempt to add the field if it is not
    present.

6
Error messages and ArcTool scripts
  • Scripts run from ArcToolbox can be difficult to
    troubleshoot
  • Make sure the script works before importing it.
  • ArcGIS will not know the reason for a script
    failure unless the script tells it.
  • Send the error message to the ArcTool progress
    window

7
Getting messages from the geoprocessor
  • ArcTools always generate a message when they are
    finished running
  • Indicates the success or failure of the operation
  • Indicates the reason for a failure if known.
  • The script can access ArcTool messages with the
    geoprocessors GetMessages method.
  • Retrieves the message generated by the last run
    ArcTool

8
Example catching error messages from the
geoprocessor
Try reclassification
If unsuccessful, print ArcTool message
9
Sending messages to the ArcMap progress window
  • The print statement does not print to the script
    progress window in ArcMap.
  • To send messages to the progress window, use the
    geoprocessors AddMessage, AddError, or
    AddWarning methods.

10
Progress window messages
11
User-specified parameters
  • A 3rd party user will want to specify certain
    parameters when running a script tool
    user-specified parameters.
  • These parameters are specified when the script is
    run.
  • There are two ways to have the script request the
    parameters
  • the python way
  • the geoprocessor way

12
Requesting parameters before script execution -
the python way
  • The argv tool in the sys module allows a script
    to request a parameter at run-time
  • The sys module must be imported before hand.
  • The parameter number is specified in brackets.
  • This method will work when running scripts
    through the Python interface or through the
    ArcGIS interface.

13
Requesting parameters during script execution -
the python way
  • Parameters can be requested during script
    execution using the raw_input statement
  • In the interactive window (or Python Shell), user
    will be prompted with specified string
  • User can the type in an appropriate value
  • Note this will not work if script is run through
    ArcGIS

14
Script path name
  • Another use for sys.argv is to get the file path
    name of the script
  • Script can set workspaces using script folder.

15
Requesting parameters the geoprocessor way
  • The geoprocessor has two tools for requesting
    run-time parameters
  • Parameter number is specified in parenthesis.
  • These methods will only work when running a
    script through ArcGIS.

16
Importing a script into a toolbox
17
Specify tool properties
18
Specify file path name of script
19
Parameter name
Data type
Parameter properties
20
Parameter name
  • Specify parameter names in the same order that
    they are numbered in the script.
  • Name will be displayed in the tool dialog window.

21
Parameter data type
22
Parameter properties type
  • The type property specifies if parameter is
    required, optional, or derived.
  • The derived option is used when a parameter value
    is derived within the script. Usually the
    parameter is an output that needs to be exposed
    for use in a model.

23
Parameter properties direction
  • Specify if the parameter is an input or an output.

24
Parameter properties multi-value
  • Specify if parameter can have multiple values.
    The values will be placed in a string with values
    separated by

25
Parameter properties default
  • Specify a default value for the parameter.

26
Parameter properties environment
  • Set parameter equal to value of an environment
    setting (e.g. set parameter value equal to the
    cell size specified in the environment)

27
Parameter properties domain
28
Parameter properties dependency
  • Specify if the parameter is dependant on the
    value specified for another parameter. For
    example, a field parameter should have dependency
    on an input dataset parameter.

29
Help on setting parameters
  • Use question cursor to get help on listing
    parameters or setting parameter properties.

30
The script tool
  • After a script is imported into a toolbox
  • it can be run from the dialog window like a
    model.
  • It can be documented like a model
  • Dialog window documentation
  • Help page documentation
  • Script tools can be used in models.

31
In-class practical
  • Download the Demo script sharable scripts
    through the syllabus
  • Convert this script to a sharable script
  • Identify model parameters
  • Set up variables for model parameters if needed
  • Use sys.argv, GetParameter, GetParameterAsText,
    or raw_input to get values for parameters at
    runtime.
  • Import script into ArcToolbox
Write a Comment
User Comments (0)
About PowerShow.com