在 arcgis 工具箱中传递参数

Passing parameters in the arcgis toolbox

我正在尝试将一些功能代码放入 arcgis (Python 2.7) 的 python 工具箱中。 我在外部使用 arcpy 运行 代码。 我设置了一个新的 python 工具箱,编辑了一个参数,一个带有站点代码的字符串,例如SiteX。 这应该将默认 online_id 设置为“SiteX”,并将 SiteCode 设置为“SiteX20”。

消息列出了所有功能 类 但不喜欢 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

编辑

arcgis给出的错误信息:

Executing: Tool X
Start Time: Tue Oct 06 15:57:33 2020
Running script Tool...
S:\PROJECTS\Workflow\GN_Coding\OA 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)

你应该设置online_id = parameters[0].value。这应该给你 online_id 设置为一个字符串,因为你的参数是一个字符串类型。