PYTHON BUILDING GEOPROCESSING TOOLS David Wynne Geri Miller
PYTHON: BUILDING GEOPROCESSING TOOLS David Wynne, Geri Miller
Why we build geoprocessing tools • • Your work becomes part of the geoprocessing framework - Easy to access and run from within Arc. GIS - Familiar look and feel - Make a mistake? Re-run from the previous result - Run from Python, Model. Builder, as a service - Add to a tool bar No UI programming
Geoprocessing Tool Commandments Thou 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
Deconstructing a geoprocessing tool • A geoprocessing tool does three things 1. Defines parameters 2. Validates parameters 3. Executes source code
Turning Python code into geoprocessing tools Demo
Script tools vs Python toolboxes • • Using Python, we can build tools in two ways: Script tools Python toolboxes • Source is Python • Parameters through wizard • Validation is Python (stored in toolbox) • Entirely in Python Which do I use? - “A tool is a tool”
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 Filter Environment Type Dependency Symbology Direction Default Multivalue
Key parameter properties Data type 1. - Feature layers, raster layers, table views, … String, Boolean, long, double, … Direction 2. - Input, output Parameter type 3. - Required, optional, derived
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
Parameters - Symbology • Control the appearance of output by setting symbology • Set using a layer file either as : symbology based on logical criteria 1. Parameter property 2. Or in your source code - Important if you want to vary the
Filters • • Filters are used to limit choices to acceptable values For example: 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 - A number between 1 and 10 - A string parameter with keywords Feature Class - A file parameter that will only accept files with a. txt extension Allowable feature class types Feature Layer, Feature Class File extensions File Field Supported field types Field Workspace Supported workspaces Workspace
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
Parameter-izing a tool Demo
Parameters – Validation • As parameters are entered and modified, validation responds and interacts • Tools validate parameters in two ways 1. Basic (‘free’) validation, such as: 2. - Does the input (or output) exist? - Is it the right data type? - Does this value match its filter? - Have all required parameters been supplied? Additional rules and behavior you add
Validation • Provides more control - Parameter interaction - Calculate defaults - Enable or disable parameters • Setting parameter errors and messages • Defining output characteristics - Chain tools in Model. Builder
Validation • Responding to: - value (or value. As. Text) - altered - has. Been. Validated
A quick note about Python toolboxes and script tools
Validation: Model. Builder • Describe outputs for chaining in Model. Builder • Data variables in Model. Builder include descriptions of data • By updating the schema, subsequent tools can see pending changes prior to execution
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 • • clear. Message() • • has. Error() • has. Warning() • set. Error. Message() set. IDMessage() set. Warning. Message()
Validation – Feature Layers • The type of a layer parameter changes depending if the input is a layer or feature class
Working with validation: Script tools and Python toolboxes Demo
Making your Python code work as a tool Receiving parameters 1. - Script tool – arcpy. Get. Parameter / Get. Parameter. As. Text - Python toolbox – parameter. value / parameter. value. As. Text Internal 2. 3. - Messages – arcpy. Add. Message / Add. Warning / Add. Error … - Progressor (step or ‘cylon eye’) - Control actions (or not) after a cancellation Derived values, if any, are pushed back to the tool
Cancelling tools Added at 10. 4, Pro 1. 1 • 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. auto. Cancelling - arcpy. env. is. Cancelled
Distributing your tools http: //esriurl. com/extending. GP • Can ‘share’ a toolbox - • • Simply share the files or package a result 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
Please take our Survey Your feedback allows us to help maintain high standards and to help presenters Find your event in the Esri Events App Find the session you want to review Scroll down to the bottom of the session Answer survey questions and submit
Python at User Conference http: //esriurl. com/Python. UC 16 Tues Wed Python: An Introduction 8: 30 Python: Beyond the Basics 10: 15 Python: Building Geoprocessing tools 1: 30 Thurs Python: Raster Analysis 8: 30 1: 30 Python: Arc. Py Tips and Tricks 1: 30 Harnessing the Power of Python in Arc. GIS using the Conda Distribution 3: 30 Python Map Automation: Introduction to arcpy. mapping / arcpy. mp 1: 30 Advanced Map Automation with Python in Arc. GIS for Desktop 3: 15
- Slides: 27