Python AddIns Blog

cancel
Showing results for 
Search instead for 
Did you mean: 

Latest Activity

(1 Posts)
New Contributor II

I have made a Python add in toolbar that has 3 combo boxes with one button. The add in allows for a user to choose a layer, choose a field from the selected layer, enter a keyword, and then when the search button is pressed, a query is applied and the extent of the data frame is zoomed to selected features. Before I go on, the tool works perfectly just as I designed it except for when the field type is 'String'. Then to make it work the user must enter the keyword in quotes. My goal is to not have the user worrying about having to type stuff in quotes because then the user would have to know what the field type is for when they are searching and that is not the case.

The query that is applied is:

[HYDRANT_NO] = 24

The snippet of code is:

# Apply search query

where_clause    = "[" + CHOICE_field + "] = " + CHOICE_keyword

arcpy.SelectLayerByAttribute_management(newLayer, "NEW_SELECTION", where_clause)

This works because the field.type of HYDRANT_NO is not 'String'. In the below example, the user selects the LastName field which is a 'String' field.type. The first figure returns no results. The second image returns the expected number of results.

1. Does Not work

2. Does work - The actual word in the table is JONES and is handled in the code on how to handle it which is why it works.

I have taken into account that I am using a personal geodatabase and that matters when writing the where_clause since it differs from ArcSDE and file geodatabases. The way that I have written the query WORKS when I do it manually in ArcMap whether it is typed into the Python window or using the Select Layer By Attribute Tool. Below is how I wrote how to check the keywords for string. It works manually but not with the addin. I have also attached the full add-in which will show the variables that are referenced. I feel that I am either missing something so small or it doesn't work because it can't work that way.

try:

    # Make layer from feature class

    arcpy.MakeFeatureLayer_management(featureClass, newLayer)

    # Apply search query

    where_clause    = "[" + CHOICE_field + "] = " + CHOICE_keyword

    arcpy.SelectLayerByAttribute_management(newLayer, "NEW_SELECTION", where_clause)

    # Get count of how many records met query

    layerCount      = arcpy.GetCount_management(newLayer)

    count           = int(layerCount.getOutput(0))

    if count != 0:

        # Zoom to layer

        df.zoomToSelectedFeatures()

    elif count == 0:

        #where_clause = '[' + CHOICE_field + "] = '" + str(CHOICE_keyword) + "'"

        where_clause = '[' + CHOICE_field + "] = '" + CHOICE_keyword_string + "'"

        arcpy.SelectLayerByAttribute_management(newLayer, "NEW_SELECTION", where_clause)

        layerCount   = arcpy.GetCount_management(newLayer)

        count        = int(layerCount.getOutput(0))

        if count != 0:

            df.zoomToSelectedFeatures()

        elif count == 0:

            where_clause = '[' + CHOICE_field + "] = '" + CHOICE_keyword_string.upper() + "'"

            arcpy.SelectLayerByAttribute_management(newLayer, "NEW_SELECTION", where_clause)

            layerCount   = arcpy.GetCount_management(newLayer)

            count        = int(layerCount.getOutput(0))

            if count != 0:

                df.zoomToSelectedFeatures()

            elif count == 0:

                where_clause = '[' + CHOICE_field + "] = '" + CHOICE_keyword_string.lower() + "'"

                arcpy.SelectLayerByAttribute_management(newLayer, "NEW_SELECTION", where_clause)

                layerCount   = arcpy.GetCount_management(newLayer)

                count        = int(layerCount.getOutput(0))

                if count != 0:

                    df.zoomToSelectedFeatures()

                elif count == 0:

                    where_clause = '[' + CHOICE_field + "] = '" + CHOICE_keyword_string.capitalize() + "'"

                    arcpy.SelectLayerByAttribute_management(newLayer, "NEW_SELECTION", where_clause)

                    layerCount   = arcpy.GetCount_management(newLayer)

                    count        = int(layerCount.getOutput(0))

                    if count != 0:

                        df.zoomToSelectedFeatures()

                    elif count == 0:

                        for lyr in arcpy.mapping.ListLayers(mxd, "", df):

                            if lyr.name == newLayer:

                                arcpy.mapping.RemoveLayer(df, lyr)

                        # Add a pop up if no results

                        pythonaddins.MessageBox("0 records matching query", "NNWW Toolbar")                           

except:

    for lyr in arcpy.mapping.ListLayers(mxd, "", df):

        if lyr.name == newLayer:

            arcpy.mapping.RemoveLayer(df, lyr)

    # Add a pop up if no results

    pythonaddins.MessageBox("0 records matching query", "NNWW Toolbar")               

more
0 10 1,483