Custom Search tool with Python Add-Ins

2831
3
05-27-2014 01:01 PM
PaulScipione1
New Contributor III
I have a search toolbar created in a different "GIS" software which runs via straight SQL. I am re-creating this tool in ArcGIS Desktop 10.2 with Python Add-Ins. The toolbar contains a toolpalette with 14 different buttons representing different layers to search.  The toolbar also has 2 comboboxes. 8 of these layers only need a single combobox to search. The other 6 layers require the use of 2 comboxes where the selection in combobox1 determines what's in the pick list in combobox2 and the 2 selections form the query. Most of these are dual combobox querys perform on several fields for value in combobox1 and several fields in combobox2.  I have the 8 layers working fine with the single combobox searches. Right now I am working on the Handhole (HH) search.  It populates the first combobox with a list of values from 3 different fields. Once I select that, its supposed to populate the second combobox with only those values in any of another 3 fields that have the first combobox value in any one of the first 3 fields. To make things simpler, here is the SQL from my other "GIS" software that's used for the Hanhole search:

SET_VARIABLE vROUTE {route: = select Route1 from Handholes where Route1 is not null union select Route2 from Handholes where Route2 is not null union select Route3 from Handholes where Route3 is not null}

SET_VARIABLE vHANDHOLE {handhole: = select HandholeID1 from Handholes where HandholeID1 is not null and Route1 = '{vROUTE}' union select HandholeID2 from Handholes where HandholeID2 is not null and Route2 = '{vROUTE}' union select HandholeID3 from Handholes where HandholeID3 is not null and Route3 = '{vROUTE}'}

FIND_SQL select * from Handholes where (Route1 = '{vROUTE}' and HandholeID1 = '{vHANDHOLE}') or (Route2 = '{vROUTE}' and HandholeID2 = '{vHANDHOLE}') or (Route3 = '{vROUTE}' and HandholeID3 = '{vHANDHOLE}')


Now here is the big code block of my Python Add-In.  I am focusing on the Handholes now.  I've eliminated the code for the tools that haven't been worked on yet because I am running out of space for this post:

import os, pythonaddins, sys, arcpy
from arcpy import env

env.workspace = r"Database Connections\prod_GilaRiver.sde"
srchConn = env.workspace + "\GilaRiver.dbo."
mxd = arcpy.mapping.MapDocument("CURRENT")

def ClrLists():
    cboGotoList2.items = []
    cboGotoList2.value = ""
    cboGotoList2.refresh()
    cboGotoList2.enabled = False
    cboGotoList1.items = []
    cboGotoList1.value = ""
    cboGotoList1.refresh()

def PopulateCombobox1(layer,fields):
    global srchLyr
    global srchFlds1
    srchLyr = layer
    srchFlds1 = fields
    print "*** def PopulateCombobox1"
    print "srchLyr = " + srchLyr + ", srchFlds1 = " + str(srchFlds1)
    ClrLists()
    values = []
    for field in srchFlds1:
        print "field: " + field
        values = values + [row[0] for row in arcpy.da.SearchCursor(srchConn + srchLyr, (field))]
    uniqueValues = sorted(set(values))
    for uniqueValue in uniqueValues:
        if uniqueValue is not None and uniqueValue != "":
            cboGotoList1.items.append(uniqueValue)
    cboGotoList1.enabled = True
 
def PopulateCombobox2():
    print "*** def PoplateCombobox2"
    print "srchLyr = " + srchLyr + ", srchFlds2 = " + str(srchFlds2)
    cboGotoList2.items = []
    cboGotoList2.value = ""
    cboGotoList2.refresh()
    values = []
    for field in srchFlds2:
        print "field: " + field
        values = values + [row[0] for row in arcpy.da.SearchCursor(srchConn + srchLyr, (field))]
    uniqueValues = sorted(set(values))
    for uniqueValue in uniqueValues:
        if uniqueValue is not None and uniqueValue != "":
            cboGotoList2.items.append(uniqueValue)
    cboGotoList2.enabled = True
 
def ZoomToFeature():
    print "*** def ZoomToFeature"
    print "srchVal1: " + str(srchVal1) + ", srchFlds1: " + str(srchFlds1) + ", srchFlds2: " + str(srchFlds2)
    df = arcpy.mapping.ListDataFrames(mxd, mxd.activeDataFrame.name)[0]
    dfLyr = arcpy.mapping.ListLayers(mxd, srchLyr, df)[0]
    expr1 = ""
    print "len(srchFlds1): " + str(len(srchFlds1))
    for idx, field in enumerate(srchFlds1):
        expr1 = expr1 + str(field) + " = '" + str(srchVal1) + "'"
        print "expr1(initial): " + expr1
        print "idx: " + str(idx)
        if len(srchFlds1) > 1 and idx < (len(srchFlds1) - 1):
            expr1 = " or " + expr1
        print "expr1(final): " + expr1
    if len(srchFlds2) == 0:
        arcpy.SelectLayerByAttribute_management(srchLyr,"NEW_SELECTION",expr1)
    else:
        arcpy.SelectLayerByAttribute_management(srchLyr,"NEW_SELECTION",str(srchFlds1) + " = '" + str(srchVal1) + "' and " + str(srchFlds2) + " = '" + str(srchVal2) + "'")
    df.extent = dfLyr.getSelectedExtent(False)
    if srchLyr == "zEXCHANGE":
        df.scale = df.scale * 1.05
    else:
        df.scale = 600
    arcpy.SelectLayerByAttribute_management(srchLyr,"CLEAR_SELECTION")
  
class GotoSubscriberE911(object):
    # implementation for GRTISearch_addin.btnGotoSubscriberE911 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        global srchFlds2
        srchFlds2 = []
        if arcpy.Exists("zSUBSCRIBER") == True:
            PopulateCombobox1("zSUBSCRIBER",["E911ID"])
        else:
            pythonaddins.MessageBox("Please add layer to use tool", "Missing Layer")

class GotoSubscriberAddress(object):
    # implementation for GRTISearch_addin.btnGotoSubscriberAddress (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        global srchFlds2
        srchFlds2 = []
        if arcpy.Exists("zSUBSCRIBER") == True:
            PopulateCombobox1("zSUBSCRIBER",["Address"])
        else:
            pythonaddins.MessageBox("Please add layer to use tool", "Missing Layer")
   
class GotoCentralOffice(object):
    # implementation for GRTISearch_addin.btnGotoCentralOffice (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        global srchFlds2
        srchFlds2 = []
        if arcpy.Exists("zCENTRALOFFICE") == True:
            PopulateCombobox1("zCENTRALOFFICE",["Name"])
        else:
            pythonaddins.MessageBox("Please add layer to use tool", "Missing Layer")

class GotoDLC(object):
    # implementation for GRTISearch_addin.btnGotoDLC (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        global srchFlds2
        srchFlds2 = []
        if arcpy.Exists("zDLC") == True:
            PopulateCombobox1("zDLC",["Name"])
        else:
            pythonaddins.MessageBox("Please add layer to use tool", "Missing Layer")

class GotoExchange(object):
    # implementation for GRTISearch_addin.btnGotoExchange (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        global srchFlds2
        srchFlds2 = []
        if arcpy.Exists("zEXCHANGE") == True:
            PopulateCombobox1("zEXCHANGE",["Exchange"])
        else:
            pythonaddins.MessageBox("Please add layer to use tool", "Missing Layer")

class GotoHandhole(object):
    # implementation for GRTISearch_addin.btnGotoHandhole (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        global srchFlds2
        srchFlds2 = ["HandholeID1","HandholeID2","HandholeID3"]
        if arcpy.Exists("zHANDHOLE") == True:
            PopulateCombobox1("zHANDHOLE",["Route1","Route2","Route3"])
        else:
            pythonaddins.MessageBox("Please add layer to use tool", "Missing Layer")

class GotoList1(object):
    # implementation for GRTISearch_addin.cboGotoList1 (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = False
        self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
        self.width = 'WWWWWWWW'
    def onSelChange(self, selection):
        global srchVal1
        srchVal1 = selection
        print "*** class cboGotoList1"
        print "srchVal1: " + str(srchVal1) + ", srchFlds1: " + str(srchFlds1) + ", srchFlds2: " + str(srchFlds2)
        if len(srchFlds2) == 0:
            ZoomToFeature()
        else:
            PopulateCombobox2()
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        global srchVal1
        srchVal1 = cboGotoList1.value
        print "*** class cboGotoList1"
        print "srchVal1: " + str(srchVal1) + ", srchFlds1: " + str(srchFlds1) + ", srchFlds2: " + str(srchFlds2)
        if srchVal1 != "None":
            if len(srchFlds2) == 0:
                ZoomToFeature()
            else:
                PopulateCombobox2()
    def refresh(self):
        pass

class GotoList2(object):
    # implementation for GRTISearch_addin.cboGotoList2 (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = False
        self.dropdownWidth = 'WWWWWWWWWWWWWWWW'
        self.width = 'WWWWWWWW'
    def onSelChange(self, selection):
        srchVal2 = selection
        print "*** class cboGotoList2"
        print "srchVal2: " + str(srchVal2) + ", srchFlds2: " + srchFlds2
        ZoomToFeature()
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass
0 Kudos
3 Replies
PaulScipione1
New Contributor III

I finally figured out the solution.  Got it to work.  I have layers that can be searched by a value in one field, and others by two fields, with the second value being filtered by what's selected in the first one.  Each time a layer is selected to query the pick lists populate/de-populate and enable/disable as needed.  But I haven't figured out how to share my Python add-in here.  Is there a different place than GeoNet to share code?

0 Kudos
DanPatterson_Retired
MVP Emeritus

ArcScripts 2.0 (as it is currently being called) will surface sometime in the near-ish future, in the interim GitHub seems to be the place

0 Kudos
PaulScipione1
New Contributor III

I couldn't figure out how to add the solution files here so I created a new post here Python add-in Custom Search toolbar

0 Kudos