在arcgis工具箱中传递参数

时间:2020-10-06 14:33:52

标签: python python-2.x arcgis arcpy

我正在尝试将一些功能代码放入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\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)

1 个答案:

答案 0 :(得分:1)

您应该设置online_id = parameters[0].value。由于您的参数是字符串类型,因此应该可以将online_id设置为字符串。