How can I repeat the same query multiple times on different layers?

944
6
09-14-2023 10:58 AM
Labels (3)
MAMDOUHZALAKY
New Contributor III

I Have Multiple Layers or Feature Classes with a specific filed , this field is repeated in all layers , How can I repeat the same query for this specific field multiple times on different layers without repeating definition query individually for all this layers one by one ? Iam using ARCGIS 10.8MULTIPLE QUERY.jpg

Tags (3)
0 Kudos
6 Replies
Kara_Shindle
Occasional Contributor III

You could try Python - running it in the Python window in Catalog would allow you to do selections against all the layers.  

If you want to avoid Python, you could manually perform the Query & save the results as temporary layers.

0 Kudos
MAMDOUHZALAKY
New Contributor III

Could you please .... give me more details about how can i use python script in catalog window to do selections against all the layers. , and how can i save my query as a temporary layer ?

0 Kudos
DanHopkins
Esri Contributor

Hi MAMDOUHZALAKY,

Experiment on a copy of your MXD and try the following:

1. Open the MXD.

2. Launch the Python window and add and execute the following:

 

 

 

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
     if lyr.supports("DEFINITIONQUERY"):
         lyr.definitionQuery = "INSERT_YOUR_DQ_HERE"

 

 

 

The code above will set the Definition Query (DQ) for standalone layers, layers within a group, and to layers that are within a group that is within a group in the ArcMap TOC. The limitation with the code above is that it will set the DQ to [PARCEL_ID] = '7' for a layer when the field [PARCEL_ID] and the value 7 do not exist for that layer. When that happens, when you refresh your map after running the code above, you will encounter an ArcMap Drawing Errors dialog that will display something like the following:

One or more layers failed to draw:

LAYERNAME:  An expected Field was not found or could not be retrieved properly. [LAYERNAME]
LAYERNAME:  An expected Field was not found or could not be retrieved properly.

When you encounter the error above, you will have to remove the DQ for that layer using the Layer Properties dialog and Definition Query tab.

Regards,

Dan

MAMDOUHZALAKY
New Contributor III

My First Question is, what should i change or replace in your python script to work with me . Can you give me more details ?

0 Kudos
DanHopkins
Esri Contributor
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
     if lyr.supports("DEFINITIONQUERY"):
         lyr.definitionQuery = "PARCEL_ID = '7'"
MAMDOUHZALAKY
New Contributor III

Ok its work now ..... but suppose that i have 10 layers in the TOC in MXD and i want to run that python script not all the layers i want to run that script on just 5 layers only what should i do to prevent the script from running on the other 5 layers specially if they don't have that field Parcel_ id and i do not want to face that error message ,can i put the layers that i want in a group and run just the script only on them ,how can i do that ?or there are another solution ? Also i noticed that script run on all layers even if they do not have 7 in parcel_id field and hide all the features inside this layer that do not have value 7.

More details i want to run the script on layers aa & bb & cc and they have field parcel_id and value 7 ... but i do not want to run the script on layer fire 1992 that layer has field parcel_id but do not have value 7 or any other values, the script in that case run in all layers even fire1992 and disappear all features inside that layer.

what i want:

1- I want that python script to run on the specific layers only.

2- And other layers that do not have DQ on them, I do not want that error message that you mentioned before if that possible . because logically  some specific layers has parcel _ id field and I want to run the script on them and other layers do not have that field and I do not want to run the script on them .

0 Kudos