How to use defined parameters in a tool ?

6586
14
Jump to solution
08-07-2014 04:15 AM
CARMETSandrine
New Contributor II

I'm very new at python, and I don't understand how to use the defined parameters in the source code of the tool.

I'm trying to create a tool (with clip and contour tools) who can be used by any user (not specified folders or classes). My script is in attachment.

Can somebody take a look ? :S

Thanks

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

To get you started, have a look at the code below. Use this script by adding it as a script to a new toolbox.

Define the 5 parameters as explained on line 18 - 22

#-------------------------------------------------------------------------------

# Name:        clip_contour.py

# Purpose:     clip raster and create contours

#

# Author:      Sandrine Carmet

#

# Created:     08/08/2014

#-------------------------------------------------------------------------------

class LicenseError(Exception):

    pass

def main():

    import arcpy

    import os

    # read the parameters

    fc_extent = arcpy.GetParameterAsText(0) # define as featurelayer, filter polygon, direction input

    ras_in = arcpy.GetParameterAsText(1) # define as rasterlayer, direction input

    ras_clip = arcpy.GetParameterAsText(2) # define as raster dataset, direction output

    fc_contour = arcpy.GetParameterAsText(3) # define as featureclass, direction output

    interval = arcpy.GetParameter(4) # define as Double, set min and max range, provide default

    # configuration of contours

    contour_interval = interval # not 4...

    base_contour = 0

    z_factor = 1

    try:

        if arcpy.CheckExtension("3D") == "Available":

            arcpy.CheckOutExtension("3D")

        else:

            # raise exception

            raise LicenseError

        # set in memory workspace

        arcpy.env.workspace = "IN_MEMORY"

        # Determine the extent of the fc

        extent = arcpy.Describe(fc_extent).extent

        ext_txt = "{0} {1} {2} {3}".format(extent.XMin, extent.YMin, extent.XMax, extent.YMax)

        # Clip the raster

        arcpy.Clip_management(ras_in, ext_txt, ras_clip, fc_extent, "#", "ClippingGeometry")

        # Create the contours

        arcpy.Contour_3d(ras_clip, fc_contour, contour_interval, base_contour, z_factor)

        # return license

        arcpy.CheckInExtension("3D")

        # outputs are automatically added to the TOC...

    except LicenseError:

        print("3D Analyst license is unavailable")

    except arcpy.ExecuteError:

        print(arcpy.GetMessages(2))

if __name__ == '__main__':

    main()

Once you run the script successfully, you can go to the Results windows and see the syntax for executing the script.

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script

# The following inputs are layers or table views: "pols", "DEMsmpro"

arcpy.ClipRasterCreateContours("pols","DEMsmpro","D:/Xander/grd/test_clip","D:/Xander/tmp/test2.gdb/test_contours","50")

You could create another script that simply executes this script or change the script to work with a list of inputs.

Kind regards, Xander

View solution in original post

14 Replies
JohannesBierer
Occasional Contributor III

InRaster = arcpy.GetParameterAsText(0)

Output = arcpy.GetParameterAsText(1)

CARMETSandrine
New Contributor II

humm this is not the answer that I was looking for...But thanks for answering me back

When I start my script, there is an Error:

"Traceback (most recent call last):

  File "<string>", line 88, in execute

  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 12021, in Clip

    raise e

ExecuteError: ERROR 999999: Error executing function.

Failed to execute (Clip)."

So how can I get the extent from my shapefile to make my clip tool work:

arcpy.Clip_management(RasterWorkspace + "\\" + RasterFile, "#", RasterWorkspace + "\\" + OutRasterFile, inputWorkspace + "\\" + ShapeFileName + ".shp", "0", "ClippingGeometry")

?

0 Kudos
CARMETSandrine
New Contributor II

Hi Sol,

Yes I've already done that. But the problem is that I want to generalize my script, so that the user of my tool will be able to choose is own shapefile (for extent to the clip tool) and his raster file. But I can't find the properly form to insert my parameters in my tool.

Can you have a look to my script in attachment above, to see if you have any ideas ?

Thanks for helping.

0 Kudos
CARMETSandrine
New Contributor II

Thank you very much for the advice Sol,

I'm going to see if they can help me in the python forum.

Sandrine

0 Kudos
JohannesBierer
Occasional Contributor III

In addition to the export out of a model you could print the variables you put in the clip tool to see if there's something wrong:

arcpy.AddMessage(RasterWorkspace + "\\" + RasterFile) ......

0 Kudos
CARMETSandrine
New Contributor II

The messages didn't show me something wrong.

It seems that the problem come from the shapefile (inputWorkspace + "\\" + ShapeFileName + ".shp")  that I used as an extent for the Clip tool.

Maybe I should use an other function to obtain the extent values from the shapefile, and then put it directly in the clip tool. But I don't know how to do that... I'm going to continue my research.

0 Kudos
JohannesBierer
Occasional Contributor III

As I can see it's all OK, so I don't know what could be wrong.

It's quite sophisticated what you are doing. Twice the use of parameters[0].valueAsText? One time .valueastext = not correct?

I would try to put the shape in the clip syntax hard coded, to see if it works?

CARMETSandrine
New Contributor II

I think I went to far with my script by trying things. I'm going to simplify it as possible.

For the clip, I'll try what you say to see if it works !

Thankss Johannes!

0 Kudos
AdamFeidt
New Contributor II

These links should help:

Create the custom script tool and define the input parameters via the Add Script Tool Wizard.

http://resources.arcgis.com/en/help/main/10.1/index.html#/Adding_a_script_tool/00150000001r000000/

Access the input parameters and plug them into your Clip_management code:

http://resources.arcgis.com/en/help/main/10.1/index.html#/Accessing_parameters_in_a_script_tool/0015...