Select to view content in your preferred language

Selecting multiple subsets

2439
9
Jump to solution
09-18-2012 10:34 AM
NoahHuntington
Deactivated User
[ATTACH=CONFIG]17770[/ATTACH]
Zooming to full extent when optional parameter values  are not entered.  Can anybody tell me why this is?

Thanks in advance...

import arcpy  def main():     arcpy.AddMessage("Starting")      # Get and set variables     sub = arcpy.GetParameterAsText(0)     unit = arcpy.GetParameterAsText(1)     blk = arcpy.GetParameterAsText(2)     lot = arcpy.GetParameterAsText(3)     layer_name = "tax_acct"     df_name = "Layers"       arcpy.AddMessage(sub)     arcpy.AddMessage(unit)     arcpy.AddMessage(blk)     arcpy.AddMessage(lot)          # Define mxd, df and lyr objects     mxd = arcpy.mapping.MapDocument("CURRENT")     df = arcpy.mapping.ListDataFrames(mxd, df_name)[0]     lyr = arcpy.mapping.ListLayers(mxd, layer_name, df)[0]      arcpy.AddMessage(lyr.name)      # Create where clause for selection     query = "SUBDIVISIO = '%s'" % (sub)     query1 = "UNIT_NUMBE = '%s'" % (unit)     query2 = "BLOCK_NUMB = '%s'" % (blk)     query3 = "LOT_NUMBER = '%s'" % (lot)      arcpy.AddMessage(query)     arcpy.AddMessage(query1)     arcpy.AddMessage(query2)     arcpy.AddMessage(query3)                    arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query)     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query1)     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query2)     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query3)          # Set df extent to layer selection     df.extent = lyr.getSelectedExtent()     arcpy.RefreshActiveView()      arcpy.AddMessage("Completed")   main()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChrisSnyder
Honored Contributor
Okay I get what you are doing... Here's what's wrong:

If one of your query parameters is left blank, then you need to not execute that query. Blank parametrs are pased back to the script as a blank string. So for example if param3 and param4 are blank:

param1 = "Dog"
param2 = "3"
param3 = ""
param4 = ""

Then in your query, when the query for param3 (a subset) is executed, basically you are saying "take my current selection, and from thoss records select the records where "MYFIELD = ''". Which correctly returns 0 records, becasue you don't have any subset records with '' (blank) values.

What you need to do is if any of the query parameters is blank (i.e. ''), then don't execute that particular query. The logic of this may be a bit longwinded.

something like:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query) if unit != "":     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query1) if blk != "":     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query2) if lot != "":     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query3)

View solution in original post

0 Kudos
9 Replies
ChrisSnyder
Honored Contributor
If nothing was selcted, what would you want the tool to do?

Perhaps you could have something check if any records/features were selected?

Like GetCount_mangement()
0 Kudos
NoahHuntington
Deactivated User
Its true.  I definitely need to add some error handling to the script.  I am just confused b/c I am sure there are records for SLEEPY HOLLOW and unit 4 (the default values)? Infact when all parameters are filled it works fine.  What do you think?
0 Kudos
ChrisSnyder
Honored Contributor
I guess my suggestion was to just make sure the queries are being executed correctly...

For example, insert something like this after the SelectlayerByAttribute tools run:

arcpy.AddMessage("Query selected " + str(arcpy.GetCount_management(lyr).getoutput(0) + " records...")
0 Kudos
NoahHuntington
Deactivated User
[ATTACH=CONFIG]17781[/ATTACH][ATTACH=CONFIG]17782[/ATTACH]

Successful in first run...Second returned no selections when the first two parameters clearly exist?
0 Kudos
NoahHuntington
Deactivated User
Successful in first run...Second returned no selections when the first two parameters clearly exist?

[ATTACH=CONFIG]17784[/ATTACH][ATTACH=CONFIG]17785[/ATTACH][ATTACH=CONFIG]17786[/ATTACH][ATTACH=CONFIG]17787[/ATTACH]
0 Kudos
ChrisSnyder
Honored Contributor
Okay I get what you are doing... Here's what's wrong:

If one of your query parameters is left blank, then you need to not execute that query. Blank parametrs are pased back to the script as a blank string. So for example if param3 and param4 are blank:

param1 = "Dog"
param2 = "3"
param3 = ""
param4 = ""

Then in your query, when the query for param3 (a subset) is executed, basically you are saying "take my current selection, and from thoss records select the records where "MYFIELD = ''". Which correctly returns 0 records, becasue you don't have any subset records with '' (blank) values.

What you need to do is if any of the query parameters is blank (i.e. ''), then don't execute that particular query. The logic of this may be a bit longwinded.

something like:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query) if unit != "":     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query1) if blk != "":     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query2) if lot != "":     arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", query3)
0 Kudos
NoahHuntington
Deactivated User
Works great now!!! Thanks Chris.
0 Kudos
NoahHuntington
Deactivated User
If alright I'd like to take this opportunity to pick your brain abit more?  If I wanted to swap parameter #1 (currently arcpy.GetParameterAsText(0)) with a combobox populated with unique attributes from a field.  What might that look like?
0 Kudos
ChrisSnyder
Honored Contributor
Combo Box:

1. The simplest way is to set the parameters "MultiValue" property to Yes, and then provide some optional parameters.... which involves manually typing them all in to the Toolbox GUI when you set up the script tool: http://resources.arcgis.com/en/help/main/10.1/index.html#/Setting_script_tool_parameters/00150000000...

2. Another way is to dynamically read the possible combo box field values from some existing table or some sort of script logic. This would be accomlished with a "tool validator class": http://resources.arcgis.com/en/help/main/10.1/index.html#/Customizing_script_tool_behavior/001500000...

3. Yet another way (new in v10.1) is a "Python Toolbox", which I think has a different flavor of a "tool validator": http://resources.arcgis.com/en/help/main/10.1/index.html#/Creating_a_new_Python_toolbox/001500000034.... I havn't messed with that one yet.
0 Kudos