5

Here's the end result I'm trying for:

  • the user clicks on a feature on an ArcGIS Server JS map
  • a Python script is triggered on the server
  • the Python script uses an attribute from the feature (eg its OID) to perform some calculations (using arcpy modules)
  • the results are returned to the infoWindow as HTML, for display

I guess I'd need to be able to trigger a Python script (containing arcpy commands) on the server, passing parameters between the client and server.

Assuming this is possible, what is the best approach:

  • via the ArcGIS Server geoprocessing framework
  • directly via an AJAX call to the Python script (and is that even possible)?
  • something else?
  • Have you had a look at this response: stackoverflow.com/questions/2201561/…? It seems to be doing what you are asking. – Mr. Concolato Mar 3 '15 at 20:18
  • Thanks @Mr.Concolato - good to know. Since I'm relying on arcpy functionality, the AJAX approach probably requires a dedicated installation of ArcGIS Desktop on the server(?) so the geoprocessing service approach may be more suitable? – Stephen Lead Mar 4 '15 at 21:42
4

This is the precise use case for a geoprocessing service. Write your script and add it to a geoprocessing toolbox on the server as a script tool.

You can test it on the server while developing the script by hard coding the parameters. Once you're comfortable with your code, replace those hard coded parameters with arcpy.GetParameterAsText(), add to a geoprocessing toolbox as a script tool and publish to ArcGIS Server as either a synchronous (Execute Task) or asynchronous (Submit Job) service.

You can then use the geoprocessor (https://developers.arcgis.com/javascript/jshelp/intro_gp_overview.html) in the Javascript api to send a request to the service as well as handle the result.

  • Thanks Jeremy. I'm experienced in Python scripts on the client, but a total novice at using them via ArcGIS Server, so I'll give this a try – Stephen Lead Mar 2 '15 at 0:57
  • Sure thing, feel free to let me know if you have any issues. I'm quite certain that you won't have many issues getting it working and published. For a python developer, the more challenging part is going to be writing the Javascript code to call the service and handle the result. – Jeremy Hazel Mar 2 '15 at 1:31
  • What is a geoprocessing service states a task takes simple data captured in a web application, processes it, and returns meaningful and useful output in the form of features, maps, reports, and files - but what if I want to return text, in the form of HTML? – Stephen Lead Mar 2 '15 at 3:11
1

Having trouble adding a comment from my phone, so posting here...

I would recommend that you become familiar with JSON, if not already. I would develop your service to return a JSON object that contains key/value pairs that hold the results of your calculation. That JSON object can be easily parsed in your javascript, placed into existing html or used to create new html which can be displayed in your infoWindow.

  • This is probably veering towards needing a new question, but I'll persist here for the moment. Assuming I've built up a JSON object in Python - what is the command to return the JSON object from the server to the client? thanks – Stephen Lead Mar 2 '15 at 4:34
  • 1
    arcpy.SetParameterAsText(json.dumps(<pythonDict>)) – GisJeremy Mar 3 '15 at 1:14
  • ... still working on it but gis.stackexchange.com/a/34968/3112 helps - I need to set a Derived output parameter in order to receive a response back from the script. – Stephen Lead Mar 6 '15 at 0:00
  • Sorry I'm not getting emails when you comment, feel free to pm me (if possible) and I'll be happy to share how we normally do this – GisJeremy Mar 12 '15 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.