Working with GPServer


In this topic


Overview

The capabilities of an ArcGIS Server geoprocessing service using the SOAP application programming interface (API) are defined by the GPServerBindingStub class methods. These methods initiate stateless requests to a geoprocessing service and return results. The capabilities of GPServerBindingStub mirror the stateless ArcObjects interfaces implemented by the geoprocessing server object IGPServer. In addition, a geoprocessing service can generate results to display in a "buddied" map service. As a result, some value objects used with a MapServerBindingStub proxy are directly related to geoprocessing content. The GPServerBindingStub class is in the com.esri.arcgisws package (arcgis_agsws_stub.jar).
The following sample code snippets illustrate the use of selected methods to accomplish a common action. The samples use the Web service SOAP proxies (GPServerBindingStub and MapServerBindingStub) and value objects included with the Web Application Developer Framework (ADF) in the com.esri.arcgisws package. The same patterns apply to dynamically generated proxy classes and value objects except that the proxy class name is <service name>_GPServer and the namespace for the proxy and value objects is user defined.
The proxy and value objects for an ArcGIS Server service need only be generated once per service type. For example, once the Web service proxy and value objects for an ArcGIS Server geoprocessing service have been created, they can be reused for other ArcGIS Server Web geoprocessing services. For dynamic proxy classes, use the setURL method to define the Web service endpoint for the ArcGIS Server Web geoprocessing service you want to use. For Web ADF proxy classes that are pre-generated, the proxy constructor requires a URL input parameter to define the Web service endpoint for the ArcGIS Server Web geoprocessing service you want to use.

Iterating through input and output parameters of a geoprocessing server tool

The following sample code shows how to iterate through input and output parameters of a geoprocessing server tool.
[Java]
String endpoint = "http://myserver:8399/arcgis/services/my_gp_service/GPServer?";
GPServerBindingStub gpserver = new GPServerBindingStub(new java.net.URL(endpoint),
    null);
GPToolInfo gpti = gpserver.getToolInfo("ClipModel");
GPParameterInfo[] paramInfos = gpti.getParameterInfo();
GPParameterInfo paramInfo;
for (int i = 0; i < paramInfos.length; i++){
    paramInfo = paramInfos[i];
    System.out.println("Category: " + paramInfo.getCategory());
    String[] cl = paramInfo.getChoiceList();
    if (cl != null){
        System.out.println("Set of choices: ");
        for (int j = 0; j < cl.length; j++){
            System.out.println(cl[j]);
        }
    }
    System.out.println("Direction (input or output): " + paramInfo.getDirection()
        .toString());
    System.out.println("Data Type: " + paramInfo.getDataType());
    System.out.println("Display Name: " + paramInfo.getDisplayName());
    System.out.println("Name: " + paramInfo.getName());
    System.out.println("ParamType: " + paramInfo.getParamType().toString());
    System.out.println("Value: " + paramInfo.getValue().toString());
    System.out.println("===============");
}

Working with a geoprocessing service

The following sample code shows how to work with a geoprocessing service (and optional map service for output).
[Java]
String endpoint = "http://myserver:8399/arcgis/services/my_gp_service/GPServer?";
GPServerBindingStub gpserver = new GPServerBindingStub(new java.net.URL(endpoint),
    null);
GPToolInfo gpti = gpserver.getToolInfo("BufferModel");
GPParameterInfo[] paramInfos = gpti.getParameterInfo();
//Set the input values for user defined line, linear unit, and layer name.
// Create an input point feature to buffer.
GPFeatureRecordSetLayer gpfrs = (GPFeatureRecordSetLayer)paramInfos[0].getValue();
PointN p1 = new PointN();
p1.setX(1192415.297);
p1.setY(884757.266);

RecordSet rs = gpfrs.getRecordSet();
Fields flds = rs.getFields();
Object[] o = new Object[flds.getFieldArray().length];
Record rec = new Record();
for (int i = 0; i < flds.getFieldArray().length; i++){
    if (flds.getFieldArray()[i].getName().equals(gpfrs.getShapeFieldName())){
        o[i] = p1;
    }
    else if (flds.getFieldArray()[i].getName().equals(gpfrs.getOIDFieldName())){
        o[i] = 1;
    }
    else{
        EsriFieldType ftype = flds.getFieldArray()[i].getType();
        if (ftype == EsriFieldType.esriFieldTypeString){
            o[i] = "";
        }
        else{
            o[i] = 1;
        }
    }
}

rec.setValues(o);
Record[] recs = new Record[1];
recs[0] = rec;
gpfrs.getRecordSet().setRecords(recs);
//Create a linear unit to define the buffer distance.
GPLinearUnit gpLU = new GPLinearUnit();
gpLU.setUnits(EsriUnits.esriMeters);
gpLU.setValue(50000.0);
//Define a layer name and select features using the buffer.
GPString gpstr = new GPString();
gpstr.setValue("states");
//Set input values in the same order listed by the geoprocessing service.
GPValue[] gpvalues = new GPValue[3];
gpvalues[0] = gpfrs;
gpvalues[1] = gpLU;
gpvalues[2] = gpstr;
//Submit the job.
String JobID = gpserver.submitJob(gpti.getName(), gpvalues);
//Check the status of the job; if it's finished and successful, proceed - false delay.
while (gpserver.getJobStatus(JobID) != EsriJobStatus.esriJobSucceeded){
    Thread.currentThread().sleep(5000);

}

GPResultOptions gpro = null;
GPResult result = gpserver.getJobResult(JobID, null, gpro);
//Process geoprocessing results directly on the client,
//   -or-
//work with a map service to draw output.
// For example, process geoprocessing results on the client.
GPFeatureRecordSetLayer gpoutput_recordset = (GPFeatureRecordSetLayer)
    result.getValues()[0];
RecordSet recordset = gpoutput_recordset.getRecordSet();
int shapefld = 0;
for (int i = 0; i < recordset.getFields().getFieldArray().length; i++){
    Field f = recordset.getFields().getFieldArray()[i];
    System.out.println(f.getName());
    if (f.getName().equals(gpoutput_recordset.getShapeFieldName())){
        shapefld = i;
    }
}

for (int j = 0; j < recordset.getRecords().length; j++){
    Record record = recordset.getRecords()[j];
    Object[] objs = record.getValues();
    for (int k = 0; k < objs.length; k++){
        Object obj = objs[k];
        System.out.println(obj.toString());
        if (k == shapefld){
            PolygonN polyn = (PolygonN)obj;
            // Do something with the polygon on the client to render, calculate, and so on.
        }
    }
}

//For example, work with a map service to draw output.

if (gpserver.getResultMapServerName() != null){
    String map_endpoint = 
        "http://myserver:8399/arcgis/services/my_gp_service/MapServer";
    MapServerBindingStub mapserver = new MapServerBindingStub(new java.net.URL
        (map_endpoint), null);
    ImageType itype = new ImageType();
    itype.setImageFormat(EsriImageFormat.esriImageJPG);
    itype.setImageReturnType(EsriImageReturnType.esriImageReturnURL);
    ImageDisplay idisp = new ImageDisplay();
    idisp.setImageHeight(500);
    idisp.setImageWidth(600);
    idisp.setImageDPI(96);
    ImageDescription idesc = new ImageDescription();
    idesc.setImageDisplay(idisp);
    idesc.setImageType(itype);
    String dataframe = mapserver.getDefaultMapName();
    MapServerInfo msi = mapserver.getServerInfo(dataframe);
    MapDescription mapdesc = msi.getDefaultMapDescription();
    // Get the tool layer ID; the layerName is the same as the tool name "ClipParcels",
    // the subLayerName is the same as the output parameter "ClipOutput".  You must use
    // MapLayerInfo to get the layer names.
    GPParameterInfo paramInfo = null;
    String layerName = gpti.getDisplayName();
    String subLayerName = null;
    for (int i = 0; i < paramInfos.length; i++){
        paramInfo = paramInfos[i];
        if (paramInfo.getName().equals("ClipOutput")){
            subLayerName = paramInfo.getDisplayName();
        }
    }
    int toolLayerID =  - 1;
    int subLayerID =  - 1;

    for (int i = 0; i < msi.getMapLayerInfos().length; i++){
        MapLayerInfo layer = msi.getMapLayerInfos()[i];
        if (layer.getName().equals(layerName)){
            toolLayerID = layer.getLayerID();
        }
        else if (layer.getName().equals(subLayerName)){
            subLayerID = layer.getLayerID();
        }
    }
    LayerDescription[] lds = mapdesc.getLayerDescriptions();

    for (int i = 0; i < lds.length; i++){
        LayerDescription ld = lds[i];
        if (ld.getLayerID() == subLayerID){
            // Key: The source ID for the sublayer must be set to jobid.
            // The geoprocessing service knows to look for output data in the jobs directory.
            ld.setSourceID(JobID);
            ld.setVisible(true);
        }
        // Set the tool layer to visible to see geoprocessing output in the sublayer.
        else if (ld.getLayerID() == toolLayerID){
            ld.setVisible(true);
        }
        else{
            ld.setVisible(false);
        }
    }
    MapImage mapimg = mapserver.exportMapImage(mapdesc, idesc);

}