2

This question already has an answer here:

I have been tasked with making a tool, that when you open it you can choose from a list in a dropdown box, which then populates the second dropdown box with a sublist, like so:

Example

However I only have a basic understanding of Python and I barely understand GIS, so this is a giant learning experience for me. I have tried using ESRI's site and this community, but alas I am still horribly stuck.

Where it goes wrong is at "self.params1.filter.list =" I can make it work with if I give it a list like ["Option 1", "Option 2"], however i already have the list "mergedList" where I have the values I want, but I cannot for the life of me make it work.

I have tried with: self.params1.filter.list = mergedList

and

self.params1.filter.list = sorted(mergedList.keys()) and one other which eludes my brain right now.

Current code, it only has one of the params, since I can't get that to work I haven't done the other one.

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

    # Variables for Cursor
    featureClassName = r'ArcGIS_Drift_Grundkort_DB.GISGRUNDKORT.Jordstykke'
    cursorNavn = "ELAVSNAVN"
    cursorNr = "MATRNR"

    #Generate empty Lists
    listNavn = []
    listNr = []

    #Populate lists
    with arcpy.da.SearchCursor(featureClassName, (cursorNavn,)) as cursor:
        for row in cursor:
            listNavn.append(row)

    with arcpy.da.SearchCursor(featureClassName, (cursorNr,)) as cursor:
        for row in cursor:
            listNr.append(row)

    #Merge lists into a single list with keys
    mergedList = {}
    for i, v in enumerate(listNavn):
        mergedList[v] = mergedList.get(v, []) + [listNr[i]]

    #toolbox stuff
    self.params[1].filter.list = mergedList

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

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

It is very likely there is a better way of doing this, I just need the unique data from column A (ELAVSNAVN) which is like companies and then a sublist with the numbers (MATRNR) that goes with that.

The tool itself is incomplete since i am stuck on just the list part, just need some pointers / tips for that.

marked as duplicate by JGH, Antonio Falciano, MaryBeth, whyzar, tinlyx Dec 19 '17 at 19:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

You're pretty close to having it working. Your lists are formatted weird because of the use of dictionaries. The code below should get you what you need. After the initial list is set, you use the updateParameters section to populate the list for the second drop-down.

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

    # Variables for Cursor
    featureClassName = r'ArcGIS_Drift_Grundkort_DB.GISGRUNDKORT.Jordstykke'
    cursorNavn = "ELAVSNAVN"

    #Generate empty Lists
    listNavn = []
    #Populate lists
    listNavn = list(set(list(str(row.getValue(cursorNavn)) for row in arcpy.SearchCursor(featureClassName, fields=cursorNavn))))
    listNavn.sort()

    self.params[0].filter.list = listNavn
    self.params[0].value = listNavn[0]

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

    featureClassName = r'ArcGIS_Drift_Grundkort_DB.GISGRUNDKORT.Jordstykke'
    sNavn = str(self.params[0].value)
    cursorNavn = "ELAVSNAVN"
    cursorNr = "MATRNR"
    listNr = list(set(list(str(row.getValue(cursorNr)) for row in arcpy.SearchCursor(featureClassName, cursorNavn + "='" + sNavn + "'", fields=cursorNr))))
    listNr.sort()

    self.params[1].filter.list = listNr

    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

Make sure both parameters are setup to allow for Value Lists, as seen below:

enter image description here

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