Select to view content in your preferred language

Python Toolbox: parameter data type to access a field(s) in a table

187
3
Wednesday
JaredPilbeam2
MVP Alum

I'm attempting to create a python toolbox script that geocodes a CSV. It will be my version of the Geocode Addresses tool. What do I need to do to get the Field parameter to accept the 'ADDRESS' field of my CSV? Right now, the 3rd param accepts a Field datatype. I didn't see a good option in parameter controls either. If i run this tool as is I receive errors. Two of the errors mention needing an output parameter, which I'll get to. I'm assuming Error 000800 is referring to the Field parameter, but beyond that I don't really know. Maybe it's in the script itself.

geo.png

 

class GeocodeFile:
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Geocode File"
        self.description = "Geocodes a local file and saves it back to the same directory."
        self.params = arcpy.GetParameterInfo()
        

    def getParameterInfo(self):
        """Define the tool parameters."""
        # 1st param
        param0 = arcpy.Parameter(
            displayName="Your CSV",
            name="your_csv",
            datatype="DEFile",
            parameterType='Required',
            direction='Input'
        )
        # 2nd param
        param1 = arcpy.Parameter(
            displayName="Geocoder",
            name='geocoder',
            datatype="DEAddressLocator",
            parameterType='Required',
            direction='Input'
        )
        # 3rd param
        param2 = arcpy.Parameter(
            displayName="Field",
            name='fields',
            datatype="Field",
            parameterType='Required',
            direction='Input',
        )
        # param2.controlCLSID = '{172840BF-D385-4F83-80E8-2AC3B79EB0E0}'
        # param2.value="ADDRESS"

        params = [param0,param1,param2]

        return params

    def isLicensed(self):
        """Set whether the 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."""
        import arcpy

        arcpy.geocoding.GeocodeFile(
            in_table=parameters[0].valueAsText,
            locator=parameters[1].valueAsText,
            address_fields=parameters[2].valueAsText,
            output_type=parameters[0].valueAsText,
            # output_location=r"C:\Users\test",
            # output_name="geocodedfile",
            # country=None,
            # location_type="ROUTING_LOCATION",
            # category=None,
            # output_fields="ALL"
        )

        return

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

 

g1178.png

 

 

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Python Toolbox: parameter data type to access a fi... - Esri Community

The field names have a dependency  on the input file and on a quick read the setup might be the same as in the code examples.  Under Value table parameters there is an example which sets this for the parameter list (eg param1.parameterDependencies = [param0.name] )

Custom toolboxes make this visually easier to setup if you want to experiment with that first


... sort of retired...
0 Kudos
JaredPilbeam2
MVP Alum

Thanks @DanPatterson ,

I'm using a Python Toolbox because the end product not mentioned here requires it. Value table got me a step closer it appears. Now I'm able to get param1 Address Field to populate with fields from the input CSV. I also gave it an out file param. But, all the params still don't seem to be functioning as a whole. 

 

 def getParameterInfo(self):
        """Define the tool parameters."""
        # 1st param
        param0 = arcpy.Parameter(
            displayName="Your CSV",
            name="your_csv",
            datatype="DEFile",
            parameterType='Required',
            direction='Input'
        )
        param1 = arcpy.Parameter(
            displayName='Address Field',
            name='ADDRESS',
            datatype='GPValueTable',
            parameterType='Required',
            direction='Input'
        )
        param1.parameterDependencies = [param0.name]
        param1.columns = [['Field', 'Field']]

        # 2nd param
        param2 = arcpy.Parameter(
            displayName="Geocoder",
            name='geocoder',
            datatype="DEAddressLocator",
            parameterType='Required',
            direction='Input'
        )
        # 3rd param
        param3 = arcpy.Parameter(
            displayName="Out File",
            name='out_file',
            datatype="DEFile",
            parameterType='Required',
            direction='Output',
        )
        params = [param0,param1,param2,param3]

        return params

def execute(self, parameters, messages):
        """The source code of the tool."""
        import arcpy

        arcpy.geocoding.GeocodeFile(
            in_table=parameters[0].valueAsText,
            locator=parameters[2].valueAsText,
            address_fields=parameters[1].valueAsText,
            output_type=parameters[0].valueAsText,
            # output_location=r"C:\Users\me\test",
            # output_name="geocodedfile",
            # country=None,
            # location_type="ROUTING_LOCATION",
            # category=None,
            # output_fields="ALL"
        )

 

JaredPilbeam2_0-1777565744612.png

 

 

 

0 Kudos
AlfredBaldenweck
MVP Frequent Contributor

So, looking at those error messages, it looks like you have the following problems:

  1. Is your input CSV actually a CSV? GeocodeFile() doesn't think it is, and I can't see the file extension on your screenshots. You might want to add a filter to that parameter to ensure you get the right file type in there.
  2. The issue for output location and output name is that you're not providing a destination for the output file to GeocodeFile(). For example, in both code blocks you've attached, those parameters are commented out. They look like they're required.
    1. Using the output parameter the way you're doing now is a good idea, but you're going to have to split the location and the name before feeding them to GeocodeFile()
  3. You're also going to have problems using the first parameter for the output format, since that parameter takes specific values. I plugged in "CSV" below.
def execute(self, parameters, messages):
        """The source code of the tool."""
        import arcpy
        import os
        outfile = parameters[3].valueAsText
        outloc = os.path.dirname(outfile)
        outname = os.path.basename(outfile)
        arcpy.geocoding.GeocodeFile(
            in_table=parameters[0].valueAsText,
            locator=parameters[2].valueAsText,
            address_fields=parameters[1].valueAsText,
            output_type= "CSV",
            output_location= outloc,
            output_name= outname,
            # country=None,
            # location_type="ROUTING_LOCATION",
            # category=None,
            # output_fields="ALL"
        )