Select to view content in your preferred language

Adjusting the order of Definition Queries

1306
4
07-15-2020 12:58 PM
Status: Open
Labels (1)
ElenaS
by
New Contributor III

I would like the ability to change the order of definition queries. I created multiple definition queries in ArcGIS Pro but not in the order that I wanted the queries to appear in the dropdown menu. To have the queries in the right order, I would have to recreate them in the appropriate order. 

4 Comments
RandyWeaver

@ElenaS I too have the same problem, the only way I have gotten around it is to save each query expression to a file, delete all of the queries from the layer, rename the .exp files that you saved to reflect the order that you want, then use the "Add definition queries from files..." method (it is on the drop down for "New definition query"). When the dialog comes up to pick your file(s), you will already have them in order based on how you renamed them, so you pick the first file, hold down your shift key, and then choose the last file. Alternatively, you can also use your CTRL key to pick the order that you want, even if you didn't rename the .exported .exp files. This way you can keep the name of the query like you want it to show as in the list, and you can specify the order from how you pick them.

pkorst_coa

Agree with @ElenaS

💡Would like the functionality to manually sort Definition Queries (click, drag) or else alphabetize (by Query name) in the Data/Definition Query drop-down list. 

In addition, would like the functionality to AUTO-Zoom to Selection from drop-down list. For example, the list contains queries selecting City Parks by Name (alphabetically, hopefully) and when a particular City Park name is selected from the list, the map will automatically Zoom To Selection on that City Park layer. 

Thanks. 

 

 

 

ElenaS
by

@RandyWeaver: Oh, smart! I'll definitely use that next time.

@pkorst_coa: I agree an auto-zoom feature would be nice. Rather than Zoom to Selection though, Zoom to Layer might be more relevant to avoid an additional step (selection).

Also, in case it helps, I added the Zoom To Layer command into my Quick Access Toolbar to make it a little bit more accessible for now.

FranziskaSumpf

For anyone still looking for this (like I did an hour ago): I wrote a python snippet that will sort definition queries in alphabetical order by query name. It can be used in the python window in an ArcGIS Pro Project (Tested in 3.1.1).

Bild2.png

 

To sort definition queries, paste the below code into the python window and replace Name of Map and Name of Layer (or Name of Table) in lines 2 and 3 with the names of your map and the layer/table where you want to sort the queries (keep the quotation marks). Then press enter twice to run the code.

For a single layer:

aprx = arcpy.mp.ArcGISProject('CURRENT')
mp = aprx.listMaps('Name of Map')[0]
lyr = mp.listLayers('Name of Layer')[0]
if lyr.supports('DEFINITIONQUERY'):
	dq = lyr.listDefinitionQueries()
	#print(dq)
	sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
	#print(sorted_dq)
	lyr.updateDefinitionQueries(sorted_dq)

 

For a single standalone table:

aprx = arcpy.mp.ArcGISProject('CURRENT')
mp = aprx.listMaps('Name of Map')[0]
tbl = mp.listTables('Name of Table')[0]
dq = tbl.listDefinitionQueries()
#print(dq)
sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
#print(sorted_dq)
tbl.updateDefinitionQueries(sorted_dq)

If you have already executed the code for one layer or table and want to do another one in the same map, you don't need lines 1 and 2 (the variables are still saved in the system – but it won't break anything if you define them again).

 

You can also sort the definition queries in all layers and tables in the map:

aprx = arcpy.mp.ArcGISProject('CURRENT')
mp = aprx.listMaps('Name of Map')[0]
lyrs = mp.listLayers()
for lyr in lyrs:
	if lyr.supports('DEFINITIONQUERY'):
		dq = lyr.listDefinitionQueries()
		if len(dq)>1:
			sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
			lyr.updateDefinitionQueries(sorted_dq)
tbls = mp.listTables()
for tbl in tbls:
	dq = tbl.listDefinitionQueries()
	if len(dq)>1:
		sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
		tbl.updateDefinitionQueries(sorted_dq)

 

Have fun 🙂