Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to create a tool from a python script I have written that will take a list I have created and use it as a drop-down menu in the finished tool as one of the inputs (see attached image for example):

enter image description here

The list I am using is a large list that includes all the towns in the state of Vermont, and I generate it in the script from a table (see code below). I suspect my problem at the moment is just with setting the tool Properties to take this list and use it to create a drop-down list for the user. Here is the block of code that creates the list for use in the parameter - does anybody see any problems with this code-end of the tool?

import arcpy
arcpy.env.workspace = "Z:\\OPS\\TechnicalServices\\Culverts\\GetCulverts\\GetCulverts.gdb"
towns = "Database Connections\\GDB_GEN.sde\\GDB_Gen.VTRANS_ADMIN.townindex"
arcpy.MakeFeatureLayer_management(towns,"towns_lyr")

NameList = []
NameListArray = set()
rows = arcpy.SearchCursor("towns_lyr")
for row in rows:
    value = row.getValue("TOWNNAME")
if value not in NameListArray:
    NameList.append(value)
town = NameList

town = arcpy.GetParameterAsText(0)

Here is an image of the Tool properties as well, with the default validation code - do I need to alter this validation code?

I looked for info on altering this validation code, but I could not find info on using it for formatting drop-down lists.

enter image description here

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Try setting the tool validator class code to this:

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."""
    towns = "Database Connections\\GDB_GEN.sde\\GDB_Gen.VTRANS_ADMIN.townindex"
    rows = arcpy.SearchCursor(towns)
    self.params[0].filter.list = sorted(list(set(r.getValue('TOWNNAME') for r in rows)))
    del rows
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return
share|improve this answer
    
Excellent! Thanks! That worked perfect. The next task I have, which may be more complicated, is to make the next user input do the same thing, but have it based on the choice in the first input - I will post a follow up to this question with those details soon. –  GISdan Sep 15 '14 at 16:23
1  
GISdan if it worked for you then you should tick @crmackey answer as correct. It's just not cricket if you don't... :( –  Hornbydd Sep 15 '14 at 17:28
    
Sorry, I was working on a follow up to crmackey's reply, but haven't finished testing it yet. I have now ticked the answer as correct. Thanks for the nudge. –  GISdan Sep 15 '14 at 19:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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