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.
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
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
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"
)
So, looking at those error messages, it looks like you have the following problems:
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"
)
Thanks @AlfredBaldenweck ,
1.) Yes, I have only been testing CSVs. Added a filter anyway.
2.) I should've had this filled out completely to begin with. It's a lot clearer now.
3) Added a filter here too. Seems to be fine now.
All in all those were good fixes. It's narrowed down to two errors tied to the GeocodeFile() function which have pretty unhelpful pages. Error 003346 and Error 002617
arcgisscripting.ExecuteError: ERROR 003346: Geocode File failed.
ERROR 002617: Batch Geocoding stopped with status esriJobFailed.
Failed to execute (GeocodeFile).
Things I've looked into regarding the geocoding errors:
1. Our locators are hosted on Enterprise Portal v. 11.3 which allows batch geocoding up to 1000 rows.
2. I've tested with different CSVs, different addresses and different field value formats.
3. The locator coordinate system is set for NAD_83.
4. I've been trying different locators and the tool has not ran with any of them while every time it has produced at least one of the two errors above. Error 002618 was also produced once too.