Issue
I'm trying to put some functioning code into a python toolbox in arcgis (Python 2.7). I've run the code externally using arcpy. I've setup a new python toolbox, edited a single parameter, a string with a site code e.g. SiteX. This should set the default online_id to "SiteX" and the SiteCode to "SiteX20".
The messages list all the feature classes but it is not liking arcpy.AssignDefaultToField_management()
:
class Tool(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"""
param0 = arcpy.Parameter(
displayName="Site Code",
name="online_id",
# datatype="Field",
datatype="GPString",
# parameterType="Optional",
parameterType="Required",
direction="Input")
params = [param0]
# params = None
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):
online_id = parameters # user request required
# online_id = arcpy.GetParameterAsText(online_id)
# online_id = raw_input('Enter your value :')
dateyr = time.strftime("%y", time.localtime())
sitecode = str(online_id) + str(dateyr)
arcpy.env.workspace = arcpy.env.workspace + "\\survey"
fclist = arcpy.ListFeatureClasses()
arcpy.AddMessage(arcpy.env.workspace)
arcpy.AddMessage(online_id)
arcpy.AddMessage(sitecode)
# Loop through all the feature classes and assign the default value for online_id
for fc in fclist:
arcpy.AddMessage(fc)
arcpy.AddMessage("{0} has {1} features.".format(fc, online_id))
arcpy.AssignDefaultToField_management(in_table=fc, field_name="online_id", default_value=online_id)
# arcpy.AssignDefaultToField_management(in_table=fc, field_name="site_code", default_value=sitecode)
return
Edit
The error message given by arcgis:
Executing: Tool X
Start Time: Tue Oct 06 15:57:33 2020
Running script Tool...
S:\PROJECTS\Workflow\GN_Coding\OA\03 GIS Projects - Coding Review\Geodatabase\DRS_survey_data.gdb\survey
[<geoprocessing parameter object object at 0x50DF5FC0>]
[<geoprocessing parameter object object at 0x50DF5FC0>]20
drafting_ply
drafting_ply has [<geoprocessing parameter object object at 0x50DF5FC0>] features.
Traceback (most recent call last):
File "<string>", line 75, in execute
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\management.py", line 3574, in AssignDefaultToField
raise e
RuntimeError: Object: Error in executing tool
Failed to execute (Tool).
Failed at Tue Oct 06 15:57:40 2020 (Elapsed Time: 7.53 seconds)
Solution
You should set online_id = parameters[0].value
. This should give you online_id set to a string since your parameter is a string type.
Answered By - Anna Forrest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.