Select to view content in your preferred language

Filter list of values - Choose Description but code sent to database

2086
15
Jump to solution
07-02-2013 06:02 AM
ionarawilson1
Deactivated User
I am a newbie to Python but eager to learn. I need to create tools in ArcMap to enter and edit data. Users will need to enter for example the name of the office they work, so I created domains for that. So let's say the user completed a task in Alpine, then he would enter Alpine in the drop-down box. So for each office there is a domain. For example:

Code   Description

AL       Alpine
AU      Austin
CA      Carthage


And so on...

The problem is that I need the user to see the description in the dropdown box but the code to be sent to the database when he chooses an office name. So for example he would choose Austin, but the database would record AU in the office field.

The way it is now, when I create the parameters using the out-of-the-box properties of the script, I have to use the Filter and enter a value list. However,  if I use the description value in the list of values (which is want I want) , this description will be recorded in the table. For example if the user chooses Austin, Austin would be recorded in the database, instead of AU. Is there a way to change this? I was reading about tool validators but I have no idea how to use it. Thank you for any help you can provide!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
StacyRendall1
Frequent Contributor
Ionara, I will elaborate on mzcoyles solution, as it is the right one for this situation.

A Python dictionary is a way of storing information under 'keys'. A dictionary will return a value when supplied the correct key. For example, a dictionary of phone numbers (name is key, phone number is value):
>>> phoneBook = {'Bob': 4566461, 'Jane': 8954556}


The dictionary is constructed by wrapping it in curly braces. The names are strings, so they have quotes around them, while the numbers are stored as numbers. The : indicates that the value corresponds to the key.

If I query the phoneBook dictionary with the key for Bob, his name, it will return his phone number:
>>> phoneBook['Bob'] 4566461


So, in your case, something like this should work:
 # userValue represents what the user selected, i.e. userValue = 'Alpine'  officeCodeDict = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA'}  # databaseValue is what you send to the database databaseValue = officeCodeDict[userValue]  # so, if userValue is Alpine, databaseValue is AL


Hope this helps.

View solution in original post

0 Kudos
15 Replies
MathewCoyle
Honored Contributor
Just use a dictionary with the domain value/description so you can reference one with the other.
0 Kudos
ionarawilson1
Deactivated User
Thank you Matthew, but can you be a little more specific? Do you have any similar examples? thanks!
0 Kudos
RhettZufelt
MVP Notable Contributor
Ionara,

Really don't make tools, so not sure what an editing tool would do.  Since it will be running in ArcMap, if there is a way to open the Attribute window (from editor) (perhaps a template mxd with your tool), then they would be able to pick all the domain values directly from the dropdown in the attribute window itself.

Would be active for either selected features, or newly created features.

not sure if this helps at all, just thougth I'd point it out if you weren't already familiar with it.

R_
0 Kudos
ionarawilson1
Deactivated User
rzufelt,

I know we could use that, but we want to make sure the users will be entering all the records. Creating a tool insures that because the script does not run if they don't choose all the parameters but an user can leave a record blank when editing the feature in the table of attributes. At least I haven't found a good way to prevent that. Thanks
0 Kudos
RhettZufelt
MVP Notable Contributor
Only way I can think of would be when you create the fields, set it to not allow null values.  This way, "something" would have to be entered, but could be anything that meets the field type, would not be restricted to your list (except, of course, for the fields that have a domain on them).

R_
0 Kudos
StacyRendall1
Frequent Contributor
Ionara, I will elaborate on mzcoyles solution, as it is the right one for this situation.

A Python dictionary is a way of storing information under 'keys'. A dictionary will return a value when supplied the correct key. For example, a dictionary of phone numbers (name is key, phone number is value):
>>> phoneBook = {'Bob': 4566461, 'Jane': 8954556}


The dictionary is constructed by wrapping it in curly braces. The names are strings, so they have quotes around them, while the numbers are stored as numbers. The : indicates that the value corresponds to the key.

If I query the phoneBook dictionary with the key for Bob, his name, it will return his phone number:
>>> phoneBook['Bob'] 4566461


So, in your case, something like this should work:
 # userValue represents what the user selected, i.e. userValue = 'Alpine'  officeCodeDict = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA'}  # databaseValue is what you send to the database databaseValue = officeCodeDict[userValue]  # so, if userValue is Alpine, databaseValue is AL


Hope this helps.
0 Kudos
RhettZufelt
MVP Notable Contributor
and to check if the key exists then do something, I use this:

         if (officeCodeDict.has_key(userValue)):
             myVal= siteDict[userValue]
         else:
             print "not an acceptable value"

R_
0 Kudos
ionarawilson1
Deactivated User
Thank you Stacy and Rhett. You guys are awesome!!!:o





#Sets parameters (attributes)
    Office =  gp.GetParameterAsText(0)

    Forester = gp.GetParameterAsText(1)
    Status =  gp.GetParameterAsText(2)

    officeDomain = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA', 'Corpus Christi': 'CC', 'Conroe': 'CO', 'Crockett': 'CR',
                      'Crockett':'CR', 'College Station': 'CS', 'Canyon': 'CY', 'Dallas': 'DA', 'El Paso': 'EP', 'Fort Worth': 'FW',
                      'Gilmer': 'GI', 'Granbury': 'GR', 'Hamilton': 'HA', 'Henderson': 'HE'}
    officeCode= officeDomain[Office]
    foresterDomain = {'Brittany Compton': 'bcompton', 'Brian Pope': 'bpope', 'Buster Robinson': 'brobinson',
                        'Clay Bales': 'cbales', 'Daniel Duncum': 'dduncum', 'Daniel Lewis': 'dlewis'}
    foresterCode = foresterDomain[Forester]



#Create Update Cursor
    rows = arcpy.UpdateCursor("Stewardship")

    #Update Forester
    for row in rows:


        if (officeDomain.has_key(Office)):
            row.Office = officeCode
            rows.updateRow(row)

0 Kudos
ionarawilson1
Deactivated User
Also, one last question: Do I have to create an update cursor fo every field? I tried having just one update cursor for all fields and it works but does that cause data locks? Should I create one cursor for each cursor and keep deleting the rows so as to remove data locks? Thank you!!!
0 Kudos