Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Using Validator Class updateParameter Function to Change Parameter Datatype

$
0
0
I am building an ArcGIS gui that takes 4 parameters:
  1. An input feature class or shapefile
  2. A parameter to query on the input feature class or shapefile
  3. A predefinied multivalue list of layer names that the user can choose from
  4. An output folder location to print out an excel file

The tool will clip data from any of the layers choosen in Parameter #3 based on the input feature class or shapefile (clip feature) from Parameter #1

If the user chooses a feature class as an input, it is know that there are only 2 to choose from ("AREA1", "AREA2"). They will use Parameter 2 to Query a record from which ever feature class they choose. (eg "SITE_NUMBER" = 10023) Also, I know that these 2 feature classes contain the same list of fields, so I can code this into the underlying python script to write the names of particular ones I want to the resultant excel spreasheet.

If the user chooses a shapefile, it could be one of several hundred. Each shapefile only has one record (polygon) so I do not need them to query on a feature using an SQL Query. However, each shapefile does not have the same list of fields from one to the next. In this case, instead of having Parameter #2 of datatype "SQL Expression", I would like to dynamically change the datatype, using the updateParameter function in the Validator Class, to string and a multivalue filter of field names from the shapefile to which the user can choose from.

In the following script, I have attempted to set the some default values for Parameters #1 and 2. dayatype on Parameter #2 if the user changes the input value for Parameter #1. For the programmers out there, this obviously doesn't work as it is a read only parameter according to the help docs:

http://resources.arcgis.com/en/help/...0000000v000000

Code:

   
# Set Parameters #1nad 2 default values
if not self.params[0].altered:
      self.params[0].value = r"Database Connections\CONNECT1.sde\AREA1_SPATIAL.ER_SVW"
    if not self.params[1].altered:
      self.params[1].datatype = "SQL Expression"
      self.params[1].value = ""

    # if the user changes Parameter #1, then update the datatype and values
    if self.params[0].value == r"Database Connections\CONNECT1.sde\AREA1_SPATIAL.ER_SVW":
      self.params[1].datatype = "SQL Expression"
      self.params[1].value = "SITE_NUMBER = "
    elif self.params[0].value == r"Database Connections\CONNECT1.sde\AREA2_SPATIAL.ER_SVW":
      self.params[1].datatype = "SQL Expression"
      self.params[1].filter.list = "SITE_NUMBER = "
    elif self.params[0].value not in [r"Database Connections\CONNECT1.sde\AREA1_SPATIAL.ER_SVW", r"Database Connections\CONNECT1.sde\AREA2_SPATIAL.ER_SVW"]:
      self.params[1].datatype = "String"
      descFields = arcpy.Describe(self.params[0].value).fields
      self.params[1].filter.list = [field.name for field in descFields]
      self.params[1].filter.values = self.params[1].filter.list

There seems to be a way to set the parameter datatype on the Parameter object, but I'm not sure how to pass that from getParameterInfo function into the updateParameter function:

http://resources.arcgis.com/en/help/...00000035000000

I've been playing around with a bunch of approaches, but none seem to work. So, for what I tried to outline as my outcome above....is what I am trying to do possible?

Viewing all articles
Browse latest Browse all 2485

Trending Articles