Select to view content in your preferred language

Check Spatial Reference for Params in Tool Validation

903
3
04-11-2014 11:16 AM
NoahHuntington
Deactivated User
I am struggling trying to check the spatial reference in tool validation.  Can anybody provide a little direction.  You should get the gist of what I am trying to do with what I have here...
  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."""
    sr = arcpy.SpatialReference(2275)
    if arcpy.Describe(self.params[0]).spatialReference != sr:
        self.params[0].setErrorMessage("Coordinate System Must be SPC 4201")
Tags (2)
0 Kudos
3 Replies
WilliamCraft
MVP Alum
Are you getting an error?  If so, please provide a description of the error.  Also, what is the output if you include the following lines after setting the SR variable? 

print arcpy.Describe(self.params[0]).spatialReference
arcpy.AddMessage(arcpy.Describe(self.params[0]).spatialReference)
0 Kudos
FilipKrál
Frequent Contributor
Hi wtgeographer,
I am guessing your problem is that your condition on line 6 always evaluates to False.

Try it this way:
  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."""
    wkid = getattr(getattr(arcpy.Describe(self.params[0]), "spatialReference", None), "factoryCode", None)
    if wkid != 2275:
        self.params[0].setErrorMessage("Coordinate System Must be SPC 4201")


I think your condition is always False because you are comparing two objects that are in fact different (in different places in memory?), whereas comparison of integer numbers (like factory codes of spatial reference objects) should work as you would expect.

My line 5 basically says `wkid = arcpy.Describe(self.params[0]).spatialReference.factoryCode`, but my version won't fail if the attributes are not present, for whatever reason.

Hope this helps.
Filip.
0 Kudos
NoahHuntington
Deactivated User
These are all great approaches. Unfortunately I don't believe they work with what I already have in tool validation.  How can I apply the step of validating spatial reference while keeping existing validation in update parameters section?

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()
    self.fcfield = (None, None)

  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 and self.params[1].value:
      fc, col = str(self.params[0].value), str(self.params[1].value)
      if self.fcfield != (fc, col):
        self.fcfield = (fc, col)

        # Get the unique values of the field 'col' in the feature class 'fc'
        unique_values = [str(val) for val in
                            sorted(
                                set(
                                    row.getValue(col)
                                        for row in arcpy.SearchCursor(fc, None, None, col)
                                )
                            )
                        ]

        # Assign the unique_values list to parameters 2 and 3
        self.params[2].filter.list = unique_values
        self.params[3].filter.list = unique_values

    # Set the default values of parameters 2 and 3 to the first item in the list
    if self.params[2].value not in self.params[2].filter.list:
      self.params[2].value = self.params[2].filter.list[0]
    if self.params[3].value not in self.params[3].filter.list:
      if self.params[3].filter.list[0] != self.params[2].value:
        self.params[3].value = self.params[3].filter.list[0]
      else:
        self.params[3].value = self.params[3].filter.list[.1]

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    if self.params[2].value == self.params[3].value:
        self.params[3].setErrorMessage("Values cannot be the same")

0 Kudos