JS Variable Text Output As Input Parameter For Geoprocessing Tool

2535
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
LloydBronn
Frequent Contributor

OK. Thanks. I'll try it out

0 Kudos
KevinHibma
Esri Regular Contributor

I might be jumping in a little late here - and maybe I dont understand - why are you taking the 2 input X/Y values, writing them to a text file and sending that to the GP Service? I dont understand why you're putting the XY into the textfile. Is the goal to return a textfile to the end user, or as I understand it, pass the textfile to the service. 

If the service ultimately just needs the XY value, I think adding in a text file is just complicating it. You should still be able to achieve your workflow by passing the XY value to the GP Service.

LloydBronn
Frequent Contributor

The GP service will run a Python script. That script takes it's inputs from a text file, and currently the text file is written in the Python script itself and saved locally. It's probably convoluted, but it's the way my company's scripts are written. These are long, complicated scripts that pull data from SQL databases and/or rasters, and create charts in Excel using VB scripts. I'm loath to try to alter them too much. I think I just need the input parameters from the GP service to take the place of the "get parameter as text" fields in the Python script. Those parameters are written to the text file. I've been entering them manually to make sure the GP tool works. Sorry if my posts have been confusing, I'm probably making this way more complicated than it needs to be. 

0 Kudos