【ArcPy】Python工具的参数校验

发布时间 2023-10-28 18:02:26作者: yzhyingcool

在updateMessages方法中检查输入图层数据源的工作空间是否是本地数据,如果不是,设置错误。
在updateParameters方法中从图层派生出第4个参数,即输出要素类的路径。注意该参数的类型需要是“派生(Derived)”

import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""
    if self.params[0].value:
        desc=arcpy.Describe(self.params[0].value)
        path=desc.dataElement.catalogPath
        path=path.split('\\')
        path.pop()
        path='\\'.join(path)
        self.params[3].value=arcpy.CreateScratchName('JZD',data_type="FeatureClass",workspace=path)
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    if self.params[0].value:
        self.params[0].clearMessage()
        workspacePath=self.params[0].value.workspacePath
        desc=arcpy.Describe(workspacePath)
        if desc.workspaceType != 'LocalDatabase':
            self.params[0].setErrorMessage('The workspace type must be "LocalDatabase".')
    return