Select to view content in your preferred language

What should I change to run this python script on the specific layers only (not all the layers in the MXD) , or just some layers in a group ?

1213
4
Jump to solution
09-17-2023 11:59 AM
MAMDOUHZALAKY
Occasional Contributor

 

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):

     if lyr.supports("DEFINITIONQUERY"):

         lyr.definitionQuery = "INSERT_YOUR_DQ_HERE"

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

To insert formatted code:

JohannesLindner_0-1689266678674.pngJohannesLindner_1-1689266693900.png

 

If your definition query is the same for all specified layers:

layer_names = ["Layer A", "Layer B", "Layer E"]

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

 

 

If you want different queries for the layers:

queries = {
    "Layer A": "Query for Layer A",
    "Layer B": "Query for Layer B",
    "Layer E": "Query for Layer E",
}

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name in queries and lyr.supports("DEFINITIONQUERY"):
        lyr.definitionQuery = queries[lyr.name]

Have a great day!
Johannes

View solution in original post

0 Kudos
4 Replies
JeffHouser
Frequent Contributor

For example, maybe try the following:

For file geodatabase (gdb) or shapefile:
lyr.definitionQuery = '"Docket"' + " = " + "'1254-2017'"
or for personal gdb
lyr.definitionQuery = '[Docket]' + " = " + "'1254-2017'"

-Jeff

0 Kudos
MAMDOUHZALAKY
Occasional Contributor

Can you explain to me with more details .... what should i replace in my python script to do what i want ? I do not understand Docket"' + " = " + "'1254-2017'" !!

0 Kudos
JeffHouser
Frequent Contributor

Docket = field in the layers attribute file.

'1254-2017' = is a specific txt value.

Please also refer to JL's post... Great info

-Jeff 

0 Kudos
JohannesLindner
MVP Frequent Contributor

To insert formatted code:

JohannesLindner_0-1689266678674.pngJohannesLindner_1-1689266693900.png

 

If your definition query is the same for all specified layers:

layer_names = ["Layer A", "Layer B", "Layer E"]

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

 

 

If you want different queries for the layers:

queries = {
    "Layer A": "Query for Layer A",
    "Layer B": "Query for Layer B",
    "Layer E": "Query for Layer E",
}

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name in queries and lyr.supports("DEFINITIONQUERY"):
        lyr.definitionQuery = queries[lyr.name]

Have a great day!
Johannes
0 Kudos