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

Get FieldName from Field Alias

$
0
0
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?

Code:

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()


Viewing all articles
Browse latest Browse all 2485

Trending Articles