JS Variable Text Output As Input Parameter For Geoprocessing Tool

2261
12
01-12-2017 03:57 PM
LloydBronn
Frequent Contributor

I have a blank map document. When any point on the map is clicked, the xy coordinates are converted to lat/lon and displayed in a popup. This popup also has a link to a geoprocessing tool. Currently, this is just a ESRI sample GP tool that calculates the population within a 10km buffer around the point. I have created a script tool in ArcMap that runs a Python script that creates a chart in Excel from extracted raster values. I've successfully run this script in ArcMap and returned a chart.This will eventually be published on our server as a GP tool. The inputs for the Python script are latitude, longitude and location. At the moment I have these as user-entered parameters in the script tool. These parameters are used to write a text file that is passed in to the Python script. This may not be the most efficient way to go about this, but this is the way my companies scripts are written.  I want the input text file to be generated from the xy coordinates from the click event in the map. I've found a pretty helpful code snippet that does just that. The problem is, this text file is automatically downloads to the downloads folder on my local computer. Everything I've read suggests that it's impossible to specify a download directory because that opens our server to security risks. Is there any way around this with JS or HTML?

map.on("click",function saveTextAsFile(evt){
     
       var mp = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);
          var x = mp.x.toFixed(3);
          var y = mp.y.toFixed(3);
          var textToSave = x.toString() + "," + y.toString() + "," + "Location";
           var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
          var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
          var fileNameToSaveAs = "location.txt";
 
           var downloadLink = document.createElement("a");
            downloadLink.download = fileNameToSaveAs;
            downloadLink.innerHTML = "Download File";
            downloadLink.href = textToSaveAsURL;
            downloadLink.onclick = destroyClickedElement;
            downloadLink.style.display = "none";
            document.body.appendChild(downloadLink);
 
            downloadLink.click();
            
            });
0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

   For security reason as you have already stated there is no workaround for specifying a output location for JS client code. You can develop a .Net web service for this but I bet you will find that the LOE is to high for the meer reason of that is the way your GP is written. It would be much easier to re-factor your GP to accept a string value instead of a file.

0 Kudos
LloydBronn
Frequent Contributor

OK. How would I go about outputting the string values from JS to pass them into Python? 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,  


  Just like your current sample code passes th population GP a point you would pass a string.

0 Kudos
LloydBronn
Frequent Contributor

Here is the section of Python code I have now. It's set to take input parameters as text from the text file written from the click event. Would I need to write another function to output the results of click events as strings instead of writing a text file? 

 

location_filepath = "C:\\Website_Test\\Python_Scripts\\Test_Scripts\\Location_Input_Files"

Latitude = arcpy.GetParameterAsText(0)
Longitude = arcpy.GetParameterAsText(1)
Location = arcpy.GetParameterAsText(2)

coordinate_list = open(location_filepath + "\\" + "location.txt" ,"w")
coordinate_list.write(Latitude + ",")
coordinate_list.write(Longitude + ",")
coordinate_list.write(Location.strip())
coordinate_list.close()
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

   I am really confused here. I thought the workflow you were looking for was the user clicked on the map (in a JS app) and the click location was sent to a GP to be used as an input.

0 Kudos
LloydBronn
Frequent Contributor

That's right. The GP tool is a standalone Python script, not a model. For testing purposes I have it set to input the parameters as text, and then write these to a text file. The script then reads that text file and pulls data for those coordinates from a raster and creates a chart in Excel. This works perfectly running the GP tool in ArcMap. I'm adapting scripts that run behind the scenes every day on our local network. They are all set up to take string inputs from text files. Right now we attach these charts to set points in web map services, but we want the user to be able to create charts on the fly based on map clicks. 

 

Instead of manually entering the parameters, I want to just skip that step and have the text file written from the output lat/lon coordinates from the map click. Using the example above, I can write a text file directly in the JS, but of course I can't specify where that file goes. Would it be possible to place this text file (or the string values from it) on a dummy HTML page on our server and access the values from the URL address instead from a local drive location?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

   I am not GP expert but as the GP is running locally on the server you should be able to specify a location that the GP will write to. When developing GPs there is such a thing as a scratchworkspace that can be used so I would start googling there.

0 Kudos
LloydBronn
Frequent Contributor

Sure, but I still have to get the values from the map click out of the JS somehow. That's why I was asking about PHP. I haven't worked with it, but it seems like it's more secure than JS. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

   Sorry the capturing of the map click point and setting a string var to be passed to the GP service is so basic I am lost as to why you would think that you need another component like php to be added.

You are using the Geoprocessor class from the API right?

Geoprocessor | API Reference | ArcGIS API for JavaScript 3.19 

Your GP Service has certain defined input parameters and one of them should be a lat lon string.

0 Kudos