Geoprocessing with JS API 4.0

3956
6
Jump to solution
06-13-2016 09:08 AM
AbbyGallegos
New Contributor II

I am having trouble figuring out how to call an asynchronous geoprocessing service with the new 4.0 API.  I have looked at the example for calling the sychronous service, but I didn't see any examples for an asynchronous service.  I understand that it requires using a "promise", which I am trying to learn to use, and it appears that I have to use "getResultImageLayer()" or "getResultImage()", but I don't understand what I would use for the parameters "resultName" and "imageParams".  In the 3.x API, I had to use the URL for the MapServer of the asynchronous service along with the JobId, which would return the result as a layer.   I guess I am just confused how this now works in the 4.0 API.  I wish there was an example of using an asychronous service.   Does anyone know of any examples?

Thanks,

Abby

1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there,

Please try this. I tested it and it works:

function drawResultData(result) {

          console.log(result);

          var resultLayer = gp.getResultImageLayer(result.jobId);

          map.layers.add(resultLayer); 

}

View solution in original post

6 Replies
UndralBatsukh
Esri Regular Contributor

Hi there,

As you have mentioned submiJob() returns an instance of promise and it takes 3 arguments:

'callback'- the function called when the promise is resolved

'errback', - the function called when the promise is rejected.

'progback' - the function called when the promise has progress.

More about Promise check this document​.

So you can run a geoprocessing service asynchronously as shown below:

//gp params as required 

var params = {

     "in_features": featureSet,

     "material_type": "Chlorine"

};

//run gp service

gp.submitJob(params).then(gpResultData, errBack, progTest);

// This function is called when gp resolves successfully

// result contains GP service result

function gpResultData(result) {

  console.log(result);

}

// This function is called if gp service is rejected

function errBack(err){

    console.log("gp error: ", err);

}

//use this function to check on the progress of gp service

function progTest(value){

   console.log(value.jobStatus);

   console.log(value);

}

Hope this helps.

0 Kudos
AbbyGallegos
New Contributor II

Thanks Undral for the reply, but I am still a bit confused on how to call "getResultImageLayer" to display the result on the map.  Also, I am getting an error that I don't understand when trying to create a new "ImageParameters()".  See attached code.

Thanks for your help.

Abby

0 Kudos
UndralBatsukh
Esri Regular Contributor

I deleted last message because I failed to understand your issue. I have not tried it myself but pass in null for the ImageParameters when calling getResultImageLayer. I will test it here shortly.

0 Kudos
AbbyGallegos
New Contributor II

Undral, you stated that I should be able to access the results of my GP service from the result object.  The output of the service that I am trying to access has a Data Type = GPRasterDataLayer.  When I was using the 3.x API, I was able to just instantiate a new ArcGISDynamicMapServiceLayer with the MapService URL and the jobId appended to it, then add the layer to the map.  It worked in 3.16 API.

However, I am now using the 4.0 API with 3D, and I do see the result object in the console.log(result), but I am confused as to how to add it to the map for display.  The job succeeds and within the result object I see a "results" with "Viewer_Viewshed" (the data layer I am trying to display).  However, I don't know how to add it to the map to display it in my 3D view.  This is the code I am using:

function drawResult(result) {

     console.log(result);

     var gpMapServiceURL = gpMapUrl + "/" + result.jobId;       // get the url to the MapService with the JobId

     var resultImage = new MapImageLayer({

          url: gpMapServiceURL

     });

     map.add(resultImage);             // Not able to see the result on the map

}

gpResult.PNG

I get no errors (using Firebug in firefox), but the result image does not show up on the map.  By the way, it does work using 3.16 API.

I really appreciate your help.  I would like to get this to work.

Thanks,

Abby

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there,

Please try this. I tested it and it works:

function drawResultData(result) {

          console.log(result);

          var resultLayer = gp.getResultImageLayer(result.jobId);

          map.layers.add(resultLayer); 

}

AbbyGallegos
New Contributor II

Awsome, thanks Undral, this works for what I needed.

I really appreciate your help!

Abby

0 Kudos