Developers Guide To Geoprocessing PDF
Developers Guide To Geoprocessing PDF
www.esri.com/devsummitquestions
Command Line
Models
Tool dialog
Scripts
11
Geoprocessing Framework
– 9.0, 9.1
from win32com.client import Dispatch
gp = win32com.client.Dispatch(“esriGeoprocessing.GpDispatch.1”)
– 9.2
import arcgisscripting
gp = arcgisscripting.create()
Scripting
• ArcGIS 9.2 Desktop, Engine & Server install python 2.4.1.
• Pythonwin is no longer automatically installed.
– The version for 2.4.1 has an install bug that prevents a silent install
– Users must manually install for existing scripts to continue to work
– Install EXE is distributed with ArcGIS
• Users may use any IDE when they use the arcgisscripting
module.
• Users can edit existing scripts to create the geoprocessor
the new way and avoid Pythonwin altogether.
– Unless scripts use other win32com functions, such as messagebox
Scripting – Running a Tool
• http://webhelp.esri.com/arcgisdesktop/9.2
– Geoprocessing (Automating your work with Scripts)
Visual Basic 6 and C++
IVariantArrayPtr ipValues(CLSID_VarArray);
ipValues->Add(vInputFC);
ipValues->Add(vOutputFC);
ipValues->Add(vBufferDist);
buffer.setInFeatures("C:/usa/usa.gdb/ushigh");
buffer.setBufferDistanceOrField("500 METERS");
buffer.setOutFeatureClass( "C:/usa/usa.gdb/ushigh_buff");
buffer.setDissolveOption("ALL");
// Execute buffer
gp.Execute( buffer, null);
Working with Tool Names and Avoid Name
Conflicts
• When using multiple toolboxes, it is possible that two or
more toolboxes will contain a tool with the same name.
• All toolboxes have an Alias property. The alias is a short,
alternative name for the toolbox.
• Use the Execute method in which you specify the tool name
along with the toolbox alias.
…
IFeatureClass inputFC = pInputName.Open
…
bufferTool.in_features = inputFC;
bufferTool.out_feature_class = @"C:\Data\Nfld.gdb\roads_500";
bufferTool.buffer_Distance_or_field = “500 METERS”;
GP.Execute(bufferTool, null)
Geoprocesssing Results
• The Execute method returns an IGeoProcessorResult
object which manages the results.
– ESRI.ArcGIS.Geoprocessing namespace
• The result object will have the return value of a tool when
executed.
• Return values are necessary when a tool has no output
dataset, instead, it has an output scalar value, such as an
integer or Boolean. (i.e. GetCount)
Geoprocessing Results
Example 1: Return path to output data
using ESRI.ArcGIS.Geoprocessing;
…
// The return value is an Object of type string
IGeoProcessorResult pResult = GP.Execute(bufferTool, null);
object path = pResult.ReturnValue;
…
IFeatureClass pFc = GP.Open(path);
C#
string messages = GPResult.GetMessages(ref sev);
System.Console.WriteLine(messages);
JAVA
String messages = GPResult.getMessages(2);
System.out.println(messages);
Geoprocessing Messages
// Execute Union
IGeoProcessorResult GPResult = GP.Execute(uniontool, null);
If (GPResult.MessageCount > 0) {
for (int Count = 0; Count <= GPResult.MessageCount - 1; Count++)
{
Console.WriteLine(GPResult.GetMessage(Count));
}
}
Environment Settings
• Environments are global parameters for tools.
• Script and program writers set the environment and tools
use it.
– General settings: current workspace, output spatial reference, extent
– Raster analysis settings: cell size, mask
– Coverage settings: derived and new precision, project compare
– More…
try:
if gp.CheckExtension("spatial") == "Available":
gp.CheckOutExtension("spatial")
else:
raise "LicenseError"
Checking out an Extension
//Initialize the application
IAoInitialize m_AoInitialize = new AoInitializeClass();
licenseStatus =
m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEngine);
licenseStatus =
m_AoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtension
CodeSpatialAnalyst);
MB Window
Why Build Models?
Exposed model
parameters appear as
input boxes on the
model dialog
Working with variables: Intermediate data
Derived data is
flagged as
intermediate
Intermediate Data
• By default all derived data is flagged as intermediate
• Exceptions:
–Variables set as model parameters
–Output of tools that modify the input (Ex: AddField)
•Add
•Remove
•Change order
Model Properties: Environments
Model Environment Settings
• Values used by multiple tools, similar to a parameter (ex:
current workspace, cell size, cluster tolerance)
• Can be set application wide, model level, or process level
– Model specific settings override application settings
– Process specific settings override model settings
Create Environment Variables
• Variables created this way will be attached to just this tool
Create Parameter Variables
• All parameters for a tool, other than input and output, must
be set as variables to be shared between processes.
Use models to organize your work
• You can include one model in another just like any other
tool.
Sharing Models
• To make models easy to share
– Set relative paths for model and script tools
– Set relative paths for map documents
– Document
– Share entire directory/geodatabase
ArcGIS 9.2 - More Advanced Programming
Constructs
• Lists & Series
– Variable properties: List
• Do loop
– Model properties: Iteration
• Fixed loop
• Conditional loop
– Variable properties
• Series
• Feedback
• If/Then
– Process properties: Preconditions
– Merge Branch tool
Demo – Create a Model Tool
Script Tools
Learning Python Scripting with ArcGIS
• Why?
–Provides an efficient method for defining and executing a
series of geoprocessing tools
• Automation
–The script is generic and can be used with other data
• Script can use arguments from the user
–You want to use a script in ModelBuilder
• Incorporate another system with a script wrapper or do branching
–You want to easily share your script
• Not everyone knows how to run a stand-alone script
• Puts a familiar face on your work
Scripting Tasks and Needs
• Batch processing
– Scripts require the ability to find and iterate data (i.e. tables, features
classes, workspaces, fields)
• Data properties
– Scripts need to be able to determine properties of data (i.e. Spatial
reference, geometry type)
• Data Access
– Miscellaneous functions to make scripts easier to write and more
productive
• Error Handling
– Scripts need to determine if something went wrong and report why
ESRI Recommends Python
• Fulfills the needs of our user community
–Free
–Simple
–Modular
–Object oriented
–Easy to maintain
–Scalable
–Cross platform (windows & UNIX/Linux)
–Integrated Development Environment (IDE)
–Established and active user community
–Most geoprocessing examples are available in Python
Creating Tools from Scripts
• http://edndoc.esri.com/arcobjects/9
.2/NET/462f5942-7928-44d6-b85b-
56dc1a0d2ac4.htm
Running Custom Geoprocessing Tools - JAVA
• http://edndoc.esri.com/arcobjects/9
.2/Java/java/engine/ide_integration
/eclipse/EclipseGPTool.html
Demo – Show Code for Running a Model Tool - .NET
Summary: Use Geoprocessing Tools in
Your Applications
• IGPFunction::ParameterInfo property
–Define the parameters
–Returns an array (IArray) of parameter objects
(IGPParameter).
Parameter Type
• Required
– <input_featureclass>
• Optional
– {cluster_tolerance}
• Derived
– Used to indicate when the tool updates the input parameter i.e.
AddField
– Does not show up on the dialog or command line
– Used in ModelBuilder to indicate changed state of the value and
to provide proper chaining.
Parameter Type - Derived
‘VB Code
‘ Define the datatype of the Derived Parameter
Dim pGPParameterEdit as IGPParameterEdit
Set pGPParameterEdit = New GPParameter
Set pGPParameterEdit.DataType = New DEFeatureClassType
'Value object is DEFeatureClass
Set pValue = New DEFeatureClass
Set pParamEdit.Value = pValue
'Set Output Parameter properties
pParamEdit.Direction = esriGPParameterDirectionOutput
pParamEdit.DisplayName = "Output FeatureClass"
pParamEdit.Enabled = True
pParamEdit.Name = "out_featureclass"
pParamEdit.ParameterType = esriGPParameterTypeDerived
pParamEdit.AddDependency "input_featureclass"
Parameter DataType
‘VB Code
‘ Define the datatype of the Input Parameter
Dim pGPParameterEdit as IGPParameterEdit
Set pGPParameterEdit = New GPParameter
Set pGPParameterEdit.DataType = New DEFeatureClassType
Parameter DataType
‘GPValueTableType
Dim pValueTableType As IGPValueTableType
Set pValueTableType = New GPValueTableType
'GPValueTable
Dim pGPValueTable As IGPValueTable
Set pGPValueTable = New GPValueTable
'First DataType
Dim pDataType1 As IGPFeatureLayerType
Set pDataType1 = New GPFeatureLayerType
pValueTableType.AddDataType pDataType1, “input_Features", 100, Nothing
‘Second DataType
Dim pDataType2 As IGPDoubleType
Set pDataType2 = New GPDoubleType
pValueTableType.AddDataType pDataType2, "double", 100, Nothing
'Add DataTypes to the GPValueTable
pGPValueTable.AddDataType pDataType1
pGPValueTable.AddDataType pDataType2
Parameter DataType - Composite
• Enable/Disable parameters.
– AddField
• Selecting FieldType of Text enables the Field Length parameter.
Validate – Update Output Properties
• Deliver the .chm file which is used to describe the tool and
its operation.
• Deliver the .xml file containing the default metadata for this
tool.
– For information about creating the metadata file refer to Knowledge
base article:
• http://support.esri.com/index.cfm?fa=knowledgebase.techarticles.articleSh
ow&d=27000
Function Tool Deployment – Install Program
• http://edndoc.esri.com/arcobjects/9.2/NET/shared/geoprocessing/geopr
ocessing/what_is_geoprocessing_qst_.htm
• http://edndoc.esri.com/arcobjects/9.2/Java/java/gp.html
Session Evaluations Reminder
Session Attendees:
Please turn in your session evaluations.
. . . Thank you