Using Output Parameters in Input Parameters in Script Tools

1924
7
Jump to solution
03-17-2018 01:02 PM
JoshuaBrengel
New Contributor III

Hi,

I created a script tool in ArcGIS using Python.   While the script tool is mostly functional, it does not complete the final step.  This step is as follows in the Python code (Note: the syntax highlighter doesn't seem to work correctly but I think the syntax is right):

try:
   # Add Parameters 
   outrast = arcpy.GetParameterAsText(7) # Output raster dataset clipped to stream buffer polygons 
   # Run the clip tool on hillshade using the output buffer fc 
   arcpy.Clip_management(outpath, "#", outrast, buffout, "ClippingGeometry", 'NO_MAINTAIN_EXTENT') 
   # Add a success message 
   arcpy.AddMessage("Clipping of hillshade to buffer output successful!") 
except: 
   # Report any error messages that the Buffer tool might have generated 
   arcpy.AddMessage(arcpy.GetMessage(0))
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I have already defined buffout as arcpy.GetParameterAsText(5) and used it as an ouput parameter in arcpy.Buffer_analysis in a previous step in my code not shown above.  In my script tool Properties, under the "Parameters" tab, I have set up buffout as a "Required" parameter with a direction of "Output".    I'm assuming my problem is that I can't use an output parameter as an input parameter in a later step of code without adjusting the code somehow. 

How do I write code to use this output parameter as an input parameter in the arcpy.Clip_management() portion of my code above?  I've struggled with this concept while designing script tools and it would be helpful to have a clear, exact method.  If this is not the problem and my assumption is wrong, I'd appreciate any thoughts on another solution.  Thanks!

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

You don't need to use SetParameterAsText if the path is set on input with GetParameterAsText. You only need to use SetParameterAsText for derived outputs, for example, if you had a feature class input and had a second output, you could set the path using:

out_path2 = out_path + "_2"

Why do you think you need derived parameters? You are specifying all your datasets as parameters so there is no need to generate new paths.

I do not see what your problem is, as there is no reason you can't use one tool's output to run another tool, and you didn't provide an error message from Clip_managment.

Except, perhaps you did not skip the nodata_value parameter by putting in a needed blank parameter value?

arcpy.Clip_management(outrast, "", outpath, buffout, 
    "ClippingGeometry", "", "NO_MAINTAIN_EXTENT")‍‍‍‍

View solution in original post

7 Replies
curtvprice
MVP Esteemed Contributor

Hi Joshua,

The way to make the output the same as the input is to use parameter type Derived and set its value in the code like this:

arcpy.SetParameterAsText(7, outrast)

Setting script tool parameters—Help | ArcGIS Desktop 

BTW, I recommend using "" instead of "#" to skip parameters (or use named parameters if you need to skip a lot of them.) The use of "#" is a throwback to the ArcGIS 9x geoprocessing command line, which needed a placeholder and also didn't support named parameters. This will help with your syntax highlighting.

JoshuaBrengel
New Contributor III

Thank you for your reply Curtis and for the tip about "" vs "#"!  I'm still not getting it, sorry. I do understand that I need to change an output parameter used as input to a "Derived" Type.  But, in general I'm still not understanding this concept and honestly, Esri''s help documents are not very clear on this topic in my opinion.

The output parameter I'd like to use as an input parameter is actually "buffout", not "outrast".  This "buffout" variable is the output parameter of arcpy.Buffer_management a few steps earlier in my script, something similar to:

buffout = arcpy.GetParameterAsText(5) # Feature class output of buffer analysis-  DO I GET RID OF THIS STEP??
buff_dist = arcpy.GetParameterAsText(6) # Buffer distance parameter
    
# Run the Buffer Analysis tool on stream feature
arcpy.Buffer_analysis(outfc, buffout, buff_dist)
    ‍‍‍‍‍
try:
    # Add Parameters
    outrast = arcpy.GetParameterAsText(7) # Output raster dataset clipped to stream buffer polygons
    
    # Run the clip tool on hillshade using the buffout feature class
    #arcpy.SetParameterAsText(8, buffout) IS THIS CORRECT PLACEMENT??
    arcpy.Clip_management(outpath, "", outrast, buffout, "ClippingGeometry", 'NO_MAINTAIN_EXTENT')
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If I want "buffout" from this earlier arcpy.Buffer_analysis step- line 1- as an input in the Clip_management step- line 13-, I'm assuming I'd use arcpy.SetParameterAsText(8 (or whatever next correct index is...), buffout) somewhere in my code, but where?

Also, when I set the "buffout" parameter as "Derived" in script tool properties, why does it not show up as a parameter when opening my script tool?

Thanks again!!

0 Kudos
curtvprice
MVP Esteemed Contributor

SetParameterAsText is used anytime after you've created the derived output. 

Note, derived parameters are not included in the tool dialog. For this reason, it's best to have derived parameters *last* in your list of parameters -- it's just less confusing that way.

Since the derived parameter is not provided in the tool dialog, you need to come up (ie calculate the path) inside your script somehow before you run the Buffer tool.

JoshuaBrengel
New Contributor III

Thanks for responding Curtis.  I really appreciate you taking the time to help me understand this concept.  It's starting to get clearer but I think I need to circle back around for clarity's and organization's sake (for myself and others in geonet community). 

Here is an re-arranged version of my full code for the original python script behind script tool that keeps giving me errors. Hopefully it clarifies my end goals:

# Set up parameters for script tool
inrast = arcpy.GetParameterAsText(0)# File path to original raster clipped to study area
outrast = arcpy.GetParameterAsText(1) #File path to raster that's been clipped to study area
outext = arcpy.GetParameterAsText(2) # Feature class used as the study area to which raster and streams are clipped
infc = arcpy.GetParameterAsText(3) # Input stream feature class to be clipped to study area
outfc = arcpy.GetParameterAsText(4) # Output clipped stream feature class
buff_dist = arcpy.GetParameterAsText(5) # Input Buffer distance parameter
buffout = arcpy.GetParameterAsText(6)# Output buffered stream feature class parameter
outpath = arcpy.GetParameterAsText(7) # Output raster dataset (raster clipped to buffered stream polys) parameter 
    
# Add Message
arcpy.AddMessage("Starting clipping analysis...")
        
# Run the Clip management Tool on Raster
arcpy.Clip_management(inrast, "", outrast, outext, "", 'ClippingGeometry', 'NO_MAINTAIN_EXTENT')
arcpy.AddMessage("Raster clipping to study area successful!")

# Run the Clip analysis Tool on Vector Stream lines fc
arcpy.Clip_analysis(infc, outext, outfc)
arcpy.AddMessage("Vector clipping to study area successful!")
    
# Run the Buffer Analysis tool on clipped stream (outfc)
arcpy.Buffer_analysis(outfc, buffout, buff_dist)
# Add a success message
arcpy.AddMessage("Buffer of streams successful!")
    
# Run the clip tool on hillshade using the output buffer fc
arcpy.Clip_management(outrast, "", outpath, buffout, "ClippingGeometry", 'NO_MAINTAIN_EXTENT')
    
# Add a success message
arcpy.AddMessage("Clipping of hillshade to buffer output successful!")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

As you can see, I'm not using any derived parameters in this script- just required output and input parameters.  I get the first clipped raster, the clipped stream feature class, and the buffered stream polygon feature class into a file geodatabase.  But every time I've run this tool, it gives me an error for the final Clip step.

I understand that derived parameters don't show up in tool dialog and that it's most effective to put them at end of parameters for organizational purposes.  Not exactly clear how to follow your suggestion of setting path of buffout.

Would calculate the path of buffer output look something like this?:

buffpath = os.path(buffout)

Then I'd use arcpy.SetParameterAsText():

arcpy.SetParameterAsText(8, buffout)‍‍‍‍

Finally, input the above into Clip Management tool:

arcpy.Clip_management(outrast, "", outpath, buffpath + buffout, "ClippingGeometry", "NO_MAINTAIN_EXTENT")

Also, would I be able to set other outputs from tools (ie outfc, outrast) as inputs using SetParameterAsText()?

Again, much thanks for your help Curtis!  Josh

0 Kudos
curtvprice
MVP Esteemed Contributor

You don't need to use SetParameterAsText if the path is set on input with GetParameterAsText. You only need to use SetParameterAsText for derived outputs, for example, if you had a feature class input and had a second output, you could set the path using:

out_path2 = out_path + "_2"

Why do you think you need derived parameters? You are specifying all your datasets as parameters so there is no need to generate new paths.

I do not see what your problem is, as there is no reason you can't use one tool's output to run another tool, and you didn't provide an error message from Clip_managment.

Except, perhaps you did not skip the nodata_value parameter by putting in a needed blank parameter value?

arcpy.Clip_management(outrast, "", outpath, buffout, 
    "ClippingGeometry", "", "NO_MAINTAIN_EXTENT")‍‍‍‍
JoshuaBrengel
New Contributor III

Hi Curtis,  thanks again for taking the time to answer my inquiries.  I don't script regularly in Python and pretty new to creating script tools.  Getting insight from an expert is helpful in my understanding of setting parameters, something I've struggled to understand.  

It was indeed my missing "nodata_value" parameter.  Should have been helped by the error I was getting (Error 000800: The value is not a member of ClippingGeometry | None). 

I guess I was thinking this error was generated because one of my input parameters (buffout or outrast) was an output of a previous tool and had to be set (by using SetParameterAsText and creating a parameter with Type "Derived") as an input parameter in my final clip_management step.  I see now that SetParameterAsText is not exactly relevant to my situation because my datasets are previously set as parameters (using GetParameterAsText()) and therefore, the script can call any of them as arguments (ie parameters) in gp tools later on in the script.

One final question, if you don't mind.  Can you point me to helpful resources on tips to reduce the number of overall parameters in a tool dialog for intermediate data?   I think wanting fewer parameters on this script tool's dialog also played into my attempted use of SetParameterAsText().  

Marked your answer as correct.  Thanks again!!

0 Kudos
curtvprice
MVP Esteemed Contributor

You are welcome!

You can generate paths for intermediate datasets inside the script and delete them at the end. Hope this helps.

temp1 = arcpy.CreateScratchName("", "", env.scratchGDB) # returns a full path
Make sure your script cleans up before it ends:
arcpy.Delete_management(temp1)‍‍‍
0 Kudos