Select to view content in your preferred language

Get FieldName from Field Alias

739
2
Jump to solution
02-24-2014 12:58 PM
JohnDye
Deactivated User
Perhaps I'm thinking a little too hard about this but I can't really figure out if there is a way to determine which field a particular field alias resolves to.

My situation is that I've got a series of python add-in comboboxes. The first allows a user to select a layer, the next a field, the next specify a search value.

For the second combobox, it autopopulates on focus with the aliasName of each field returned from FieldList. The question is, once a user makes a selection from this field, how do I resolve that back to the actual field?

class MAPSearch_item(object):     """Implementation for MAPSearch.item (ComboBox)"""     def __init__(self):         self.items = ["Stores", "Customers"]         self.editable = False         self.enabled = true         self.dropdownWidth = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'         self.width = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'     def onSelChange(self, selection):         global SearchItem         SearchItem = selection         global SearchLayer         if selection == "Stores":             try:                 SearchLayer = arcpy.mapping.Layer(r"C:applicationDeployment\\" + \                                                    "MAPS 1.5\Layers\Stores.lyr")             except Exception as e:                 pythonaddins.MessageBox("\                 An unexpected error occured. Please contact the GIS Manager with the \                 following error message: \                 " + e.message, "ERROR", 0)                 return         elif selection == "Customers":             try:                 SearchLayer = arcpy.mapping.Layer(r"C:applicationDeployment\\" + \                                                    "MAPS 1.5\Layers\Customers.lyr")             except Exception as e:                 pythonaddins.MessageBox("\                 An unexpected error occured. Please contact the GIS Manager with the \                 following error message: \                 " + e.message, "ERROR", 0)                 return         else:             pythonaddins.MessageBox("Item is not a valid selection.", "ERROR", 0)             return     def onEditChange(self, text):         pass     def onFocus(self, focused):         pass     def onEnter(self):         pass     def refresh(self):         self.refresh()  class MAPSearch_field(object):     """Implementation for MAPSearch.field (ComboBox)"""     def __init__(self):         self.editable = False         self.enabled = True         self.dropdownWidth = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'         self.width = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'     def onSelChange(self, selection):         global SearchField         # The question is, once the user makes a selection, the selection is actually an aliasName for a field as          # specified in the onFocus function. So 'selection' returns an aliasName. How do I resolve the given aliasName         # back to the actual field in order to complete the below Query?         SearchField = selection         global SearchSet         try:             SearchSet = set([r[0] for r in arcpy.da.SearchCursor(SearchLayer,                                                                  SearchField)])         except Exception as e:             pythonaddins.MessageBox("\             An unexpected error occured. Please contact the GIS Manager with the \             following error message: \             " + e.message, "ERROR", 0)             return     def onEditChange(self, text):         pass     def onFocus(self, focused):         # When user gives focus to combobox, dynamically populate the combobox with the aliasName          # of every 'Text' field in the 'SearchLayer' established in the 'MAPSearch_item'         self.items = []         for Field in arcpy.ListFields(SearchLayer, "*", "TEXT"):             self.items.append(Field.aliasName)     def onEnter(self):         pass     def refresh(self):         self.refresh() 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
You can make a reference dictionary with the alias name as the key.

dict = {f.aliasName: f.name for f in arcpy.ListFields(layer)}

View solution in original post

0 Kudos
2 Replies
MathewCoyle
Honored Contributor
You can make a reference dictionary with the alias name as the key.

dict = {f.aliasName: f.name for f in arcpy.ListFields(layer)}
0 Kudos
JohnDye
Deactivated User
You can make a reference dictionary with the alias name as the key.

dict = {f.aliasName: f.name for f in arcpy.ListFields(layer)}


oooooooooo

I had actually just figured out an alternative solution by creating another list and iterating back through it and interrogating every aliasName to see if it matched the selection, but this is much more elegant! Thanks Matthew!
0 Kudos