Hello,
I have collaborated to the creation of a tool that transforms feature layer's coordinates from an unknown coordinate system to ETRS89. The tool allows the user to set the name of the newly transformed feature class through a string Parameter. If the name of the newly transformed feature already exist in the chosen dataset, the tool will return an error, however no error message appears before running the tool. I am aware that this error message can be set with validation from the script properties, as previously I have already set a similar message for another tool for an excising field name however, I am finding it difficult to do the same for this tool.
When I set the error message for the field name I used the following commands, where params[0] is the Input Feature Class and Params[1] is the Newly created field in the input feature class:
import arcpy
import os
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 parameter
has been changed."""
desc = arcpy.Describe(self.params[0])
flist = desc.fields
if self.params[1].valueAsText in [x.name for x in flist]:
self.params[1].setErrorMessage('Field name already exists')
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
desc = arcpy.Describe(self.params[0])
flist = desc.fields
if self.params[1].valueAsText in [x.name for x in flist]:
self.params[1].setErrorMessage('Field name already exists')
return
I am trying to do something similar for the feature layer name, as this time I am trying to describe the children of the dataset and then list their names. Here Params[2] is the input Dataset at which the transformed feature layer is saved, and Params[3] is an input string parameter that corresponds to the name of the transformed feature layer:
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 parameter
has been changed."""
desc = arcpy.Describe(self.params[2].children)
flist = desc.name
if self.params[3].valueAsText in [x.name for x in flist]:
self.params[3].setErrorMessage('Feature class name already exists')
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
desc = arcpy.Describe(self.params[2].children)
flist = desc.name
if self.params[3].valueAsText in [x.name for x in flist]:
self.params[3].setErrorMessage('Feature class name already exists')
return
However since no error is given when I test it I am obviously missing something, or am probably giving a totally wrong command. Any ideas are greatly appreciated!
Solved! Go to Solution.
I think it would error on 'x.name' since the string 'f' doesn't have a method of .name. Since .desc returns a string, doing [x for x in flist] iterates over the characters in the name. i.e, if the flist is 'fcname', you are checking x.name against 'f','c','n','a','m','e'.
flist = desc.name
if self.params[3].valueAsText in [x.name for x in flist]:
try this:
if self.params[3].valueAsText == flist:
Solved it! The validation should be as followed:
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 parameter
has been changed."""
desc = arcpy.Describe(self.params[2])
flist = desc.children
if self.params[3].valueAsText in [child.name for child in flist]:
self.params[3].setErrorMessage('Feature class name already exists')
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
desc = arcpy.Describe(self.params[2])
flist = desc.children
if self.params[3].valueAsText in [child.name for child in flist]:
self.params[3].setErrorMessage('Feature class name already exists')
return
I think it would error on 'x.name' since the string 'f' doesn't have a method of .name. Since .desc returns a string, doing [x for x in flist] iterates over the characters in the name. i.e, if the flist is 'fcname', you are checking x.name against 'f','c','n','a','m','e'.
flist = desc.name
if self.params[3].valueAsText in [x.name for x in flist]:
try this:
if self.params[3].valueAsText == flist:
Thank you, yeah the problem was with x.name for x, I changed the validation to:
desc = arcpy.Describe(self.params[2])
flist = desc.children
if self.params[3].valueAsText in [child.name for child in flist]:
self.params[3].setErrorMessage('Feature class name already exists')
return
and it worked 🙂
Solved it! The validation should be as followed:
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 parameter
has been changed."""
desc = arcpy.Describe(self.params[2])
flist = desc.children
if self.params[3].valueAsText in [child.name for child in flist]:
self.params[3].setErrorMessage('Feature class name already exists')
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
desc = arcpy.Describe(self.params[2])
flist = desc.children
if self.params[3].valueAsText in [child.name for child in flist]:
self.params[3].setErrorMessage('Feature class name already exists')
return