User-Defined Definition Query

379
2
Jump to solution
01-09-2014 12:09 PM
JonathanBotts
New Contributor
I am hoping to allow a user to modify the definition query of a layer without actually typing it.

For instance, let's say I have a dataset of all of the stores in all the malls in Ohio. Each store feature has a StoreID and a MallID I would like to be able to have form (or something else) where a user can first select the MallID and then a second selection of StoreID. This would have the effect of setting the definition query so that only the 1 store displays for the user.

Is this possible without major programming efforts?

Thanks
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
I would do this using a gp script tool.  The tool would have two parameters: Select Mall ID and Select Store ID.  The script tool would also have a validation script that would control:  when the script tool is first opened, only the Select Mall ID parameter would display a list of mall ids in a parameter pulldown box.  Once you select a mall id, then the validation script would display on the Store IDs for that mall.

The script tool would then pass the two parameters to the calling python script that would automagically set a layer def query.

Here is a sample main python script that the script tool would call.

mallID = arcpy.GetParameterAsText(0)  #the first parameter in script tool storeID = arcpy.GetParameterAsText(1) #the second parameter in script tool  mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd, "My Mall Layer")[0] lyr.definitionQuery = '"MallID" = ' + str(mallID) + ' AND "StoreID" = ' str(storeID) arcpy.RefreshActiveView()


The validation script (a property of the script tool) would look something like:

def initializeParameters(self):   #THIS IS CALLED WHEN THE SCRIPT TOOL OPENS AND AUTO POPULATES THE FIRST PARAMETER     """Refine the properties of a tool's parameters.  This method is     called when the tool is opened."""      #Generate unique list of Mall IDs     import arcpy     mxd = arcpy.mapping.MapDocument("CURRENT")     mlyr = arcpy.mapping.ListLayers(mxd, "My Mall Layer")     fieldName = "MallID"     rows = arcpy.SearchCursor(mlyr.dataSource)     uniqueMallList = []     for row in rows:         if row.getValue(fieldName) not in uniqueMallList:             uniqueMall.append(row.getValue(fieldName))     uniqueMallList.sort()     self.params[0].filter.list = uniqueMallList     return  def updateParameters(self): #THIS IS CALLED WHEN THE FIRST PARAMETER IS SELECTED AND POPULATES 2ND PARAMETER     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""     import arcpy      if self.params[0].value:         #Generate unique list of Store IDs         slyr = arcpy.mapping.ListLayers(mxd, "My Store Layer")         fieldName = "StoreID"         rows = arcpy.SearchCursor(slyr.dataSource)         uniqueStoreList = []         for row in rows:             if row.getValue(fieldName) not in uniqueStoreList:                 uniqueStoreList.append(row.getValue(fieldName))         uniqueStoreList.sort()         self.params[1].filter.list = uniqueStoreList     return   


This code is untested and I'm sure has some silly typos but all the parts are here.  I hope this helps,
Jeff

View solution in original post

0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
I would do this using a gp script tool.  The tool would have two parameters: Select Mall ID and Select Store ID.  The script tool would also have a validation script that would control:  when the script tool is first opened, only the Select Mall ID parameter would display a list of mall ids in a parameter pulldown box.  Once you select a mall id, then the validation script would display on the Store IDs for that mall.

The script tool would then pass the two parameters to the calling python script that would automagically set a layer def query.

Here is a sample main python script that the script tool would call.

mallID = arcpy.GetParameterAsText(0)  #the first parameter in script tool storeID = arcpy.GetParameterAsText(1) #the second parameter in script tool  mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd, "My Mall Layer")[0] lyr.definitionQuery = '"MallID" = ' + str(mallID) + ' AND "StoreID" = ' str(storeID) arcpy.RefreshActiveView()


The validation script (a property of the script tool) would look something like:

def initializeParameters(self):   #THIS IS CALLED WHEN THE SCRIPT TOOL OPENS AND AUTO POPULATES THE FIRST PARAMETER     """Refine the properties of a tool's parameters.  This method is     called when the tool is opened."""      #Generate unique list of Mall IDs     import arcpy     mxd = arcpy.mapping.MapDocument("CURRENT")     mlyr = arcpy.mapping.ListLayers(mxd, "My Mall Layer")     fieldName = "MallID"     rows = arcpy.SearchCursor(mlyr.dataSource)     uniqueMallList = []     for row in rows:         if row.getValue(fieldName) not in uniqueMallList:             uniqueMall.append(row.getValue(fieldName))     uniqueMallList.sort()     self.params[0].filter.list = uniqueMallList     return  def updateParameters(self): #THIS IS CALLED WHEN THE FIRST PARAMETER IS SELECTED AND POPULATES 2ND PARAMETER     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""     import arcpy      if self.params[0].value:         #Generate unique list of Store IDs         slyr = arcpy.mapping.ListLayers(mxd, "My Store Layer")         fieldName = "StoreID"         rows = arcpy.SearchCursor(slyr.dataSource)         uniqueStoreList = []         for row in rows:             if row.getValue(fieldName) not in uniqueStoreList:                 uniqueStoreList.append(row.getValue(fieldName))         uniqueStoreList.sort()         self.params[1].filter.list = uniqueStoreList     return   


This code is untested and I'm sure has some silly typos but all the parts are here.  I hope this helps,
Jeff
0 Kudos
JonathanBotts
New Contributor
Jeff,

Thanks! With a little modification, the script works really well. After testing the drop-downs, I decided to allow the user to type in the MallID, so I ended up just taking out the Initialize Parameters, but it functioned just as I hoped.

Thanks again!

Jonathan
0 Kudos