Use Model Builder to set Definition Query on two layers, loop through list

3047
3
12-07-2012 05:32 AM
deleted-user-zGZcJ5TpRUeA
New Contributor III
I am trying to build a model that will apply a definition query to two layers, run a tool, and then reset the deinition query with my next variable until it reaches the end of my list. Any ideas?
0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor
I am trying to build a model that will apply a definition query to two layers, run a tool, and then reset the deinition query with my next variable until it reaches the end of my list. Any ideas?


I don't think you need to set a definition query - most tools honor the selected set of a layer and only process selected features.

Passing the inputs through the Select Layer by Attribute tool before you run the tool can probably do what you want. (If the inputs are feature classes, not layers, you will have to create layers using Make Feature Layer.)

You also may be able to use the iteration tools (for example, Iterate Features) to generate the values needed for your query strings.
0 Kudos
MatthewReynolds
New Contributor
Were you able to build your model to create the Freature Classes based and a Query and iterate to the end of the list?  I'm trying to do the same thing: Create 10 seperate Feature Classes based on 10 seperate Values (Strings) in a Column.  I'm really interested to see how this model is set up....My model is running the iteration, creating 10 seperate feature classes but the feature classes dont have the query applied; all the same.

Thanks!
0 Kudos
MichaelStead
Occasional Contributor III
I had issues intergating selections into my custom tools and for my purposes had to do it in Python. There is likely other ways to accomplish this. I stole this from a script that was referred to in a similair thread. The "%" symbols in this little piece of code do some sort of subtitution that gets around the problem with escaping all the quotes you think you would need by predefining the query.

while counti <= done:
    qry = "%s = %s" % (delimitedfield, counti)
    arcpy.SelectLayerByAttribute_management("L_LYR", "NEW_SELECTION",qry)
    arcpy.CopyFeatures_management("L_LYR", fcbase +str(counti))
    counti = counti +1


Also the query may be on a GDB, MDB, Shapefile, SDE..... so to get around syntax differences for field names try adding something like this at the beginning of your script and instead of using the field use the variable.

delimitedfield = arcpy.AddFieldDelimiters(LayeredLayer, 'Rank')
0 Kudos