Help with using Geoprocessing task in JS application

3738
11
Jump to solution
07-21-2015 11:55 AM
JssrRR
by
Occasional Contributor II

Hi,

I am working with GP task for the first time and I am having some trouble getting it to work. I have published a synchronous GP service and I am trying to use that in a JS web application.

In my application all I want is a base map and I am using a map click to draw a buffer, with a user defined distance and select all the census block points that fall within that distance and output a summary of total block points, population and housing units within the buffer, I am not using a feature layer to select features or a result map service to draw the selected points on the map, but I want to use GP task to output a summary table.

What I am trying to do is, add the buffer graphics on the map and use it to input GP task result, something like in his sample which is using the user defined polygon to input the GP task, and getting a summary of population for that area.

http://developers.arcgis.com/javascript/sandbox/sandbox.html?sample=gp_zonalstats

The GP task is working fine, because I added This GP task to the geoprocessing widget in the web app builder and it is doing exactly like I want it:

I am having trouble adding it to JS application, here is a JS fiddle with what I have so far but is not working:

This is what JSON from the service looks like:

I have also attached the image of the model used to publish the service and the details from the REST service directory.

Any help will be greatly appreciated!!

0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Regular Contributor

To convert the point you would use this:

var ptGeo = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);

However, there are several other issues in the code that will also prevent it from working. I have created a modified version that uses your service and shows the results.

Please check the working example - GP Task  (link removed).

The source is available from this page (link removed).

Let me know if you have questions about the code.

EDIT - the links have been removed as requested, please see the update by Saloni Rajput​ with the working code sections.

View solution in original post

0 Kudos
11 Replies
OwenEarley
Regular Contributor

You may need to project the input point from your JS application to match the projection used in your service. From the REST service image the input point should be in Spatial Reference 102603 (3081) - Texas?

However, most ESRI web mapping applications use the spatial reference 102100 so the point you are sending to the service is most likely in the wrong coordinate system and you are getting no results.

0 Kudos
JssrRR
by
Occasional Contributor II

Owen,

I thought the adding this to the code while a creating the GP task object should take care of the projection and match the input to the projection of the map :

gpTask.setOutSpatialReference = map.spatialReference;

Let me know if you think I still need to republish the service with SR 102100.

Thanks.

0 Kudos
OwenEarley
Regular Contributor

I think this only changes the output geometries:

setOutSpatialReference(spatialReference)NoneSets the well-known ID of the spatial reference of the output geometries.

I haven't tried using this but you may be able to use setProcessSpatialReference() to force the task to complete the analysis using the map's spatial reference.

Alternatively, I would publish the service in WGS84 and use the webMercatorUtils to convert your map point to a geographic point.

JssrRR
by
Occasional Contributor II

Owen,

Thanks again for the quick response.

So you mean that if I publish the service in WGS84 and add this, in GP task input params?

like this

param.geometries = [webMercatorUtils.webMercatorToGeographic(feature.geometry)];

and  I will not have to worry about, setOutSpatialReference to map's spatial reference?

0 Kudos
JssrRR
by
Occasional Contributor II

Owen,

I have republished the GP service in WGS84 and I have also set the out spatial reference in the model's environments settings to  WGS 84.

I have added the require and function in the application but do not what is the actual code that would be needed I got this from the API reference something like this?

require(["esri/geometry/webMercatorUtils"],

function(webMercatorUtils) {var pt = Point(0, 0), // a geographic point.
    result
;
if (webMercatorUtils.canProject(pt, map)) {
  result
= webMercatorUtils.project(pt, map)

} });

Here is the updated JS fiddlle, if you could please look at that and let me know if the code looks right?

http://jsfiddle.net/1snzLwcm/

I have also updated my attachments to my  original post, with new REST inputs and JSON image

Thank you!

0 Kudos
OwenEarley
Regular Contributor

To convert the point you would use this:

var ptGeo = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);

However, there are several other issues in the code that will also prevent it from working. I have created a modified version that uses your service and shows the results.

Please check the working example - GP Task  (link removed).

The source is available from this page (link removed).

Let me know if you have questions about the code.

EDIT - the links have been removed as requested, please see the update by Saloni Rajput​ with the working code sections.

0 Kudos
JssrRR
by
Occasional Contributor II

Thanks, Owen for modifying the code and making it work. I will look at the changes. 

0 Kudos
JssrRR
by
Occasional Contributor II

Hi Owen,

I have modified the code in my application and is working now. Could you please delete the link to the URL of my service for the GP task and the source code, and I will paste the required code for others to refer.

Thanks

0 Kudos
OwenEarley
Regular Contributor

Done - can you make the relevant sections of code available on this post? Thanks.

0 Kudos