Setting Feature Layer as Default in Geoprocessing Service written in Python

1633
10
02-19-2020 07:09 AM
by Anonymous User
Not applicable

Hello! I have a python script for a geoprocessing service. It takes all of the streams that lie inside of polygons selected from up to three different feature layers and runs different calculations (total length, cost, etc.).Currently, when you open the tool (in a widget), you have to specify each of the feature classes for each parameter. That is, there are three drop-down boxes under headings (HUC8, HUC10, County), and you have to select the correct feature class for each parameter.

What I would like to do is have each parameter default to a certain feature class so the user doesn't have to do it each time. I feel like this should be easy, but I can't figure it out (plus, I'm still very new at Python scripting.).

Should this be written in the script, or does it have to be set when the script is served online? Or...?

The script was written for Python 2 to be used in ArcMap (as opposed to ArcPro).

I know more information is needed to help you all answer my question. Please specify what more you need!

Annina

0 Kudos
10 Replies
DavidPike
MVP Frequent Contributor

I would remove these as parameters in your tool and hardcode the, or set a default value in your arcmap toolbox script properties then republished.

0 Kudos
by Anonymous User
Not applicable

Thanks for the response! I don't think I can hardcode the layers as there are other parts of the script that reference these parameters (I didn't write the script, I'm just trying to figure out how to set defaults). I tried setting the properties, but the Export option is greyed out, so I can't export the script to republish. I may be misunderstanding what you're suggesting, though. 

0 Kudos
DavidPike
MVP Frequent Contributor

I guess its password protected? I actually think you can set defaults in the gp widget but I'm not sure. I'd try and get hold of the script, though can you not run it, go to the results window and share as a service? The defaults could be changed in the publishing window maybe.

0 Kudos
by Anonymous User
Not applicable

I got to thinking about the publishing/sharing settings after I responded to you. I looked into it this morning and there's a section to set defaults when publishing. I never see this portion of the workflow because I'm not an admin! I'll have to update this/mark as answered later on next week when my administrator returns and gets it running. Thank you, David!

0 Kudos
by Anonymous User
Not applicable

Thanks for the suggestions, David, but it doesn't seem to work. You can set defaults when you serve the tool. But, if I choose defaults, I then lose the ability to make a selection in the tool. That is, the tool will run on the entire default layer and not a subset. That part is pretty important to us. Will be trying Vicky's suggestion next.

0 Kudos
VickyWang
Esri Contributor

Hi @Annina Rupe, 

You might want to use the config file for the pre-set value for parameters. There is configparser python library that can read the value from the config file.

eg.
config = configparser

config.get("program","layer1")  => get the value of 'HUC8' 

the example of config.ini

[program]

layer1=HUC8

layer2=HUC10

layer3=Countries

In the python code, to assign the value to your default drop-down list when you initialize the dropdown list. 

To publish the web tool with config.ini, need to select 'copy all' data option. the process copied the config.ini to the deploy directory in ArcGIS server.

the path looks like below (you can check the directories form ArcGIS Manager): 

C:\arcgis\arcgisserver\directories\arcgissystem\arcgisinput\ExportGPTool_n.GPServer\extracted\cd\gp_export\config.ini

To use the config.ini that allows you to update the default value after you deployed the web tool without changing the code. 

The other option will require that your layer list can select the default layer by logical pre-selection in the code; such as sorting, or the layer name contains a particular keyword, etc.

~Vicky Wang~
by Anonymous User
Not applicable

Vicky, Thanks for the detailed information! Will be doing some testing later on this week, so will let you know!

0 Kudos
by Anonymous User
Not applicable

Vicky,

I partially understand the config instructions. First of all, how do I create this config.ini file? As far as I know, there wasn't one automatically created. 

Then, when I have the config.ini file, do I insert the italicized text above exactly as it's shown above? And where in both files? I apologize; I'm just learning Python and it's not as intuitive as I thought.

Annina

0 Kudos
VickyWang
Esri Contributor

Hi Annina Rupe‌, 

The config.ini is a plan tex file. It can be created in any text editor (notepad, notepad++). Please reference to python configparser help document below.  

13.2. ConfigParser — Configuration file parser — Python 2.7.17 documentation

You will create the section and variable names in the config.ini.

eg.[program] is the section name; layer1, layer2, and layer3 are variable names. The HUC8, HUC10, Countries are the values of your variables which will be read into the python script. The config file location can locate in the same folder as the python script (or anywhere the script can access, as long as you define where to read in the script). There are examples from the python help doc. 

[program]   

layer1=HUC8

layer2=HUC10

layer3=Countries

~Vicky Wang~
0 Kudos