Building a custom geoprocessing tool in Eclipse IDE


Summary This topic walks you through the workflow for building a custom geoprocessing function tool using the ESRI Geoprocessing Extension wizard in Eclipse IDE. The Geoprocessing Extension wizard auto-generates Java code to create a custom geoprocessing function tool. The workflow includes developing the custom function tool, deploying the tool in ArcGIS, and consuming it in ArcToolbox. This topic covers the key concepts and ArcObjects necessary to build a custom geoprocessing tool.

In this topic, you'll build the Delete Features tool, which deletes features based on a user-defined SQL Expression.

Click here to get the sample associated with this walkthrough.

In this topic


About building a custom geoprocessing tool in Eclipse IDE

Building a custom function tool requires implementing the following tasks: 
For more information about creating custom function tool classes and custom function factory classes, see How to build custom geoprocessing tools.
In this topic, you'll build a custom geoprocessing function tool using the ESRI Geoprocessing Extension wizard in Eclipse IDE. The Geoprocessing Extension Wizard accepts user inputs and auto-generates code for the custom function factory class and the custom function tool class.

Create a Java project

To create a Java project, perform the following steps:
  1. Start Eclipse IDE.
  2. Click File, select New, and select Project.
    The New Project, Select a wizard dialog box opens.
  3. Select Java Project and click Next as shown in the following screen shot:



    The New Java Project, Create a Java project dialog box opens.
  4. Type the project name and click Next as shown in the following screen shot:



    The Java Settings dialog box opens.
  5. Click the Libraries tab and click Add Library as shown in the following screen shot:



    The Add Library dialog box opens. 
  6. Select ArcGIS Engine Library and click Next as shown in the following screen shot:

  7. In the subsequent Add Library dialog box, click Finish.
    The Java Settings dialog box opens and ArcGIS Engine Library is added to the build path of the project as shown in the following screen shot: 

     
  8. Click Finish.
    You have created the geoprocessing.custom_gptool.deletefeatures Java project. It is listed in the Package Explorer window as shown in the following screen shot:

Auto-generate a geoprocessing tool implementation

To create both a geoprocessing tool Java class that defines the input and output parameters as well as the operation of the tool, and a custom function factory Java class that hosts the geoprocessing function tool in ArcToolbox using the Geoprocessing Extension wizard in Eclipse IDE, perform the following steps:
  1. In the Java Eclipse Platform dialog box, click File, click New, and click Other as shown in the following screen shot:



    The New, Select a wizard dialog box opens.
  2. Expand the ESRI Templates node, expand ArcGIS Extension, select Geoprocessing, and click Next as shown in the following screen shot:



    The New Geoprocessing Extension, Geoprocessing Tool dialog box opens. 
  3. Type the name, or browse to the source folder, of the Java project you created.
  4. Type in or browse to the Java Package name.
  5. Fill in the tool Name, Display Name, and Description parameters as shown in the following screen shot:



    A Java class for the geoprocessing function tool is created in the source folder and package.
  6. To change the function factory default settings, click the Advanced tab, check the Rename Function Factory or use an existing Function Factory check box, and type in the function factory name to which the tool belongs. Click the browse button if the custom function factory already exists in the same project.
  7. Type in the Toolset category and click Next as shown in the following screen shot:



    The Add Geoprocessing Input Parameters dialog box opens.

    The Delete Features tool can delete features from a feature class, a feature layer, or a shapefile. Features are deleted based on the SQL Expression you provide. The parameters for the Delete Features tool are as follows:
    • Input parameters
      • in_features—A feature layer (.lyr) file or feature class from which the features will be deleted
      • in_SQLExpression—The SQL expression dependent on the in_features parameter that queries the features to be deleted
    • Output Parameter
      • out_feature—The derived output feature class
  8. To create these parameters, in the Add Geoprocessing Input Parameters dialog box, click New as shown in the following screen shot:



    The Parameter Properties dialog box opens.
  9. Define the Name, Display Name, and Description parameter properties for in_features.
  10. For the Data Type parameter, click the browse button. The Geoprocessing Data Types dialog box opens so you can select the appropriate parameter values. The in_feature parameter accepts feature classes and feature layers.
  11. Select the DEFeatureClass and GPFeatureLayer values as shown in the following screen shot:



    If you select more than one value for the parameter, the wizard automatically designates it a composite data type. For more information about data types, see Defining data types for tool parameters.

    The Direction parameter property identifies whether the parameter is an input or an output parameter. Since you are creating input features, the wizard automatically identifies it as input.
  12. Select Required from the Type drop-down box since in_features is a required parameter for execution of the tool. For more information about setting parameter properties, see Creating a custom function tool class.
  13. Click OK. The in_features parameter that you created is listed under Input Parameters as shown in the following screen shot:


  14. Click New to create another input parameter using the following parameter properties:
    • Name: in_sqlExpression
    • Display Name: Input SQL Expression
    • Description: The SQL Expression to select the features to be deleted
    • Data Type: GPSQLExpression
    • Direction: Input
    • Type: Required
    • Dependency: in_Features (The SQL expression is dependent on the input features.)
    The Parameter Properties dialog box will look like the following screen shot:

  15. Click OK.
    The Add Geoprocessing Input Parameters dialog box opens.
  16. Click Next as shown in the following screen shot:



    The Add Geoprocessing Output Parameters dialog box opens. 
  17. Click New. See the following screen shot:



    The Parameter Properties dialog box opens.
  18. Fill in the parameter properties for the out_features output parameter as shown in the following screen shot:



    The Direction property is defined as output by the wizard. This property is disabled on the dialog box and cannot be modified. The Type property is defined as derived because the out_features parameter is dependent on the in_features parameter. 
  19. Click OK. The Add Geoprocessing Output Parameters dialog box opens as shown in the following screen shot. The out_features parameter is now listed under Output Parameters.
  20. Click Next.

     

Specify the license requirements and create a help file

To specify the license required for your tool and to create a help file for your tool, perform the following steps:
  1. In the Specify help and license information dialog box, select the appropriate product license and extension license required for the operation of your tool.
  2. If necessary, modify the default tool help file name that will be created by the wizard at <ArcGISInstallDir>\help\gp.
  3. Click Finish.

    These steps are shown in the following screen shot:



    The wizard creates the custom geoprocessing function tool class and the custom function factory Java class in the specified Eclipse project based on your inputs. The wizard also auto-generates some of the method implementation based on those inputs as shown in the following screen shot:



    The custom function tool class and the getName()getDisplayName(), and getFullName() implementations are generated by the information you provided in the Geoprocessing Tool dialog boxes. The getParameterInfo() implementation is based on the input and output parameter properties you provided in the Add Geoprocessing Input Parameters and Add Geoprocessing Output Parameters dialog boxes. The getMetaDataFile() and isLicensed() implementations are based on the information you provided in the Specify help and license information dialog box.

    The @ArcGISExtension annotation is added to your class definition. The auto-generated getName() method returns the function factory name that you provided. The getFunction() method returns the function tool associated with the factory and the getFunctionName() and getFunctionNames() methods create FunctionName objects based on the input tools created through the wizard.

Define the tool's operation

The Java Delete Features tool deletes features selected by the SQL Expression from the input features. The BaseGeoprocessingTool.execute() method defines a tool's operation. To define the Delete Features tool's operation, perform the following steps:
  1. Check for an overwrite output value since the output parameter (out_features) is a derived type parameter and it overwrites the input parameter data (in_features).
  2. Unpack the parameter values.
  3. Create a query based on the SQL Expression and delete the features.
These steps are shown in the following code:
[Java]
public class CustomDeleteFeatures extends BaseGeoprocessingTool{

    //...

    /**
     * Execute the tool.
     */
    public void execute(IArray paramvalues, ITrackCancel trackcancel,
        IGPEnvironmentManager envMgr, IGPMessages messages){
        try{
            //Get the overwrite output value. 
            IGeoProcessorSettings gpEnv = new IGeoProcessorSettingsProxy(envMgr);
            if (!gpEnv.isOverwriteOutput()){
                messages.addError(esriGPMessageSeverity.esriGPMessageSeverityError, 
                    "The Geoprocessing settings did not allow OverwriteOutput");
            }
            if (messages.getMaxSeverity() ==
                esriGPMessageSeverity.esriGPMessageSeverityError){
                return ;
            }
            else{
                //Input features parameter.
                IGPParameter inputFeaturesParameter = (IGPParameter)
                    paramvalues.getElement(0);
                IGPValue inputFeaturesValue = this.gpUtilities.unpackGPValue
                    (inputFeaturesParameter);
                //Retrieve the input as a feature class.
                IFeatureClass[] ifc = new IFeatureClass[1];
                ifc[0] = new IFeatureClassProxy();
                this.gpUtilities.decodeFeatureLayer(inputFeaturesValue, ifc, null);
                FeatureClass inputFeatureClass = new FeatureClass(ifc[0]);

                //SQL Expression parameter.
                IGPParameter expressionParameter = (IGPParameter)
                    paramvalues.getElement(1);
                IGPValue expressionValue = this.gpUtilities.unpackGPValue
                    (expressionParameter);
                String expression = expressionValue.getAsText();
                messages.addMessage("\tDeleting selected feature(s)...");

                //Delete the features.
                Workspace wsEdit = null;
                try{
                    wsEdit = new Workspace(inputFeatureClass.getWorkspace());
                    wsEdit.startEditing(false);
                    wsEdit.startEditOperation();
                    messages.addMessage(
                        "\nexecute() in DeleteFeatures: Num features before delete :" + inputFeatureClass.featureCount(null));
                    //Use the SQL Expression provided to determine the features to delete.
                    QueryFilter deleteFilter = new QueryFilter();
                    deleteFilter.setWhereClause(expression);
                    inputFeatureClass.deleteSearchedRows(deleteFilter);
                    messages.addMessage(
                        "\nexecute() in DeleteFeatures: Num features after delete : " + inputFeatureClass.featureCount(null));
                    wsEdit.stopEditOperation();
                    wsEdit.stopEditing(true);
                }
                catch (Exception e){
                    if (wsEdit != null && wsEdit.isBeingEdited()){
                        wsEdit.stopEditing(false);
                    }
                    e.printStackTrace();
                }
            }
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

    //...
}

Export the tool as a JAR file

The custom geoprocessing tool you created must be bundled as a Java Archive (JAR) file before it can be deployed to ArcGIS. Export the function factory Java class and the custom function tool Java class as a JAR file—for example, as custom_delete_features.jar.
For more information about creating a JAR file in Eclipse IDE, see How to export extension classes as a JAR file.

Deploy the tool

To consume the custom tool in ArcToolbox as well as in Java applications, the custom geoprocessing function tool JAR file must be deployed to ArcGIS. To deploy the tool, move the JAR file (custom_delete_features.jar) to the <ArcGIS Install Dir>\java\lib\ext folder. An ArcGIS application (ArcMap, ArcCatalog, an ArcGIS Engine application, or an ArcGIS Server application) recognizes the JAR file as a custom geoprocessing function tool when the application is started. If the ArcGIS application is already running, it must be restarted after the JAR file is deployed. In the case of testing, if you modify the code in the Java classes bundled in the JAR file, the JAR file must be recreated and redeployed. Restart the ArcGIS application after redeployment as well.

Consume the tool

After deployment, the custom geoprocessing tool can be consumed in ArcToolbox or in a Java application using the ArcObjects Geoprocessing application programming interfaces (APIs). To consume the custom geoprocessing tool in ArcToolbox, perform the following steps:
  1. To create a new toolbox and add the custom Delete Features tool, right-click ArcToolbox and click New Toolbox.
  2. Right-click the new toolbox, click Add, and click Tool.
  3. In the Add Tool dialog box, select CustomJavaFunctionFactory, and click OK.

    These steps are shown in the following illustration:


  4. To execute the tool in ArcToolbox, double-click the tool. The description of the tool and its parameters are displayed in the help window. The output feature is not shown in the tool dialog box since it is of parameter type derived.
  5. Click the Input Features open folder icon button and browse to the test data (for example, %ArcGISHome%\java\samples\data\usa\states.lyr).
  6. Make a copy of the test data, since some features might be deleted.
  7. Click the SQL Expression icon button. The Query Builder dialog box opens.
  8. Build the query to select the features to be deleted.
  9. Click OK to execute the tool.

    These steps are shown in the following screen shots:


  10. Verify in ArcCatalog that the selected features were deleted.
You can also test the tool by consuming it in a Java application. For more information, see How to consume custom geoprocessing tools in Java applications.


See Also:

How to build custom geoprocessing tools
Creating a custom function tool class
Creating a custom function factory




Development licensing Deployment licensing
Engine Developer Kit Engine Runtime
Server Server
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo