2

I am developing a Python tool (my first one). The tool has 3 input parameters:

  1. a raster
  2. z min
  3. z max

I am trying to get automatically z min and z max after the user specify the raster using the validation script but I got this error:

TypeError: expected a raster or layer

This is my code (commented lines are other attempts I tried without results):

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."""

if self.params[0].altered:
  if self.params[0].value:
    #rst_ds = self.params[0]
    rst_ds = self.params[0].value
    self.params[1].value = arcpy.Raster(rst_ds).minimum

    #self.params[2].value = arcpy.Raster(rst_ds.replace('\\','/')).maximum
    #arcpy.MakeRasterLayer_management(rst_ds, "rst_lyr")
    #self.params[1].value = arcpy.Raster("rst_lyr").minimum

return
2
  • Is this from a Python Script Tool in a standard toolbox (*.tbx) or from a tool in a Python Toolbox (*.pyt)?
    – PolyGeo
    Jan 14, 2021 at 9:00
  • It's a Python Script Tool in a standard toolbox Jan 14, 2021 at 9:10

1 Answer 1

1

You were almost there! Firstly ensure your parameters are their correct data type as shown below:

Parameters

The correct code in the validation function updateParameters would be:

  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."""
    if self.params[0].altered:
      if self.params[0].value:
        rst_ds = self.params[0].valueAsText
        ra = arcpy.Raster(rst_ds)
        self.params[1].value = ra.minimum
        self.params[2].value = ra.maximum
    return
1
  • 1
    It works! Thank you @Hornbydd Jan 15, 2021 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.