Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded...
Transcript of Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded...
![Page 1: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/1.jpg)
Python: Developing Geoprocessing Tools
Gerhard Trichtl
![Page 2: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/2.jpg)
Why we build geoprocessing tools
• Your work becomes part of the geoprocessing framework
- Easy to access and run from within ArcGIS
- Familiar look and feel
- Make a mistake? Re-run from the previous result
- Run from Python, ModelBuilder, as a service
- Add to a tool bar
• No UI programming
![Page 3: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/3.jpg)
Geoprocessing Tool Commandments
You shall …
… have unique parameter names within the tool
… keep the cost of validation to a minimum
… always have an output, even if it must be derived
… populate all output data elements within validation
… not test the validity of any derived value within validation
… have a coded value domain for every Boolean
… test the function from a script, a model, a dialog, and the command line
![Page 4: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/4.jpg)
Deconstructing a geoprocessing tool
• A geoprocessing tool does three things
1. Defines parameters
2. Validates parameters
3. Executes source code
![Page 5: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/5.jpg)
Demo
Turning Python code into
geoprocessing tools
![Page 6: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/6.jpg)
Script tools vs Python toolboxes
• Using Python, we can build tools in two ways:
• Which do I use?
- “A tool is a tool”
Script tools Python toolboxes
• Source is Python
• Parameters through wizard
• Validation is Python
(stored in toolbox)
• Entirely in Python
![Page 7: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/7.jpg)
Parameters
• Parameters are how you interact
with a tool
• Simple rules to guide behaviors
- Does an input exist?
- What are valid fields for this data?
- Is this value an expected keyword?
Data type
Type
Direction Default
Dependency
Filter Environment
Symbology
Multivalue
![Page 8: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/8.jpg)
Key parameter properties
1. Data type
- Feature layers, raster layers, table views, …
- String, Boolean, long, double, …
2. Direction
- Input, Output
3. Parameter type
- Required, optional, derived
![Page 9: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/9.jpg)
Parameter Dependencies (‘obtained from’)
• Some data types are dependent on other parameters
- Field, SQL Expression, Field Info provide little value on their own
• Set a dependency to a different data type
![Page 10: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/10.jpg)
Parameters - Symbology
• Control the appearance of output by setting symbology
• Set using a layer file either as :
1. Parameter property
2. Or in your source code
- Important if you want to vary the
symbology based on logical criteria
![Page 11: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/11.jpg)
Filters
• Filters are used to limit choices to acceptable values
• For example:
- A number between 1 and 10
- A string parameter with keywords
- A file parameter that will only accept files with a .txt extension
Filter Values Relevant data
types
Value List String (keywords)
or numeric values
String, Long,
Double, Boolean
Range Between a
minimum and
maximum value
Long, Double
Feature Class Allowable feature
class types
Feature Layer,
Feature Class
File File extensions File
Field Supported field
types
Field
Workspace Supported
workspaces
Workspace
![Page 12: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/12.jpg)
Parameters - Defaults
• Set a parameter default by value or by environment
- Some data types match closely with geoprocessing environments
- Can configure a parameter to use an environment by default
![Page 13: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/13.jpg)
Demo
Parameter-izing a tool
![Page 14: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/14.jpg)
Parameters – Validation
• As parameters are entered and modified, validation responds and interacts
• Tools validate parameters in two ways
1. Basic (‘free’) validation, such as:- Does the input (or output) exist?
- Is it the right data type?
- Does this value match its filter?
- Have all required parameters been supplied?
2. Additional rules and behavior you add
![Page 15: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/15.jpg)
Validation
• Provides more control
- Parameter interaction
- Calculate defaults
- Enable or disable parameters
• Setting parameter errors and messages
• Defining output characteristics
- Chain tools in ModelBuilder
![Page 16: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/16.jpg)
Validation
• Responding to:
- value (or valueAsText)
- altered
- hasBeenValidated
![Page 17: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/17.jpg)
A quick note about Python toolboxes and script toolsPython-Toolbox
Script-Tools
![Page 18: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/18.jpg)
Validation: Messages
• Control over parameter messages
- Evaluate system messages, and relax or change
- Add more stringent errors based on your own criteria
Parameter message properties/methods
• message • setErrorMessage()
• clearMessage() • setIDMessage()
• hasError() • setWarningMessage()
• hasWarning()
![Page 19: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/19.jpg)
Validation – Feature Layers
• The type of a layer parameter changes depending if the input is a layer or feature class
![Page 20: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/20.jpg)
Demo
Validation
![Page 21: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/21.jpg)
Making your Python code work as a tool
1. Receiving parameters
- Script tool – arcpy.GetParameter / GetParameterAsText
- Python toolbox – parameter.value / parameter.valueAsText
2. Internal
- Messages – arcpy.AddMessage / AddWarning / AddError …
- Progressor (step or ‘cylon eye’)
- Control actions (or not) after a cancellation
3. Derived values, if any, are pushed back to the tool
![Page 22: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/22.jpg)
• When a tool is cancelled…
- ... by default, code will be cancelled after the current line
• Using new environments, can respond to the cancellation
- arcpy.env.autoCancelling
- arcpy.env.isCancelled
Cancelling toolsAdded at 10.4, Pro 1.1
![Page 23: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/23.jpg)
Distributing your toolshttp://esriurl.com/extendingGP
• Can ‘share’ a toolbox
- Simply share the files or package a result
- Import the Script into the Toolbox (optionally protect the script)
• However, to have your tools blend seamlessly, distribute as a Python package
- Are easily distributable
- Toolboxes appear as System Toolboxes
- Toolboxes become available through arcpy
- Supports dialog side-panel help and localized messages
Requires specific structure
![Page 24: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/24.jpg)
Questions ?
![Page 25: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/25.jpg)
Please Take Our Survey!
Download the Esri Events app
and go to DevSummit
Select the session you attended
Scroll down to the
“Feedback” section
Complete Answers,
add a Comment,
and Select “Submit”
![Page 26: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/26.jpg)
Thank You to Our Generous Sponsor
![Page 27: Python: Developing Geoprocessing Tools€¦ · Why we build geoprocessing tools ... have a coded value domain for every Boolean … test the function from a script, a model, a dialog,](https://reader030.fdocuments.net/reader030/viewer/2022013021/5f1ed24cd4566d35347a40d4/html5/thumbnails/27.jpg)