Python Toolbox Derived Output Issues

8778
12
01-10-2016 06:07 PM
by Anonymous User
Not applicable

I am creating a python toolbox in ArcGIS 10.1 and have a python script working as a stand-along script but when I try to bring it into a python toolbox the tool does OK at creating an output XYZ event theme layer with the arcpy.MakeXYEventLayer_management tool and then I do create an output fc using arcpy.FeatureClassToFeatureClass_conversion which does create the fc on disk but when I try to use this fc as input in the next process to create a new fc that uses filter (sql_where_clause) the tool can not recognize the input fc that was just created on disk.  I am new at python toolboxes and think it is likely that I am not using the tools parameters properly. After having read all of the forums etc. that I could find I still am no closer to figuring this out. When I hard code the fc paths and use those as inputs into the next tool I use in the process the code can complete OK albeit not getting it using dynamic parameters.  The majority of the code I am using is below. Any suggestions about how to use the python toolbox parameters correctly?

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [Tool_Test]


class Tool_Test(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        """Define well location input csv file parameter definitions"""
        # First parameter
        param0 = arcpy.Parameter(
            displayName="Input File",
            name="in_file",
            datatype="DEFile",
            parameterType="Required",
            direction="Input")

        # To define a file filter that includes .csv and .txt extensions,
        #  set the filter list to a list of file extension names
        param0.filter.list = ['txt', 'csv']


        # Second parameter
        """Define derived well track fc parameter definitions"""
        param1 = arcpy.Parameter(
            displayName="Output Well Track Features",
            name="out_fc_well_track",
##            datatype="GPFeatureLayer",
            datatype="DEFeatureClass",
            parameterType="Derived",
            direction="Output")

##        param1.parameterDependencies = [param0.name]


        # Third parameter
        """Define derived well fc parameter definitions"""
        param2 = arcpy.Parameter(
            displayName="Output Well Features",
            name="out_fc",
##            datatype="GPFeatureLayer",
            datatype="DEFeatureClass",
            parameterType="Derived",
            direction="Output")

##        param2.parameterDependencies = [param0.name]


        #add each param to list
##        params = [param0, param1]
        params = [param0, param1, param2]

        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        # set local variables
        XY_Layer = 'in_file'
        out_fc_well_track = 'out_fc_well_track'
        out_fc = 'out_fc'

        # get input_fc name
        if parameters[0] != None:
            inCSV_file = parameters[0].valueAsText
            in_file_name = os.path.splitext(os.path.basename(inCSV_file))[0]
            arcpy.AddMessage("in_file_name: " + in_file_name)

            # Validate the output name so it is valid
            well_fc_name = arcpy.ValidateTableName(in_file_name)
            arcpy.AddMessage("input name validated: " + well_fc_name)

            out_fc_well_track = well_fc_name + "_well_track"
            if arcpy.Exists(out_fc_well_track):
                arcpy.Delete_management(out_fc_well_track)
            if arcpy.Exists(well_fc_name):
                arcpy.Delete_management(well_fc_name)


            arcpy.AddMessage("Start XYZ Tool...")
            # Make the XY event layer...
        ##    arcpy.MakeXYEventLayer_management(inCSV_file, x_coords, y_coords, XY_Layer, sr_In, z_coords)
            arcpy.MakeXYEventLayer_management(inCSV_file, "X_usft", "Y_usft", XY_Layer, sr_In, "TVD_usft")

            # create well track fc
##            arcpy.SetParameter(1,out_fc_well_track)
            arcpy.AddMessage("Start FC to FC Tool...")
            arcpy.FeatureClassToFeatureClass_conversion(XY_Layer, out_wrk_spc, out_fc_well_track)
            arcpy.SetParameter(1,out_fc_well_track)
            arcpy.AddMessage("Completed FC to FC Tool..." + out_fc_well_track)

            # create well fc
            out_fc = well_fc_name
            arcpy.SetParameter(2,out_fc)
            delimitedField = arcpy.AddFieldDelimiters(out_wrk_spc, where_field)
            sql_where = delimitedField + " IN('SHL', 'TPH', 'BHL')"
            arcpy.AddMessage("sql_where clause: " + sql_where)

            # using sql query to filter out SHL, TPH, and BHL from well track fc
            # TODO: remove hard code and use paramaters as intended...
##            out_fc_well_track = r'O:\Alaska\GIS\cook_inlet\maps\MRA\Code\TRS_Distances\data\MTRS_wells.gdb\SCU_322C_04_WP04___GIS_Format_well_track'
            arcpy.FeatureClassToFeatureClass_conversion(out_fc_well_track, out_wrk_spc, out_fc, sql_where)
##            arcpy.SetParameter(2,out_fc)
            arcpy.AddMessage("Completed FC to FC Tool..." + out_fc)


            # TODO: remove hard code and use paramaters as intended...
##            out_fc = r'O:\Alaska\GIS\cook_inlet\maps\MRA\Code\TRS_Distances\data\MTRS_wells.gdb\SCU_322C_04_WP04___GIS_Format'


            # Print the total rows
            if int(arcpy.GetCount_management(out_fc)[0]) == 0:
                arcpy.AddError("{0} has no features.".format(out_fc))
                arcpy.AddMessage('ERROR occurred because SHL, TPH, or BHL were not found in input file' )
                arcpy.AddMessage('Unable to continue! Please correct ERROR!' )
                raise arcpy.ExecuteError
            else:
                arcpy.AddMessage(out_fc + " has feature count of: " +  str(arcpy.GetCount_management(out_fc)))

             # add Well_Desc column
            arcpy.AddMessage("Adding 'Well_FC' field to well fc...")
            arcpy.AddField_management(out_fc, field_name, "TEXT", field_length=50)
            # update the new Well_Desc field
#            updateWell_Desc(out_fc)
            arcpy.AddMessage("Updated Well_Desc column!")

        return
0 Kudos
12 Replies
by Anonymous User
Not applicable

So, the question is:

How do you use the parameters properly when your XYZ input file creates a featureclass OK and to use that new fc created by the  arcpy.FeatureClassToFeatureClass_conversion tool as input into one more arcpy.FeatureClassToFeatureClass_conversion tool that is using a filter to create the next fc per the sql_where clause.

The first fc IS being created on disk but not recognized by the tool and throws an error that the "input parameters are not valid" for the next tool process.

0 Kudos
DanPatterson_Retired
MVP Emeritus

It is would be useful to show the messages that are produced to help isolate the place that is causing the bottle neck. You have and input xyz file, I thought copyfeatures would be the next tool to use, followed by a ffeaatureclasstofeatureclass since it has the sql query.  I would have expected a not found error rather than an input parameters not valid error

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Michael, Just out of curiosity, is there a reason why you are making a python toolbox and not just a python addin from a custom toolbox?  The main argument that I've heard for a python toolbox is if you need a pop up dialog (not just the tool dialog) or need to interact with the ArcMap display, otherwise I haven't found a need.  one issues however is adding won't work with Pro, but the custom toolbox/tools you create should.

,Personally I like the simplicity of creating the addins. If you have your script working as a standalone, do you mean that you have the script working as a tool, in a toolbox already?  If so, I have some steps for moving that into an addin That may help.

As for your error, I'm working with similar command right now in the scrip I am writing (i.e., selects, output, stinging output to input, etc) and when I've received the error you mentioned, it typically is because either it wants a Layer instead of a feature class, or vice versa.. It may help if you show the full error you are getting. I typically need to read between the lines to figure out the issue.

0 Kudos
by Anonymous User
Not applicable

The error I see when the tool fails is:

Traceback (most recent call last):

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

  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1547, in FeatureClassToFeatureClass

    raise e

ExecuteError: Failed to execute. Parameters are not valid.

ERROR 000732: Input Features: Dataset HVB_17_GIS_Format_well_track does not exist or is not supported

Failed to execute (FeatureClassToFeatureClass).

This occurs after the first FeatureClassToFeatureClass_conversion created output on disk but when I use that fc as input to the next FeatureClassToFeatureClass_conversion it doesn't recognize the newly created fc.

The main reason I am trying to get this script into a tool is so that I can have other GIS users I work with use it as they are not python compatible 

0 Kudos
DanPatterson_Retired
MVP Emeritus

Or simpler still What is a script tool?—Help | ArcGIS for Desktop   since you already know what tools you are using and you have easier control on defining the input/outparameters and their derivation etc.  You can easily use the results from the Results window to develop the script logic and ultimately the tool which you use in arctoolbox.  There is less code, it is all python and it is easier on error checking. Basically you are using system tools (including the sql) and this A quick tour of creating custom tools—Help | ArcGIS for Desktop  suggests that a pure python script of a simple tool would be the easiest since you don't require any interaction with the dataframe and you are using only system scripts.

Addendum

Have a look at What is a Python add-in?—Help | ArcGIS for Desktop to help you decide what type of "tool" you really need.  The little section Knowing When to create a python add-in might help you decide which route to go

by Anonymous User
Not applicable

I have the logic and the script working OK as a stand-alone but just trying to get it into a tool for other users to use.

0 Kudos
DanPatterson_Retired
MVP Emeritus

check the addendum I just added, it sounds like a conventional tool is all you need.  You can use the results of a manual run through your steps since they ae recorded in the Results window

"You are not allowed to use Modelbuilder": When Instructors need to get smarter

"You are not allowed to use Geoprocessing Results... ": The Students get smarter

It is easy to define your own parameters in a toolbox.  This link explains the differences between script tools and python tools.  the former is easier and is usually a first step from using modelbuilder or the Results window.  Creating a fully fledged python tool requires a need that a conventional tool in arctoolbox (or one of your own) wont provide

What is a script tool?—Help | ArcGIS for Desktop

by Anonymous User
Not applicable

I was able to get my script to run as a custom python toolbox (.tbx). I finally got the parameters right as it was much more straight forward than the python toolbox seems to be which I tried first. I will probably try again with the python toolbox but for right now I am going to give that a rest.

Thanks for your efforts and responses.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Glad you were able to resolve the issue.  Don't forget to mark and answers that were helpful (if any) and mark one as the answer, or if no answer, as "assumed answered" (not usually good to mark your own as an answer unless you write a summary that others can use to do the same).

0 Kudos