we use definition queries to filter data to field users. We have up to 40-50 MXDs that are identical except for 1 variable in the definition query that allows the data for that MXD to be unique. Example - "DeploymentNumber='AA'" where 'AA' is the variable. So, when we update MXDs, we would like to be able to use a python script to be able to update the variable - 'AA' component of our definition query in each layer without having to open each MXD, click on each layer and manually update (which in some cases is a 5 minute operation per MXD).
1 Answer
You'll have to change this for your specific data since there might be layers that you don't want to change / don't have def queries set:
import os
import arcpy
# top level directory where all the map docs are
path = r"<path>"
# recursively search the directory for .mxd files
def getMXD(path):
for dirpath, dirnames, filenames in os.walk(path):
for f in filenames:
if f.endswith(".mxd"):
yield os.path.join(dirpath, f)
for mapdoc in getMXD(path):
# might need to change wildcarding here
mxd = arcpy.mapping.MapDocument(mapdoc)
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
if lyr.supports("DEFINITIONQUERY"):
lyr.definitionQuery = "DeploymentNumber = 'AA'"
mxd.save() # or mxd.saveACopy(<newpath>)
del mxd, df
-
2Was on my way to paste almost identical code :) suggest using
mxd_files = arcpy.ListFiles("*.mxd")
and thenmxd_file_path = os.path.join(mxd_files_folder,mxd_file)
, a bit less code? Commented Nov 18, 2015 at 6:39 -
@AlexTereshenkov, good point (and a lot simpler!) if all your map docs are in the same folder. You'll want
os.walk()
to recursively drill down into sub-folders asarcpy.ListFiles()
only works on whatever is set toenv.workspace
.– PaulCommented Nov 18, 2015 at 6:50 -
ah you are right. You would need to change env.workspace every time you are in another directory.. was always wondering why a parameter
input_folder
was not implemented for theList*
functions in arcpy, is there any specific reason for that? Would love to havearcpy.ListFeatureClasses(input_folder)
:) Commented Nov 18, 2015 at 6:57 -
1
-
I will get into that tomorrow when I get back into the office and let you know how I go. Much appreciated.– Kim GCommented Nov 18, 2015 at 7:20