BLOG
|
Can't say that the solution is pretty but since it kept throwing errors where I wanted the the if/else statements to take over, I used try/except inside of a try/except and it worked exactly how I want. Thanks for the assistance.
... View more
03-09-2016
07:29 AM
|
0
|
0
|
465
|
BLOG
|
The first where_clause is for the string and then if it is not a string with no results coming back, then I declare the where_clause variable back to what it was in the first place, and its not catching
... View more
03-08-2016
12:53 PM
|
0
|
0
|
465
|
BLOG
|
03-08-2016
12:51 PM
|
0
|
0
|
465
|
BLOG
|
try: # Make layer from feature class arcpy.MakeFeatureLayer_management(featureClass, newLayer) # Apply search query where_clause = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(featureClass, 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 = """{0} = {1}""".format(arcpy.AddFieldDelimiters(featureClass, CHOICE_field), int(CHOICE_keyword)) where_clause = "[" + CHOICE_field + "] = " + CHOICE_keyword 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) print '0 records found' # 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")
... View more
03-08-2016
12:49 PM
|
0
|
0
|
465
|
BLOG
|
So funny thing...I changed the code to the following and it will now work for strings, but only strings. I can't search for OBJECTIDs or any integers which is what worked in the first place.
... View more
03-08-2016
12:46 PM
|
0
|
0
|
465
|
BLOG
|
I will give it a shot and let you know if I can get it to work. Hopefully it is just as simple as this: where_clause = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(newLayer, CHOICE_field), CHOICE_keyword)
... View more
03-08-2016
11:42 AM
|
0
|
0
|
465
|
BLOG
|
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")
... View more
03-08-2016
08:16 AM
|
0
|
10
|
2110
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|