Selection to Definition Query

11-15-2013 07:46 AM
Status: Open
Labels (1)
Occasional Contributor

Add a tool (w/ gui preferably) to get the attribute values for a set of selected features and use them in an interactive definition query dialog box which would allow the user to select which attributes to use for the querry and how to use them.  If this is set up in gui it could also be used to visually analyse for trends within the selected data.



I created an Add-in that supports the following scenario:

1) User: selects a layer in Table of Contents and makes a selection
2) User: clicks on a button in the Ribbon bar (Table ->View)
3) Tool: creates a new Layer Definition based on current selection (from selected layer) based on ObjectID field under a specific name 'YYYY-MM-DD HH:MM:SS'. New Layer Definition is not active.

Would that do the job?

Looking for your feedback.



#### Edit:

If anyone is interested in, here is a link to the Add-in (ArcGIS Pro 2.8).





When creating maps, I often find a situation where I need to display a few polygons on a map. Instead of performing a definition query, or selecting the features and exporting, it would be nice to have an option from right clicking a layer to "Display only selected features", instead of having to make a new layer.

For example, a cartographer only needs to show a grouping of counties. They could draw a polygon to select counties and export to a new shp/feature layer. Or They could enter the county names in a def query, only to realize counties with like names from other states are still showing right across the state boarder. This gets trickier by having to add more expressions/expression sets to exclude the state, but maybe not one county in the grouping that may also share the other state...  If the cartographer could choose to "Display only selected features" in the map it would alleviate some additional workflows. They could export a map without creating new layers or making a complex def query. 

An additional but similar feature, it would also be nice to have the option to filer based on map extent like in Portal or AGOL.


I would also want the option to create the definition query using my own unique ID field like ASSET_ID, not just the OBJECTID. My unique ID field is more meaningful to me than the OBJECTID and isn’t at risk of changing due to edits.


For what it’s worth, it's possible to do it with PythonCreating definition query from selected features using ArcGIS Pro

The script creates a definition query from the selection. It uses the first layer in the Contents pane that is called MY_USER.ROADS.

def qdef_selected_features(lyr):
    desc = arcpy.Describe(lyr)
    # Get a semicolon-delimited string of selected feature IDs 
    fid_list = desc.FIDSet.split(";")
    # build the query definition
    query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
    # apply the query definition back to the layer   
    lyr.definitionQuery = query

aprx ='current') 
m = aprx.activeMap 
lyr = m.listLayers('MY_USER.ROADS')[0] 

But I think we’re all saying we’d rather have an OOTB tool.




I often use attribute queries when exploring data to figure out what's what: what features of this dataset do I want, and which ones don't I want? This is a really useful method when looking at the map and if I have an idea about what the fields are. The very next thing I do is go create a definition query that's either the same, inverse of, or similar to my selection query. To save some steps along the way, a button in the select by attributes window for 'convert to definition query' would be really helpful. I picture a prompt that asks for a definition query name, and maybe display the expression editor again in case the one you want to save is slightly different than the one you're using for your attribute query. Hit OK and the definition query is applied!


Hi @KoryKramer  this actually does not seem to be the same idea. This idea, also good, seems to be talking about having the software convert selections into queries, not user-defined queries themselves within the select by attribute window. Thanks.


@rburke The workflow you described is creating a selection using an attribute query, supposedly using Select by Attributes? So you end up with a selection in the data exploration process. 

"The very next thing I do is go create a definition query that's either the same, inverse of, or similar to my selection query." 

Are you saying that this present idea of converting a selection to a definition query would not satisfy what you're looking for?

i.e. you just want a quick way of taking a query that you've constructed using Select by Attributes, and without making a selection, just take the query and push it into a definition query?


The comments through this idea have to do both with interactive selections (no attribute query) as well as selections made using attribute queries. The latter, which is what I understand you to be requesting, should be easier and more robust as I understand it from the development side, whereas the former (an interactive selection not based on a structured query) would have more challenges. Either way, we'd be considering both when looking at this idea. 

I can check back in with the dev team on this, but in the meantime, please provide further clarification about why you feel your new idea is distinct from this existing idea. 

Thank you


@KoryKramer  yes -- looking for a quick way to take a query I made there, perhaps modify it a little, and use it as a definition query, regardless of what's selected. E.g. -- I take road data, and do a couple select bys on a road type field to see which values are driveways and alleys. I might want those excluded, so saving the expression as a definition query with the opportunity to change "=" to "<>" would be helpful, without copying & pasting the sql and heading to the layer properties > definition query tab to set it up. The way you're saying might be understandably trickier technically -- to have the software just invent a query (or something like it) from a selection -- probably would not suffice for reasons you probably are thinking of, too -- what field would that even use? would it be smart enough to find one? would it add a field and populate it with something shared? how would it handle new data? would it just use ObjectID? etc etc -- maybe the solution to some of that problem is just making selection layers more like definition queries where you can have multiple & turn them on and off. So for those reasons I think these are very different ideas but if you are considering them together then do what you gotta do, and thanks.